public class OpenFrame extends JFrame implements ActionListener {
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private BudgetPlanner budgetPlanner;
private JLabel welcomeMessage;
private JPanel openpanel = new JPanel();
private JButton loadButton = new JButton();
private JButton resetButton = new JButton();
private JFrame openFrame;
private JLabel getWelcomeMessage;
public BudgetPlanner callBudgetPlanner() {
try {
budgetPlanner = new BudgetPlanner();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return budgetPlanner;
}
public OpenFrame() {
openFrame = new JFrame();
openFrame.setTitle("BudgetPlanner");
openFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
openFrame.setResizable(false);
openFrame.setLayout(null);
openFrame.setLocationRelativeTo(null);
openFrame.setSize(new Dimension(WIDTH, HEIGHT));
openFrame.getContentPane().setBackground(Color.GRAY);
openPanel();
openFrame.setContentPane(openpanel);
openFrame.setVisible(true);
}
public void openPanel() {
openpanel.setLayout(null);
welcomeMessage = new JLabel("Hello there! Welcome back. Choose an option below:");
welcomeMessage.setVerticalTextPosition(TOP);
welcomeMessage.setHorizontalTextPosition(SwingConstants.CENTER);
welcomeMessage.setBounds(50, 20, 500, 200);
welcomeMessage.setFont(new Font("Arial", Font.PLAIN, 17));
openpanel.setBounds(0, 0, 500, 500);
openpanel.add(welcomeMessage);
loadButton.setBounds(200, 170, 100, 50);
loadButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
loadButton.setText("Load Data");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LoadCompletePanel loadCompletePanel = new LoadCompletePanel();
openpanel.add(loadCompletePanel);
}
});
loadButton.setFocusable(false);
resetButton.setBounds(200, 230, 100, 50);
resetButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
resetButton.setText("Reset Data");
resetButton.addActionListener(this);
resetButton.setFocusable(false);
openpanel.add(loadButton);
openpanel.add(resetButton);
}
Above is my code, and I have an issue. I would like to call a new panel (made in a separate class) by clicking on loadButton, but it doesn't seem to work - nothing happens even if I click on the button.
How should I fix this? By the way, below is the code for a new panel:
public class LoadCompletePanel extends JPanel implements ActionListener {
private BudgetPlanner budgetPlanner;
private JPanel loadCompletePanel;
private JLabel loadCompleteMessage;
private JButton addExpense;
private JButton addIncome;
private JButton viewMonthlyExpense;
private JButton viewMonthlyIncome;
public LoadCompletePanel() {
loadCompletePanel = new JPanel();
loadScreenPanel();
loadCompletePanel.add(loadCompleteMessage);
loadCompletePanel.setBounds(200, 170, 100, 50);
setVisible(true);
}
private void loadScreenPanel() {
loadCompletePanel.setLayout(null);
loadCompleteMessage = new JLabel("Loading Complete.");
loadCompleteMessage.setBounds(130, 50, 500, 200);
}
Sorry if this is a very trivial & basic question. I'm still a beginner at Java. Any help would be appreciated.
CodePudding user response:
Do the following:
public void actionPerformed(ActionEvent e) {
LoadCompletePanel loadCompletePanel = new LoadCompletePanel();
openpanel.add(loadCompletePanel);
openPanel.repaint();
openPanel.revalidate();
}
This code will make sure that the panel paints the newly added components.
CodePudding user response:
Since I'm not familiar with setLayout(null) associated with setBounds(...) and I haven't been doing Swing for a long time, I never managed to display the LoadCompletePanel instance even directly. Anyway as the problem is in the action listener I replaced the loadCompletePanel panel with the LoadindCompleteLabel label. For this label to appear in the action listener, you must invoke repaint() on the effected container.
public void openPanel() {
openpanel.setLayout(null);
welcomeMessage = new JLabel("Hello there! Welcome back. Choose an option below:");
welcomeMessage.setVerticalTextPosition(SwingConstants.TOP);
welcomeMessage.setHorizontalTextPosition(SwingConstants.CENTER);
welcomeMessage.setBounds(50, 20, 500, 200);
welcomeMessage.setFont(new Font("Arial", Font.PLAIN, 17));
openpanel.setBounds(0, 0, 500, 500);
openpanel.add(welcomeMessage);
openpanel.add(loadCompletePanel);
JLabel LoadindCompleteLabel = new JLabel("Loading Complete.");
LoadindCompleteLabel.setBounds(130, 50, 500, 200);
loadButton.setBounds(200, 170, 100, 50);
loadButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
loadButton.setText("Load Data");
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openpanel.add(LoadindCompleteLabel);
openpanel.repaint();
}
});
loadButton.setFocusable(false);
resetButton.setBounds(200, 230, 100, 50);
resetButton.setFont(new Font("Arial", Font.CENTER_BASELINE, 15));
resetButton.setText("Reset Data");
resetButton.addActionListener(this);
resetButton.setFocusable(false);
openpanel.add(loadButton);
openpanel.add(resetButton);
}