patternjavaMinor
Splitting the GUI into smaller classes
Viewed 0 times
theintosplittingsmallerclassesgui
Problem
I am currently working on a course project and have been assigned to write a GUI. I've written it and it's about 2000 lines of code. It will be bigger when I add the SQL codes and new panels. So, I've successfully divided it into different classes for each screen. My question is: how do I improve that solution? Here is the first shape of code. I'll just write a small part of code for better clarity.
```
/**
*When the program starts, first I call passScreen. GUI class extends the JFrame by the way.
*/
public class GUI extends JFrame{
public GUI() {
passScreen();
setSize(700,700);
setResizable(true);
setTitle("KARGO");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
/**
*When a user clicks login button, I simply making panelG invisible, calling client method and
*then removing it. I am not writing user_id and password control codes for the sake of clarity
*/
private void passScreen(){
final JPanel panelG = new JPanel();
...
...
JButton login = new JButton("Login");
login.addActionListener(new ActionListener){
panelG.setVisible(false);
client();
getContentPane().remove(panelG);
panelG.removeAll();
}};
setContentPane(panelG);
}
/**
*When the user clicks exit, I make the current pane invisible, calling
*passScreen method.
*/
private void client(){
final JPanel panelG = new JPanel();
...
...
```
/**
*When the program starts, first I call passScreen. GUI class extends the JFrame by the way.
*/
public class GUI extends JFrame{
public GUI() {
passScreen();
setSize(700,700);
setResizable(true);
setTitle("KARGO");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
/**
*When a user clicks login button, I simply making panelG invisible, calling client method and
*then removing it. I am not writing user_id and password control codes for the sake of clarity
*/
private void passScreen(){
final JPanel panelG = new JPanel();
...
...
JButton login = new JButton("Login");
login.addActionListener(new ActionListener){
panelG.setVisible(false);
client();
getContentPane().remove(panelG);
panelG.removeAll();
}};
setContentPane(panelG);
}
/**
*When the user clicks exit, I make the current pane invisible, calling
*passScreen method.
*/
private void client(){
final JPanel panelG = new JPanel();
...
...
Solution
I have to disagree with rolfl's approach here. I do not think it is necessary to
I would say that your first version of code is better than the second version. What you are doing in your new code is that you are creating
Instead, you could create an utility method anywhere that either can give you a
Here is an example (since I do not know all your code, this is similar to the way you used it).
Because you say that you want to avoid having methods
Now you just need to call this method, so
I do think you should apply this on your classes so that in the end, the
extend the classes. I have brought this issue up in an answer here.I would say that your first version of code is better than the second version. What you are doing in your new code is that you are creating
new Client() and then directly calling a method on that object (Client isn't the only example here).Instead, you could create an utility method anywhere that either can give you a
JPanel without actually adding it to the JFrame (in my opinion, this is better, it should be up to the caller to determine what to do with the returned object) or a method can work like you are using it now (both creating and adding it - although I do not recommend this). You don't need to extend any classes for this (or create one class for each utility method, which is essentially what you have done). These utility method does not need to be public, and they can all be placed within the same class!Here is an example (since I do not know all your code, this is similar to the way you used it).
Because you say that you want to avoid having methods
public, and also you want to avoid having everything in the same class, you can create a MyComponents class where you can put static methods.public class MyComponents {
// using default visibility so that it is only visible to the same package
static void createClientPanel(final JFrame g){
final JPanel panelG = new JPanel();
...
...
JButton exit = new JButton();
exit.addActionListener(new ActionListener){
panelG.setVisible(false);
new PassScreen().passScreen(g);
g.getContentPane().remove(panelG);
panelG.removeAll();
}};
g.setContentPane(panelG);
}
}Now you just need to call this method, so
new Client().client(g); could be changed to MyComponents.createClientPanel(g);I do think you should apply this on your classes so that in the end, the
GUI class and this MyComponents class is all that you have left.Code Snippets
public class MyComponents {
// using default visibility so that it is only visible to the same package
static void createClientPanel(final JFrame g){
final JPanel panelG = new JPanel();
...
...
JButton exit = new JButton();
exit.addActionListener(new ActionListener){
panelG.setVisible(false);
new PassScreen().passScreen(g);
g.getContentPane().remove(panelG);
panelG.removeAll();
}};
g.setContentPane(panelG);
}
}Context
StackExchange Code Review Q#36363, answer score: 3
Revisions (0)
No revisions yet.