JFrame help please?

Discussion in 'Java' started by A Watcher, Sep 8, 2007.

  1. A Watcher

    A Watcher Guest

    I use the setContentPane method to put a JPanel into a JFrame and then
    call validate and repaint. That works fine. What I can't do is
    change it. I rerun the setContentPane method with a different JPanel
    and do the validate and repaint but nothing happens. I am using java
    5. Can someone suggest something I could try?

    Thanks.
    A Watcher, Sep 8, 2007
    #1
    1. Advertising

  2. A Watcher

    SadRed Guest

    On Sep 8, 9:24 am, A Watcher <> wrote:
    > I use the setContentPane method to put a JPanel into a JFrame and then
    > call validate and repaint. That works fine. What I can't do is
    > change it. I rerun the setContentPane method with a different JPanel
    > and do the validate and repaint but nothing happens. I am using java
    > 5. Can someone suggest something I could try?
    >
    > Thanks.


    Call
    frame.invalidate();
    frame.validate();
    SadRed, Sep 8, 2007
    #2
    1. Advertising

  3. A Watcher

    A Watcher Guest

    SadRed wrote:
    > On Sep 8, 9:24 am, A Watcher <> wrote:
    >> I use the setContentPane method to put a JPanel into a JFrame and then
    >> call validate and repaint. That works fine. What I can't do is
    >> change it. I rerun the setContentPane method with a different JPanel
    >> and do the validate and repaint but nothing happens. I am using java
    >> 5. Can someone suggest something I could try?
    >>
    >> Thanks.

    >
    > Call
    > frame.invalidate();
    > frame.validate();
    >


    Thanks, I'll try that on Monday when I get back to work.
    A Watcher, Sep 8, 2007
    #3
  4. A Watcher wrote:
    > I use the setContentPane method to put a JPanel into a JFrame and then
    > call validate and repaint. That works fine. What I can't do is
    > change it. I rerun the setContentPane method with a different JPanel
    > and do the validate and repaint but nothing happens. I am using java
    > 5. Can someone suggest something I could try?
    >
    > Thanks.
    >


    There is something interesting happening here. The test code below
    works fine calling setContentPane() from the EDT and following it with
    validate(). If I take out the validate() statement it is messed up. If
    I call setContentPane() from the run() off the EDT it works fine. I
    don't think that is safe but it does work (at least it does on my computer).

    It is a requirement that the content pane be an opaque component. I
    don't know what will happen if it isn't.

    You should also look at JComponent.revalidate(). It schedules
    validation of your JComponent's layout. As it states in the docs, this
    is generally not required with JComponents as any size change or layout
    modification will call it automatically.

    Top level containers that do not extend JComponent do need to have
    validate() called when their layout is changed.

    If my example here doesn't adequately describe your problem, please post
    a test example of your own that shows the problem.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test6 extends JFrame implements Runnable {
    KPanel[] panels = new KPanel[2];
    int n;

    public test6() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panels[0] = new KPanel(Color.GREEN);
    panels[1] = new KPanel(Color.BLUE);

    setContentPane(panels[0]);

    setSize(400,300);
    setVisible(true);

    new Thread(this).start();
    }

    public void run() {
    while (true) {
    ++n;
    n %= 2;
    Runnable r = new Runnable() {
    public void run() {
    setContentPane(panels[n]);
    validate();
    }
    };
    EventQueue.invokeLater(r);

    // setContentPane(panels[n]);

    try {
    Thread.sleep(2000);
    } catch (InterruptedException ie) { }
    }
    }

    class KPanel extends JPanel {
    final private Color color;

    public KPanel(Color color) {
    this.color = color;

    setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = c.gridy = 0; c.insets = new Insets(2,2,2,2);

    JButton[] b = new JButton[4];
    for (int i=0; i<b.length; i++)
    b = new JButton("Button " + Integer.toString(i+1));

    add(b[0],c);
    ++c.gridx;
    add(b[1],c);
    c.gridx = 0; ++c.gridy;
    add(b[2],c);
    ++c.gridx;
    add(b[3],c);
    }

    public void paintComponent(Graphics g) {
    g.setColor(color);
    g.fillRect(0,0,getWidth(),getHeight());
    }
    }

    public static void main(String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    new test6();
    }
    };
    EventQueue.invokeLater(r);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Sep 8, 2007
    #4
  5. A Watcher

    A Watcher Guest

    Knute Johnson wrote:
    > A Watcher wrote:
    >> I use the setContentPane method to put a JPanel into a JFrame and then
    >> call validate and repaint. That works fine. What I can't do is
    >> change it. I rerun the setContentPane method with a different JPanel
    >> and do the validate and repaint but nothing happens. I am using java
    >> 5. Can someone suggest something I could try?
    >>
    >> Thanks.
    >>

    >
    > There is something interesting happening here. The test code below
    > works fine calling setContentPane() from the EDT and following it with
    > validate(). If I take out the validate() statement it is messed up. If
    > I call setContentPane() from the run() off the EDT it works fine. I
    > don't think that is safe but it does work (at least it does on my
    > computer).
    >
    > It is a requirement that the content pane be an opaque component. I
    > don't know what will happen if it isn't.
    >
    > You should also look at JComponent.revalidate(). It schedules
    > validation of your JComponent's layout. As it states in the docs, this
    > is generally not required with JComponents as any size change or layout
    > modification will call it automatically.
    >
    > Top level containers that do not extend JComponent do need to have
    > validate() called when their layout is changed.
    >
    > If my example here doesn't adequately describe your problem, please post
    > a test example of your own that shows the problem.
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    >
    > public class test6 extends JFrame implements Runnable {
    > KPanel[] panels = new KPanel[2];
    > int n;
    >
    > public test6() {
    > setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >
    > panels[0] = new KPanel(Color.GREEN);
    > panels[1] = new KPanel(Color.BLUE);
    >
    > setContentPane(panels[0]);
    >
    > setSize(400,300);
    > setVisible(true);
    >
    > new Thread(this).start();
    > }
    >
    > public void run() {
    > while (true) {
    > ++n;
    > n %= 2;
    > Runnable r = new Runnable() {
    > public void run() {
    > setContentPane(panels[n]);
    > validate();
    > }
    > };
    > EventQueue.invokeLater(r);
    >
    > // setContentPane(panels[n]);
    >
    > try {
    > Thread.sleep(2000);
    > } catch (InterruptedException ie) { }
    > }
    > }
    >
    > class KPanel extends JPanel {
    > final private Color color;
    >
    > public KPanel(Color color) {
    > this.color = color;
    >
    > setLayout(new GridBagLayout());
    > GridBagConstraints c = new GridBagConstraints();
    > c.gridx = c.gridy = 0; c.insets = new Insets(2,2,2,2);
    >
    > JButton[] b = new JButton[4];
    > for (int i=0; i<b.length; i++)
    > b = new JButton("Button " + Integer.toString(i+1));
    >
    > add(b[0],c);
    > ++c.gridx;
    > add(b[1],c);
    > c.gridx = 0; ++c.gridy;
    > add(b[2],c);
    > ++c.gridx;
    > add(b[3],c);
    > }
    >
    > public void paintComponent(Graphics g) {
    > g.setColor(color);
    > g.fillRect(0,0,getWidth(),getHeight());
    > }
    > }
    >
    > public static void main(String[] args) {
    > Runnable r = new Runnable() {
    > public void run() {
    > new test6();
    > }
    > };
    > EventQueue.invokeLater(r);
    > }
    > }
    >


    My problem was that I couldn't change the contents. Filling it
    initially worked fine.
    A Watcher, Sep 8, 2007
    #5
  6. A Watcher wrote:
    >
    > My problem was that I couldn't change the contents. Filling it
    > initially worked fine.
    >


    So do you have it working now?

    Just for my curiosity why do you change the content pane rather than
    adding or removing components or containers?

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Sep 8, 2007
    #6
  7. A Watcher

    A Watcher Guest

    Knute Johnson wrote:
    > A Watcher wrote:
    >>
    >> My problem was that I couldn't change the contents. Filling it
    >> initially worked fine.
    >>

    >
    > So do you have it working now?
    >
    > Just for my curiosity why do you change the content pane rather than
    > adding or removing components or containers?
    >


    I'm not an expert at Java yet. Can you point me to some examples of
    doing that?
    A Watcher, Sep 8, 2007
    #7
  8. A Watcher wrote:
    > Knute Johnson wrote:
    >> A Watcher wrote:
    >>>
    >>> My problem was that I couldn't change the contents. Filling it
    >>> initially worked fine.
    >>>

    >>
    >> So do you have it working now?
    >>
    >> Just for my curiosity why do you change the content pane rather than
    >> adding or removing components or containers?
    >>

    >
    > I'm not an expert at Java yet. Can you point me to some examples of
    > doing that?
    >

    All AWT and Swing classes are subclasses of Component and JComponent
    respectively. These are the classes that implement the setVisible()
    method, so one way of controlling the GUI seen by the user is to build
    all the window layouts you need during initialization and use
    setVisible() to control which objects the user sees at various points
    during program execution.

    That will alter the GUI layout, which is what I'm guessing that you want
    to do.

    If all you want is to change the displayed content of a GUI component
    then you'll find that there is usually a method that simply changes the
    data to be displayed, e.g. JTextField.setText() or combination of
    methods, e.g. to change a JTable's contents you change the content of
    its associated TableModel and then notify JTable of the change by
    calling a method such as TabelModel.fireTableDataChanged().


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
    Martin Gregorie, Sep 9, 2007
    #8
  9. A Watcher

    Roedy Green Guest

    On Fri, 07 Sep 2007 17:24:07 -0700, A Watcher
    <> wrote, quoted or indirectly quoted someone
    who said :

    >I use the setContentPane method to put a JPanel into a JFrame and then
    >call validate and repaint. That works fine. What I can't do is
    >change it. I rerun the setContentPane method with a different JPanel
    >and do the validate and repaint but nothing happens. I am using java
    >5. Can someone suggest something I could try?


    Normally you ADD JPanels to a JFrame. Setting the contentPane is
    meddling with the dark arts.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Sep 12, 2007
    #9
  10. A Watcher wrote:
    > Knute Johnson wrote:
    >> A Watcher wrote:
    >>>
    >>> My problem was that I couldn't change the contents. Filling it
    >>> initially worked fine.
    >>>

    >>
    >> So do you have it working now?
    >>
    >> Just for my curiosity why do you change the content pane rather than
    >> adding or removing components or containers?
    >>

    >
    > I'm not an expert at Java yet. Can you point me to some examples of
    > doing that?


    Here is a very simple example of adding and removing components and also
    making a component visible or not. Different layout managers will
    respond differently to having components added or removed but if you
    call validate, the layout manager will layout the components again.

    Often one will set components as enabled or disabled to indicate to a
    user that a particular feature is or is not available. This is often
    done with menus.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class test3 extends JPanel {
    JLabel north,center;

    public test3() {
    setLayout(new BorderLayout());

    center = new JLabel("Center",JLabel.CENTER);
    add(center,BorderLayout.CENTER);

    north = new JLabel("North",JLabel.CENTER);
    add(north,BorderLayout.NORTH);

    JButton south = new JButton("Toggle Center");
    south.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    if (center.isShowing())
    center.setVisible(false);
    else
    center.setVisible(true);
    }
    });
    add(south,BorderLayout.SOUTH);

    JButton west = new JButton("Remove North");
    west.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    if (north.isShowing()) {
    remove(north);
    validate();
    }
    }
    });
    add(west,BorderLayout.WEST);

    JButton east = new JButton("Add North");
    east.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    if (!north.isShowing()) {
    add(north,BorderLayout.NORTH);
    validate();
    }
    }
    });
    add(east,BorderLayout.EAST);
    }

    public static void main(String[] args) {
    Runnable r = new Runnable() {
    public void run() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    test3 t3 = new test3();
    f.add(t3);
    f.pack();
    f.setVisible(true);
    }
    };
    EventQueue.invokeLater(r);
    }
    }

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Sep 13, 2007
    #10
  11. A Watcher

    Roedy Green Guest

    On Wed, 12 Sep 2007 15:54:45 GMT, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >Normally you ADD JPanels to a JFrame. Setting the contentPane is
    >meddling with the dark arts.


    see http://mindprod.com/jgloss/jframe.html for sample code.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Sep 13, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    502
    Chris Uppal
    May 5, 2005
  2. KK
    Replies:
    2
    Views:
    545
    Big Brian
    Oct 14, 2003
  3. MuZZy
    Replies:
    7
    Views:
    1,740
    Mike Hewson
    Jan 7, 2005
  4. jakester
    Replies:
    3
    Views:
    1,821
    jakester
    Apr 3, 2007
  5. Replies:
    2
    Views:
    409
    markspace
    Aug 27, 2012
Loading...

Share This Page