Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPanel doesn't update when adding Component in another class

I'm fairly new to Java Swing and I'm running into a few problems.

  1. As a side question, when making a fairly large Java Swing Application, what is the best way to split up code? In my case I want to have an application that has a layout just as Microsoft Word where there is a JToolBar filled with buttons and a main JPanel where changes are made based on the buttons pressed in the Tool Bar.
  2. So as shown in the code below, I have a JFrame and I call the MainPanel class in order to create a panel and add a ToolBar with a button. When the button is pressed it adds a button to the panel. The problem comes when you click the button nothing shows up until you resize the window(in my case I simply manually drag the screen to make it larger).

    public class Main {
    
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("MathMaker");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        //Create the menu bar.  Make it have a green background.
        //MainToolBar mainTB = new MainToolBar();
        MainPanel mainPanel = new MainPanel();
    
        frame.getContentPane().add(mainPanel.getGUI(), BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
    
    }
    

    public class MainPanel implements ActionListener{
    JPanel mPanel;
    JToolBar mToolBar;
    JButton addQuestion;
        public MainPanel() {
            mPanel = new JPanel(new BorderLayout());
            mToolBar = new JToolBar();
            addQuestion = new JButton("test");
    
        addQuestion.addActionListener(this);
    
        mPanel.setLayout(new BorderLayout());
        mPanel.setBackground(new Color(248, 213, 131));
        mPanel.setPreferredSize(new Dimension(200, 180));
    
        mToolBar.add(addQuestion);
        mPanel.add(mToolBar, BorderLayout.PAGE_START);
    }
    public JComponent getGUI()
    {
        return mPanel;
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
         JButton temp = new JButton("temp");
         mPanel.add(temp);
    }
    

    }

like image 386
Toast Avatar asked Dec 05 '22 05:12

Toast


2 Answers

You should revalidate your panel

@Override
public void actionPerformed(ActionEvent e) {
   JButton temp = new JButton("temp");
   mPanel.add(temp);
   mPanel.revalidate();
   mPanel.repaint();
}
like image 170
Sergiy Medvynskyy Avatar answered Jan 20 '23 10:01

Sergiy Medvynskyy


I believe you need to call revalidate() and repaint() to see the changes, here is a similar question here

like image 35
Adam Avatar answered Jan 20 '23 11:01

Adam