add server side data into html list? - java

i am new in gwt.i want to add data come from serverside in html listbox,so how can w do this??
name is splitted string which i want to add in the listbox...
actually i want to make jqxlistbox type module.http://www.jqwidgets.com/jquery-widgets-demo/demos/jqxlistbox/index.htm
public class imagegrid implements EntryPoint
{
Label l = new Label("search");
TextBox tb=new TextBox();
VerticalPanel panel=new VerticalPanel();
String name=null;
private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
public void onModuleLoad()
{
tb.addKeyUpHandler(new handler());
panel.add(l);
panel.add(tb);
RootPanel.get().add(panel);
}
public class handler implements KeyUpHandler
{
public void onKeyUp(KeyUpEvent event)
{
String ab =tb.getText();
if(ab.length()>0)
{
greetingService.server(ab,new AsyncCallback<String>()
{
#Override
public void onFailure(Throwable caught)
{
Window.alert("Invalid");
}
#Override
public void onSuccess(String result)
{
System.out.println("At the client side..."+result);
if(result.isEmpty())
{
name="not found";
}
else
{
String match=tb.getText();
for (String retval: result.split("/"))
{
name=retval;
}
}
}
});
}
}
}
}

I think what you are looking for is CellList in GWT. You can find the sample code in showcase.
Sorry, I missed the search part of your question. For that you may need to create a new implementation og SuggestBox with CellList as drop-down. Like someone has shown here.

Related

Java - onFormClosing conversion C# to Java

I would like to ask if someone can help me with the following: I am trying to complete the following in Java. I am still quite new to Java and I hope that this can be achieved. Please note that this C# code might not be 100% correct, but I hope you understand what I am trying to achieve. Thank you in advance for your assistance.
//C#
//Form one
public void onFormShown(EventArgs e)
{
if (this.User == null)
{
frmTwo f = new frmTwo();
f += f_onFormClosing(EventArgs e);
this.Hide();
}
}
private void f_onFormClosing(EventArgs e)
{
this.Show();
this.User = f.User;
}
//frmTwo
private void btnClose_Click(EventArgs e)
{
this.Close();
}
EDIT:
private void formWindowOpened(java.awt.event.WindowEvent evt) {
if (loginUser.get().getUserLogin() == null) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Where loginUser is private AtomicReference<Singleton>
//loginUser = new AtomicReference<Singleton>();
//The reason for the data type here is to pass
//the object by reference, log the user in and change
//the value of the Main form's variable
LoginForm login = new LoginForm(loginUser);
login.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent we) {
//here I need to place the logic of
//setting the Main form's 'loginUser'
// object to the value generated in
//the Login form
}
});
}
});
this.setVisible(false);
}
}

Make a class that have "addActionListener" method

i'm doing a class that does a sort of "file explorer",
in the constructor i create the frame ,panel ecc.. but than i want to say to the main program that calls this class that the user has finish the selection, i know i can call a static method that is in the main from this class,but i want to make a action listener because i want to use this class for different programs
For Example if FileEx is my class:
public class FileEx()
{
public FileEx()
{
//program that do something
if(done == true)
//here i want to call the action
}
public void addActionListener(ActionListener ac) //i don't know if it's correct
//but i want something like this
{
}
}
public static void main(String[] args)
{
FileEx fileex = new FileEx();
fileex.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
//when done is true i want this block of code to be called
}
});
}
ActionListeners will only work when added to components that allow them to be added and that notify listeners with them such as JButtons, JMenuItems, JComboBoxes and such. We have no idea what type of class FileEx is or why it should accept an ActionListener and a little more information would be qutie helpful. If you want to notify another object that an event occurs, such as that a calculation is done, use another type of listener such as a PropertyChangeListener. Alternatively you could do the processing in a modal JDialog window, which will notify the calling window that it is done performing its duties by returning code flow to the calling window.
For example, please look at my answers to similar questions:
Drawing with paintComponent after value of Jbutton changed in another class
JTextField data in different frames, with data stored in global variable?
Loop making program freeze
EDIT
For example, if you wanted your FileEx to allow other classes to listen for changes to a String called selection (the so-called "bound" property) you could create it to look something like:
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
public class TestFileEx {
public static void main(String[] args) {
final FileEx fileEx = new FileEx();
fileEx.addPropertyChangeListener(FileEx.SELECTION, new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO code to call when fileEx has changed selections
String fileExSelection = evt.getNewValue().toString();
// or
String fileExSelection2 = fileEx.getSelection();
}
});
}
}
and
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.event.SwingPropertyChangeSupport;
publicclass FileEx {
public static final String SELECTION = "selection";
private SwingPropertyChangeSupport propertyChangeSupport = new SwingPropertyChangeSupport(
this);
private String selection;
public void someMethodThatChangesSelection() {
}
public String getSelection() {
return selection;
}
public void setSelection(String selection) {
String oldValue = this.selection;
String newValue = selection;
this.selection = selection;
// notify the listeners of change
propertyChangeSupport.firePropertyChange(SELECTION, oldValue, newValue);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}
public void rem(String propertyName, PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName, listener);
}
}
Here is the code based on your example which adds actionlisteners and calls them:
public class FileEx()
{
private final List<ActionListener> listeners = new ArrayList<>();
public FileEx()
{
//program that do something
if(done == true) {
notifyListeners();
}
}
public void addActionListener(ActionListener ac)
{
listeners.add(ac);
}
private void notifyListeners()
{
for (final ActionListener listener: listeners)
{
listener.actionPerformed(null);//You can create event if you want.
}
}
}
public static void main(String[] args)
{
FileEx fileex = new FileEx();
fileex.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e)
{
//when done is true i want this block of code to be called
}
});
}

ConfirmDialog add-on in customcomponent?

I did download ConfirmDialog add-on. Now, I'm trying create a confirmdialong in my customcomponent, but I guess that doesn't work with this ui.
public class Customer extends CustomComponent implements Button.ClickListener{
private Button btnSave;
private VerticalLayout vLayout;
public Customer(){
vLayout = new VerticalLayout();
setCompositionRoot(vLayout);
btnSave = new Button("Save");
btnSave.addClickListener(this);
vLayout.addComponent(btnSave);
}
#Override
public void buttonClick(ClickEvent event) {
if(event.getButton() == btnSave){
save();
}
}
/** save informations if ConfirmDialog return true */
private void save(){
ConfirmDialog.show(this, "Please Confirm:", "Are you really sure?",
"I am", "Not quite", new ConfirmDialog.Listener() {
public void onClose(ConfirmDialog dialog) {
if (dialog.isConfirmed()) {
System.out.println(dialog.isConfirmed());
} else {
System.out.println(dialog.isConfirmed()); }
}
});
}
}
when I create ConfirmDialog and pass my CustomComponent as Ui(this) does not accept. I tryed pass null, but does not work also.
the problem is solved. I did use UI.getCurrent() and works.
/** save informations if ConfirmDialog return true */
private void save(){
ConfirmDialog.show(UI.getCurrent(), "Please Confirm:", "Are you really sure?",
"I am", "Not quite", new ConfirmDialog.Listener() {
public void onClose(ConfirmDialog dialog) {
if (dialog.isConfirmed()) {
System.out.println(dialog.isConfirmed());
} else {
System.out.println(dialog.isConfirmed()); }
}
});
}

Access static variable from another class

I have two classes in same package. i have declared a static variable in one class and want to access that variable in another class.
Here is my code in which i have declared the static variable
public class wampusGUI extends javax.swing.JFrame {
static String userCommand;
public wampusGUI() {
initComponents();
}
public void setTextArea(String text) {
displayTextArea.append(text);
}
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
userCommand = commandText.getText();
}
public static void main(String args[]) {
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
wampusGUI w = new wampusGUI();
w.setVisible(true);
Game g = new Game(w);
g.play();
}
});
}
}
Here is the code in which i want to access variable
public class Game {
private wampusGUI gui;
public Game(wampusGUI w) {
world = new World();
world.start();
gui = w;
}
public void play() {
gui.setTextArea(welcome());
gui.setTextArea(describe());
for (;;) {
String s = userCommand; // here value should come should
System.out.println(userCommand);
Command c = Command.create(s);
String r = c.perform(world);
// is game over?
if (r == null) {
break;
}
System.out.println(r);
}
System.out.println("Game over");
}
}
However, i can pass the variable from first class as a argument. but the problem is that, when i will run program the value is going null first time, which i dont want. i want when i enter value in textfield then it should go to another class.
Thank you.
Looking at your code, it seems you want to show dialogs to your user with a certain text
gui.setTextArea(welcome());
gui.setTextArea(describe());
and sometimes, that dialog should capture user input which is handled afterwards.
Those setTextArea calls are not what you want to use. The user will never see the welcome message as it will immediately be replaced by the describe message.
Make sure you do not block the Event Dispatch Thread (EDT) or nothing will be shown at all. I do not know what your Command class will do, but I see an infinite loop on the Event Dispatch Thread which is never a good thing. Take a look at the Concurrency in Swing tutorial for more information
Thanks to that for loop, the user will simply not be capable to input any command as the EDT is busy handling your loop. What you need is a blocking call allowing the user to provide input (not blocking the EDT, but just blocking the execution of your code). The static methods in the JOptionPane class are perfectly suited for this (e.g. the JOptionPane#showInputDialog). These methods also have a mechanism to pass the user input back to the calling code without any static variables, which solves your problem.
I suggest that you use a listener of one sort or another to allow the Game object to listen for and respond to changes in the state of the GUI object. There are several ways to do this, but one of the most elegant and useful I've found is to use Swing's own innate PropertyChangeSupport to allow you to use PropertyChangeListeners. All Swing components will allow you to add a PropertyChangeListener to it. And so I suggest that you do this, that you have Game add one to your WampusGUI class (which should be capitalized) object like so:
public Game(WampusGUI w) {
gui = w;
gui.addPropertyChangeListener(new PropertyChangeListener() {
// ....
}
This will allow Game to listen for changes in the gui's state.
You'll then want to make the gui's userCommand String a "bound property" which means giving it a setter method that will fire the property change support notifying all listeners of change. I would do this like so:
public class WampusGUI extends JFrame {
public static final String USER_COMMAND = "user command";
// ....
private void setUserCommand(String userCommand) {
String oldValue = this.userCommand;
String newValue = userCommand;
this.userCommand = userCommand;
firePropertyChange(USER_COMMAND, oldValue, newValue);
}
Then you would only change this String's value via this setter method:
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
setUserCommand(commandText.getText());
}
The Game's property change listener would then respond like so:
gui.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent pcEvt) {
// is the property being changed the one we're interested in?
if (WampusGUI.USER_COMMAND.equals(pcEvt.getPropertyName())) {
// get user command:
String userCommand = pcEvt.getNewValue().toString();
// then we can do with it what we want
play(userCommand);
}
}
});
One of the beauties of this technique is that the observed class, the GUI, doesn't have to have any knowledge about the observer class (the Game). A small runnable example of this is like so:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
public class WampusGUI extends JFrame {
public static final String USER_COMMAND = "user command";
private String userCommand;
private JTextArea displayTextArea = new JTextArea(10, 30);
private JTextField commandText = new JTextField(10);
public WampusGUI() {
initComponents();
}
private void setUserCommand(String userCommand) {
String oldValue = this.userCommand;
String newValue = userCommand;
this.userCommand = userCommand;
firePropertyChange(USER_COMMAND, oldValue, newValue);
}
private void initComponents() {
displayTextArea.setEditable(false);
displayTextArea.setFocusable(false);
JButton enterButton = new JButton("Enter Command");
enterButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
enterButtonActionPerformed(evt);
}
});
JPanel commandPanel = new JPanel();
commandPanel.add(commandText);
commandPanel.add(Box.createHorizontalStrut(15));
commandPanel.add(enterButton);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(new JScrollPane(displayTextArea));
mainPanel.add(commandPanel, BorderLayout.SOUTH);
add(mainPanel);
}
public void setTextArea(String text) {
displayTextArea.append(text);
}
private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {
setUserCommand(commandText.getText());
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
WampusGUI w = new WampusGUI();
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.pack();
w.setLocationRelativeTo(null);
w.setVisible(true);
Game g = new Game(w);
g.play();
}
});
}
}
class Game {
private WampusGUI gui;
public Game(WampusGUI w) {
gui = w;
gui.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent pcEvt) {
// is the property being changed the one we're interested in?
if (WampusGUI.USER_COMMAND.equals(pcEvt.getPropertyName())) {
// get user command:
String userCommand = pcEvt.getNewValue().toString();
// then we can do with it what we want
play(userCommand);
}
}
});
}
public void play() {
gui.setTextArea("Welcome!\n");
gui.setTextArea("Please enjoy the game!\n");
}
public void play(String userCommand) {
// here we can do what we want with the String. For instance we can display it in the gui:
gui.setTextArea("User entered: " + userCommand + "\n");
}
}
I agree with Jon Skeet that this is not a good solution...
But in case u want an dirty solution to ur problem then u can try this:
public class wampusGUI extends javax.swing.JFrame
{
private static wampusGUI myInstance;
public wampusGUI( )
{
myInstance = this;
initComponents();
}
public static void getUserCommand()
{
if(myInstance!=null)
{
return myInstance.commandText.getText();
}
else
{
return null;
}
}
......
......
}
in the other class use:
public void play()
{
.....
//String s = userCommand; // here value should come should
String s = wampusGUI.getUserCommand();
.....
}
This kind of code is there in some of our legacy projects... and I hate this.

Java - Code in one file and Design in another file (Unrecognized actionPerformed event)

To start sorry for my bad english. I am confronted to a problem. I am creating a new java application and I want to put the design code in a class file named Login_Design.java and de code of the buttons and other content in another class file named Login.java.
The 2 files work like that :
Login_Design.java:
public class Login_Design
{
public static JButton jbtnlogin;
public Login_Design()
{
initComponents();
}
public void initComponents()
{
jframelogin = new JFrame();
(...)
jbtnlogin = new JButton();
(...)
jbtnlogin.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
Login.jbtnloginActionPerformed(event);
}
});
jframelogin.add(jbtnlogin);
}
}
Login.java:
package unigamex;
import java.awt.event.ActionEvent;
import unigamex.Login_Design;
public class Login
{
public Login()
{
new Login_Design();
}
protected void jbtnloginActionPerformed(ActionEvent event)
{
System.exit(0);
}
public static void main(String args[])
{
new Login();
}
public void JActionPerformed()
{
Login_Design.jbtnlogin.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
jbtnloginActionPerformed(event);
}
});
}
}
Now the problem is :
1 - If I put that code in the same java file, all works (all the buttons work)
2 - If I do like i am doing right here, all buttons are showed but it is not possible to use them.
3 - Remove the public void JActionPerformed() and putting the code in the Login_Design
initcomponents camp, but he shows me the fallowing message :
Cannot make a static reference to the non-static method jbtnsairActionPerformed(ActionEvent) from the type Login
How can i solve that problem ?
Thanks in advance,
Luis Da Costa
One way to do it would be to make Login implement ActionListener and have it register with the Login_Design object as such. The Login_Design class would setup any registrants as ActionListeners on the buttons.
EDIT:
in Login_Design:
public void addActionListenerToButtons(ActionListener listener){
jbtnlongin.addActionListener(listener);
}
then in Login add this to the Login_Design instance.
public void actionPerformed(java.awt.event.ActionEvent event)
{
Login.jbtnloginActionPerformed(event);
}
You are trying to access a method that can only be accessed via an instance variable (i.e. it is not static).
What you should do is something like the following:
public class Login
{
public Login()
{
new Login_Design(this);
}
protected void jbtnloginActionPerformed(ActionEvent event)
{
System.exit(0);
}
}
public class Login_Design{
private Login login;
public Login_Design(Login login){
this.login = login;
initComponents();
}
public void initComponents()
{
//...
jbtnlogin = new JButton();
jbtnlogin.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent event)
{
login.jbtnloginActionPerformed(event);
}
}
)
//...
}
}
}

Categories