F
Fencer
Hello, in my program I toggle between two panels depending on which
phase the program is currently in. I wrote a class that functions as the
entry point of the program and it's composed of a JFrame along with the
components of that frame. Now I wanted to break out my two panels to
make the code more readable so I created two new classes that extend
from JPanel and here's where my design issue arose. Consider the code
below (people who have been following my other thread will recognise it
;-)) which is for the "welcome panel" that is shown when the program is
launched:
package gui;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class WelcomePanel extends JPanel {
private static final long serialVersionUID = 1596841645688614873L;
public WelcomePanel(final CenteredGroup inst) {
this.inst = inst;
setPreferredSize(new Dimension(480, 100));
setBorder(new TitledBorder("Start a new session by opening a
BioModel or load a previously saved session"));
setLayout(new GridBagLayout());
initButtons();
}
private void initButtons() {
final CenteredGroup inst2 = this.inst;
b1 = new JButton("Open BioModel");
b1.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
inst2.eventOpenBioModel();
}});
b2 = new JButton("Load Saved Session");
b2.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
inst2.eventLoadSavedSession();
}});
addButtonsToGroupPanel(b1, b2);
}
private void addButtonsToGroupPanel(JButton b1, JButton b2) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.5;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
add(b1, gbc);
gbc.gridx = 1;
add(b2, gbc);
}
private CenteredGroup inst = null;
private JButton b1 = null;
private JButton b2 = null;
}
What I don't like here are the event handlers for the buttons and, more
specifically, I don't like these two things:
1. The class WelcomePanel knows about its "parent", CenteredGroup (I
will change that name btw).
2. I had to change the event*-methods in CenteredGroup from being
private to default access level so I could call them.
Am I right to worry about these design issues and, if so, how should I
solve them? Maybe I shouldn't have created this class in the first place
but I thought the code for CenteredGroup was becoming a bit long.
Thanks for reading and thanks in advance for any help.
- F
phase the program is currently in. I wrote a class that functions as the
entry point of the program and it's composed of a JFrame along with the
components of that frame. Now I wanted to break out my two panels to
make the code more readable so I created two new classes that extend
from JPanel and here's where my design issue arose. Consider the code
below (people who have been following my other thread will recognise it
;-)) which is for the "welcome panel" that is shown when the program is
launched:
package gui;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
public class WelcomePanel extends JPanel {
private static final long serialVersionUID = 1596841645688614873L;
public WelcomePanel(final CenteredGroup inst) {
this.inst = inst;
setPreferredSize(new Dimension(480, 100));
setBorder(new TitledBorder("Start a new session by opening a
BioModel or load a previously saved session"));
setLayout(new GridBagLayout());
initButtons();
}
private void initButtons() {
final CenteredGroup inst2 = this.inst;
b1 = new JButton("Open BioModel");
b1.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
inst2.eventOpenBioModel();
}});
b2 = new JButton("Load Saved Session");
b2.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) {
inst2.eventLoadSavedSession();
}});
addButtonsToGroupPanel(b1, b2);
}
private void addButtonsToGroupPanel(JButton b1, JButton b2) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 1;
gbc.weightx = 0.5;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
add(b1, gbc);
gbc.gridx = 1;
add(b2, gbc);
}
private CenteredGroup inst = null;
private JButton b1 = null;
private JButton b2 = null;
}
What I don't like here are the event handlers for the buttons and, more
specifically, I don't like these two things:
1. The class WelcomePanel knows about its "parent", CenteredGroup (I
will change that name btw).
2. I had to change the event*-methods in CenteredGroup from being
private to default access level so I could call them.
Am I right to worry about these design issues and, if so, how should I
solve them? Maybe I shouldn't have created this class in the first place
but I thought the code for CenteredGroup was becoming a bit long.
Thanks for reading and thanks in advance for any help.
- F