1 Attachment(s)
Multi-Panel multi-class GUI communication
Hello everyone! I'm building a (at this point) simple GUI that uses three JPanels. I'm trying to be modular and using different classes for each of the JPanels and the JFrame. I have a button in one JPanel that needs to update text in a label in another JPanel, but I can't figure out how.
I've done a lot of searching on Google and in my Murach's Java SE 6 book, but all the examples build the JPanels in the same class, thus they're able to just use variables for components in other classes directly.
Am I just going in the wrong direction with pulling the nested JPanels out to their own classes? It definitely seems to be easy if I just built all of my panels within the DatabasePanel class, but that doesn't feel very modular.
Below is what feels like the important bits of the code, in-line. I zipped up my Eclipse project (with a few data security-related snips) just in case someone wants to give it a more in-depth.
DatabaseAccessApp.java
Code:
import presentation.DatabaseFrame;
public class DatabaseAccessApp {
public static void main (String args[]) {
DatabaseFrame frame = new DatabaseFrame();
frame.setVisible(true);
}
}
DatabaseFrame.java
Code:
package presentation;
import javax.swing.*;
import java.awt.*;
@SuppressWarnings("serial")
public class DatabaseFrame extends JFrame{
private final int FRAME_WIDTH = 300;
private final int FRAME_HEIGHT = 300;
public DatabaseFrame() {
setTitle("Dashboard");
setSize(FRAME_WIDTH, FRAME_HEIGHT);
centerFrame();
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new DatabasePanel();
this.add(panel);
}
private void centerFrame() {
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
Dimension screenDimensions = defaultToolkit.getScreenSize();
int xPos = (screenDimensions.width - getWidth()) / 2;
int yPos = (screenDimensions.height - getHeight()) / 2;
setLocation(xPos, yPos);
}
}
DatabasePanel.java
Code:
package presentation;
import javax.swing.*;
import java.awt.BorderLayout;
@SuppressWarnings("serial")
public class DatabasePanel extends JPanel {
JPanel headerPanel;
JPanel outputPanel;
JPanel buttonPanel;
JLabel output;
public DatabasePanel() {
this.setLayout(new BorderLayout());
headerPanel = new HeaderPanel();
outputPanel = new OutputPanel();
buttonPanel = new ButtonPanel();
this.add(headerPanel, BorderLayout.NORTH);
this.add(outputPanel, BorderLayout.CENTER);
this.add(buttonPanel, BorderLayout.SOUTH);
}
}
Skipping HeaderPanel.java, as it doesn't apply to this question...
OutputPanel.java
Code:
package presentation;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
class OutputPanel extends JPanel {
JLabel output;
OutputPanel() {
output = new JLabel();
add(output);
}
}
ButtonPanel.java
Code:
package presentation;
import javax.swing.JPanel;
import javax.swing.JButton;
import business.QueryDatabase;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings("serial")
class ButtonPanel extends JPanel implements ActionListener {
JButton exit;
JButton query;
ButtonPanel() {
this.setLayout(new FlowLayout(FlowLayout.RIGHT));
query = new JButton("Run Query");
query.addActionListener(this);
this.add(query);
exit = new JButton("Exit");
exit.addActionListener(this);
this.add(exit);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == exit) {
System.exit(0);
} else if (e.getSource() == query) {
//TODO: Cause query results to appear in output panel
/*
String queryResults = QueryDatabase.query();
query.setText("Query Run");
query.setEnabled(false);
*/
}
}
}