0

I am trying to get the JFrame to refresh as I want to add a JPanel onto a JFrame after it had been set visible. invalidate() then validate() then repaint() won't work as I don't have a layout manager.

The class:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Class4 {

    public static final long serialVersionVID = 1L;

    public void mainMethod(int event){

        JFrame f = new JFrame("Love Test");

        if(event == 0){

            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setSize(500,200);
            f.setLayout(null);

            JPanel p = new JPanel(new BorderLayout());
            p.setBounds(150, 0, 350, 75);
            Class3 c3o = new Class3();
            p.add(c3o);
            f.add(p);

            JPanel p2 = new JPanel();
            Class7 c7o = new Class7();
            p2.add(c7o);
            p2.setBounds(0, 75, 500, 40);
            f.add(p2);

            JPanel p3 = new JPanel();
            p3.setBounds(0, 0, 150, 75);
            Class5 c5o = new Class5();
            Dimension d = new Dimension(150,75);
            c5o.setPreferredSize(d);
            p3.setPreferredSize(d);
            p3.add(c5o);
            f.add(p3);

            f.setVisible(true);

        }

        if(event == 5){


            JPanel p4 = new JPanel();
            p4.setBounds(0,120,500,55);
            Class2 c2o = new Class2();
            Dimension d2 = new Dimension(500,55);
            c2o.setPreferredSize(d2);
            p4.setPreferredSize(d2);
            p4.add(c2o);
            f.add(p4);

                f.invalidate();
            f.validate();
            f.repaint();

        }

    }

}

If I were to use a layout manager, how would I use it? All help will be greatly appreciated. Thanks!

zbz.lvlv
  • 3,597
  • 6
  • 34
  • 38
  • The problem is **most likely** the `null` layout manager. Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. For a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space, to organize the components. For better help sooner, post an [SSCCE](http://sscce.org/). Also please use more sensible class names than `Class5` etc.! – Andrew Thompson May 18 '13 at 04:59
  • Almost looks like it's been decompiled from a class file... – Ash May 18 '13 at 05:07
  • Your posted code doesn't make any sense to me. I don't see where you try to add a panel to a visible frame. I assume you are trying to invoke the method with event = 5. When the method executes you create a new frame and add a panel to it. You never make the frame visible so it just sits in memory, so you only see the original frame. You can't create a new frame every time the method executes. I don't know the context of your code, but I would guess the frame should be a class variable that is created for event 0, and then updated for event 5. – camickr May 18 '13 at 05:08
  • 1
    Also, use class and variable names that actually have some meaning. I second Andrews suggestion to use a Layout Manager. Newbies always think it is easier to use a null layout, but always end up asking questions on the forum wondering why it doesn't work. Use Swing the way it was designed to be used. – camickr May 18 '13 at 05:10
  • PreferredSize, invalidate, revalidate only work in relationship with layout managers. Take those away and these method have (virtually) no meaning – MadProgrammer May 18 '13 at 05:34
  • I transferred to Java from C#... So I find it easy to use setLayout(null). I will use sensible class names for my programmes next time. – zbz.lvlv May 18 '13 at 11:00
  • If I don't use layout manager, is it possible to place the JPanels in the way as shown in the code? Or if I use a layout manager, how? – zbz.lvlv May 18 '13 at 11:03
  • *"is it possible to place the JPanels in the way as shown in the code"* How are they laid out in code? That code does not compile so we can only guess. As I mentioned 23 hours ago, *for better help sooner, post an [SSCCE](http://sscce.org/).* Either that or supply ASCII art of how the GUI should look at minimum size and with extra width & height added. Tip: Add @camickr (or whoever) to notify them of a new comment. – Andrew Thompson May 19 '13 at 05:00

0 Answers0