PDA

View Full Version : Java: create UI based on Hashtable contents


Roelf
07-11-2005, 12:18 PM
For development of an application, i have to create a stub for another application. The interface for this application is predefined, so can't touch that. Everything has to run in a Windows environment, i use Eclipse as an IDE.

What have is the following. On several places in my appl, the stub is to be called. Data-transfer between the applications is done by a packed Hashtable. Every object in the table, is a String.
On a certain moment within the interfacing, a go() method is called.

What i need to do, is give an implementation to this method so it opens a Window where i can edit the values in this Hashtable. The contents of this Hashtable is not the same on every call so i have to create a UI on the fly, where every key in the Hashtable is displayed by a label and every value in the Hashtable is displayed as a textbox with content.

After this i have to refill the Hashtable with the values entered or changed in the UI.

All interactions with the Hashtable i can manage myself, but creating a UI is another piece of cake. Which package do i use, awt or swing? How do i create a window (in vb it would be a form) which i fill with the neccesary labels and textboxes? My knowledge of creating UI in Java is not sufficient to do it in a little amount of time, don't have the time to study all i need to know either, so....

Any help is welcome!

Roelf

nikkiH
07-11-2005, 05:23 PM
I'm no Java GUI wizard myself, but if you use Eclipse, you can use the little add-on thingy that gives you a more VB-ish feel to creating the UI if you use their SWT components. Drag and drop and all that stuff. Eclipse has a tutorial on it somewhere. I found it with Google when I was mucking around one time.

I can't help much, but no one else answered this, so... :D

suryad
07-13-2005, 10:24 AM
I am trying to understand your proj...so uhh the hashtable values have to have some kind of a visual representation in the new Frame that is being created right...are you using a JTable object to display the values and so on? Or how are you dealign with it?

Roelf
07-13-2005, 01:37 PM
currently i have this. Now the display of this method has to be synchronous, so when the screen is exited, the calling method has to proceed. Next, the calling application calls a method which returns a Hashtable with the new values entered in the displayed window

public class AisStubInterface{

public JLabel[] labels;
public JTextField[] textfields;
public JFrame frame;

public void createAndShowGUI(Hashtable pars) {
int numRows = pars.size();
labels = new JLabel[numRows];
textfields = new JTextField[numRows];


GridLayout gl = new GridLayout();
gl.setRows(numRows);
gl.setColumns(2);

frame = new JFrame("AisStub");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE );
frame.getContentPane().setLayout(gl);;

Enumeration keys = pars.keys();
Enumeration vals = pars.elements();

int i = 0;
while (keys.hasMoreElements()){
String name = (String) keys.nextElement();
String value = (String) vals.nextElement();
labels[i] = new JLabel(name);
frame.getContentPane().add(labels[i]);
textfields[i] = new JTextField(value);
textfields[i].setName(name);
frame.getContentPane().add(textfields[i]);
i++;
}

//Display the window.
frame.pack();
frame.setVisible(true);

}
}

Just to make it clear

The calling application executes a method "setParameters(Hashtable pars)" which makes the Hashtable available to the class implementing the defined interface
Then the calling application calls a "go()" method, in which the actual work has to be done: display the window, wait for values to change by user, close window by user, proceed with a specified returnvalue
Then the app calls a "Hashtable getParameters()" method which clearly returns the changed contents of the window in a Hashtable to the calling application.
After al that, the interfacing is finished.

Currently in the go() method i have this:

ui = new AisStubInterface();
ui.createAndShowGUI(fromFlowerHash);
// now we have to wait for the window to close, but how do we do that
return FlowerFace.FLOWERFACE_SUCCESS;

shmoove
07-13-2005, 03:39 PM
Instead of having the calling method wait for the data to be edited and the window to be closed you could make a callback method that the window you create can call on it's close event. So the go() method just opens the window, and updateData() method in the callee is called from the window when it's finished. If you need to make it possible to use this from many different classes you can use a listener interface. Something like:

public interface AisStubInterfaceListener {
public void updateData();
}


shmoove