patternjavaMinor
Swing application: Working with GridBagLayout and Mediator pattern
Viewed 0 times
gridbaglayoutapplicationwithworkingswingmediatorandpattern
Problem
SCENARIO:
As a follow-up of this question on Stackoverflow asked few days back, I am working on a swing application(article tagging tool) where the user(on an initial jpanel) would fill up some mandatory fields and then proceed to different sections(front, body and back matter of the article) where the application would need to process a lot of RTF files and create corresponding XML files.
The application(using GridBagLayout as the layout manager and the mediator as the design pattern) has the LaunchApplication class having the static SwingUtilities.invokeLater method which invokes the createAndShowGUI method and the application just starts off.
QUESTIONS:
Any help, suggestions, criticisms are appreciated. The output UI and relevant code are posted below.
Mediator interface:
```
package com.lspl.www.jbac.core;
import com.lspl.www.jbac.ui.BackMatterBtn;
import com.lspl.www.jbac.ui.BodyMatterBtn;
import com.lspl.www.jbac.ui.FrontMatterBtn;
import com.lspl.www.jbac.ui.HeaderLabel;
/**
* The Interface Mediator.
*
* @author sandeep
* @version 1.0
* @see http://en.wikipedia.org/wiki/Mediator_pattern#Java
*
* Each participant communicates to the mediator its
* activity and the mediator dispatches the expected behavior to the other
* participants.
*/
public interface Mediator {
/**
* Front matter.
*/
public void frontMatter();
/**
* Body matter.
*/
public void bodyMatter();
/**
* Back matter.
*/
public void backMatter();
/**
* Register front matter.
*
* @param frontMatterButton the front matter button
*/
public void registerFrontMatter(FrontMatterBtn frontMatterButton);
/**
* Register body matter.
*
* @param bodyMatterButton the body matter button
*/
public void registerB
As a follow-up of this question on Stackoverflow asked few days back, I am working on a swing application(article tagging tool) where the user(on an initial jpanel) would fill up some mandatory fields and then proceed to different sections(front, body and back matter of the article) where the application would need to process a lot of RTF files and create corresponding XML files.
The application(using GridBagLayout as the layout manager and the mediator as the design pattern) has the LaunchApplication class having the static SwingUtilities.invokeLater method which invokes the createAndShowGUI method and the application just starts off.
QUESTIONS:
- Am I implementing the mediator pattern correctly?
- I want to create add/remove pannels for each button click and switch between them. What would be the recommended way to do it?
Any help, suggestions, criticisms are appreciated. The output UI and relevant code are posted below.
Mediator interface:
```
package com.lspl.www.jbac.core;
import com.lspl.www.jbac.ui.BackMatterBtn;
import com.lspl.www.jbac.ui.BodyMatterBtn;
import com.lspl.www.jbac.ui.FrontMatterBtn;
import com.lspl.www.jbac.ui.HeaderLabel;
/**
* The Interface Mediator.
*
* @author sandeep
* @version 1.0
* @see http://en.wikipedia.org/wiki/Mediator_pattern#Java
*
* Each participant communicates to the mediator its
* activity and the mediator dispatches the expected behavior to the other
* participants.
*/
public interface Mediator {
/**
* Front matter.
*/
public void frontMatter();
/**
* Body matter.
*/
public void bodyMatter();
/**
* Back matter.
*/
public void backMatter();
/**
* Register front matter.
*
* @param frontMatterButton the front matter button
*/
public void registerFrontMatter(FrontMatterBtn frontMatterButton);
/**
* Register body matter.
*
* @param bodyMatterButton the body matter button
*/
public void registerB
Solution
Java 7 comes with multi-catch: You can catch multiple exceptions with the same code block.
By using it, you can change
to
Even if you think this is not as readable as it could be, blank lines could make it readable:
Additionally, you seem to have a blank line in between each line of code. I don't know whether this is due to your line endings (I don't think so). But consider using blank lines to indicate a semantic cohesion of sorts - group related lines together.
Compare the following:
with
To me, this is far more readable. You have a bit that creates the
Heck, you could even extract them:
```
/**
* For all rows, gridy should be the row number, and gridx will be 0 for the
* label and 1 for the textfield.
*
* @param position
* the position
* @param string
* the string
* @return the jtext field
*/
private JTextField addTextFieldCell(int position, String string) {
GridBagConstraints gridBagConstraints = createGridBagConstraintsForTextField(position);
JTextField
By using it, you can change
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
LaunchApplication launchApplication = new LaunchApplication();
launchApplication.createAndShowGUI();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
});
}to
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
LaunchApplication launchApplication = new LaunchApplication();
launchApplication.createAndShowGUI();
} catch (ClassNotFoundException|InstantiationException|IllegalAccessException|UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
});
}Even if you think this is not as readable as it could be, blank lines could make it readable:
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
LaunchApplication launchApplication = new LaunchApplication();
launchApplication.createAndShowGUI();
} catch (ClassNotFoundException |
InstantiationException |
IllegalAccessException |
UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
});
}Additionally, you seem to have a blank line in between each line of code. I don't know whether this is due to your line endings (I don't think so). But consider using blank lines to indicate a semantic cohesion of sorts - group related lines together.
Compare the following:
/**
* For all rows, gridy should be the row number, and gridx will be 0 for the
* label and 1 for the textfield.
*
* @param position
* the position
* @param string
* the string
* @return the jtext field
*/
private JTextField addTextFieldCell(int position, String string) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.ipadx = 10;
JTextField textField = new JTextField(string);
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = position;
textField.setColumns(10);
this.add(textField, gridBagConstraints);
return textField;
}with
/**
* For all rows, gridy should be the row number, and gridx will be 0 for the
* label and 1 for the textfield.
*
* @param position
* the position
* @param string
* the string
* @return the jtext field
*/
private JTextField addTextFieldCell(int position, String string) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.ipadx = 10;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = position;
JTextField textField = new JTextField(string);
textField.setColumns(10);
this.add(textField, gridBagConstraints);
return textField;
}To me, this is far more readable. You have a bit that creates the
GridBagConstraints, a bit that creates a JTextField, a bit that adds the JTextField and the GridBagConstraints and a bit that returns the textfield.Heck, you could even extract them:
```
/**
* For all rows, gridy should be the row number, and gridx will be 0 for the
* label and 1 for the textfield.
*
* @param position
* the position
* @param string
* the string
* @return the jtext field
*/
private JTextField addTextFieldCell(int position, String string) {
GridBagConstraints gridBagConstraints = createGridBagConstraintsForTextField(position);
JTextField
Code Snippets
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
LaunchApplication launchApplication = new LaunchApplication();
launchApplication.createAndShowGUI();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
});
}/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
LaunchApplication launchApplication = new LaunchApplication();
launchApplication.createAndShowGUI();
} catch (ClassNotFoundException|InstantiationException|IllegalAccessException|UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
});
}/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
LaunchApplication launchApplication = new LaunchApplication();
launchApplication.createAndShowGUI();
} catch (ClassNotFoundException |
InstantiationException |
IllegalAccessException |
UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
});
}/**
* For all rows, gridy should be the row number, and gridx will be 0 for the
* label and 1 for the textfield.
*
* @param position
* the position
* @param string
* the string
* @return the jtext field
*/
private JTextField addTextFieldCell(int position, String string) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.ipadx = 10;
JTextField textField = new JTextField(string);
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = position;
textField.setColumns(10);
this.add(textField, gridBagConstraints);
return textField;
}/**
* For all rows, gridy should be the row number, and gridx will be 0 for the
* label and 1 for the textfield.
*
* @param position
* the position
* @param string
* the string
* @return the jtext field
*/
private JTextField addTextFieldCell(int position, String string) {
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.EAST;
gridBagConstraints.ipadx = 10;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = position;
JTextField textField = new JTextField(string);
textField.setColumns(10);
this.add(textField, gridBagConstraints);
return textField;
}Context
StackExchange Code Review Q#68005, answer score: 2
Revisions (0)
No revisions yet.