简体   繁体   中英

How to add components to a JPanel that was generated by netbeans gui builder in my source code.

I am writing a program that draws different shapes onto a jPanel given different dimensions I used the netbeans gui builder to make the gui part, it generated a JPanel(jPanel1) where I want to draw this graphics component I have a class(MyGraphics) to do this so I do jPanel1.add(new MyGraphics()); but it does not do anything. How do I add stuff to this autogenerated JPanel? I tried doing jPanel1.setBackground(Color.red); and it worked fine so why doesn't the jPanel1.add(new MyGraphics()); work?

Try to call the JPanel method repaint or paintImmediately (inherited from JComponent).

Usually repaint would be dispatched to a runnable like this:

SwingUtilities.invokeLater(new Runnable() {
public void run() {
        repaint();
    }
}); 

Edit: If you have an GUI that a user interacts with (eg a button that fire event's), it is very important that you make use of concurrency so the application doesn't freeze.

Some useful reading on concurrency

Good luck :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM