Showing posts with label gui. Show all posts
Showing posts with label gui. Show all posts

Friday, June 27, 2008

Pop-up Window with Return Value

Trick
In this post a trick is presented: possibility to return object from a pop-up dialog.

It can be useful to
  • select and return an element of list from a pop-up dialog
  • create a new object in a pop-up dialog and return it
  • edit an object in a pop-up dialog and return it
Participants
Caller - the place, where the pop-up window is opened and the returned data is processed.
Dialog - this is the pop-up window, what produces the return object.

Step 1: Creating the dialog
Create a GUI window, what extends the javax.swing.JDialog class, like this:



My sample dialog creates and returns a String instance from a TextField.

Step 2: Extend the dialog code
First add a new class member to the created dialog class:

private <ReturnObjectClass> returnValue;

where the <ReturnObjectClass> is the class of the object, what our pop-up window returns, in my case:

private String returnValue;

Now write the actions to the buttons.
Cancel button close the window and return null:

returnValue =null;
dispose();

Return button close the window and return the new String:

returnValue = jTextField1.getText();
dispose();

At least construct the method, what return the created object:

public String getValue() {
this.setVisible(true);
return returnValue;
}

Step 3: Creating the caller
Create a class, named Main, and put the following code:

public static void main(String[] args) {
String s = new GetStringDialog(null, true).getValue();
System.out.println("Dialog returned with: " + s);
}

After you run this program, the pop-up dialog is showed and after clicking the Return button the typed string is written to the console.

Downloads
You can download sample code here as NetBeans project.

Monday, June 23, 2008

Mediator Design Patter

I present this post an useful adaptation of Mediator Design Pattern. We need often create GUI forms and of course we want to process the information given by users.

Participants of Mediator Design Patter
(based on Wikipedia)

Mediator - defines the interface for communication between Colleague objects.

ConcreteMediator - implements the Mediator interface and coordinates communication between Colleague objects. It is aware of all the Colleagues and their purpose with regards to inter communication.

ConcreteColleague - communicates with other Colleagues through its Mediator



Step 1: Create the Mediator interface
In the 1rst step we create the mediator interface:

1. public interface Mediator {
2. void setData( HashMap<String, Object> data );
3. HashMap<String, Object> getData();
4. }

Import the required classes and our Mediator is done.
Object will communicate over this interface with each other.


Step 2: Create the form
We create a simple GUI frame with two JTextField and two JButton, like this:



Step 3: Create the ConcreteMediator
To create the ConcreteMediator, implement the Mediator interface with our GUI Frame.
Change the
public class MediatorExample extends javax.swing.JFrame {

line to
public class MediatorExample extends javax.swing.JFrame imlements Mediator {

and implement the required methods, declared in Mediator interface:
public void setData(HashMap data) {
textfield_info1.setText((String) data.get("info1"));
textfield_info2.setText((String) data.get("info2"));
}
and
public HashMap getData() {
HashMap data = new HashMap();
data.put("info1", textfield_info1.getText());
data.put("info2", textfield_info2.getText());
return data;
}

Import the required classes and our ConcreteMediator is done (textfield_info1 and textfield_info2 are the name of two JTextField instance variable).


Step 4: Create the ConcreteColleague
In our example the ConcreteColleague has two function:
it can open a new Frame and fill it with initial data throught the Mediator interface
it can process form data throught Mediator interface.
My code:
public class ImportantDataProcessor {

public void showImportantDataDialog() {
HashMap<String, Object> data = new HashMap<String, Object>();
data.put("info1", "This information is very important!");
data.put("info2", "And Top Secret!");

MediatorExample frame = new MediatorExample();
frame.setData(data);
frame.setVisible(true);

}

public void processImportantData(Mediator mediator) {
HashMap<String,Object> data = mediator.getData();

String info1 = (String) data.get("info1");
String info2 = (String) data.get("info2");

JOptionPane.showMessageDialog(null, info1 + "\n" + info2, "Important Information",
JOptionPane.INFORMATION_MESSAGE);
}
}

Import the required classes and our ConcreteColleague is done .


Step 5: Process the form
Now we write the onClick() events of the two buttons.
Exit button will close the application:
this.dispose();
and the Process... button will start the form processing:
new ImportantDataProcessor().processImportantData(this);
The "this" word refer to the GUI frame itself, what implements the Mediator interface.


Step 6: Start the application
Create a class, named Main, and put the following code:
public static void main(String[] args) {
new ImportantDataProcessor().showImportantDataDialog();
}


Downloads
You can download sample code here as NetBeans project.