Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replacing an existing jPanel with a new jPanel

2,582 views
Skip to first unread message

Brian

unread,
Mar 11, 2008, 12:23:49 AM3/11/08
to
Hi everybody,

I'm working on a project and basically I've developed a swing GUI
using NetBeans Swing GUI builder. On one form, I have an existing
JPanel that contains no other components. Also on this form, I have a
button that when pressed executes a function of another Class that
creates a graph (using jFreeChart) and returns the graph on a JPanel.
My question is: Is it possible to replace the existing JPanel on the
aforementioned form with the JPanel returned from the graphing method
such that I can dynamically place the created graph on the form?

I'm not sure exactly how to go about doing this. I know that I can
take the JPanel containing the graph and show it on another frame by
creating and new frame and showing it. However my desire is to have
the graph be part of the existing form in the location where the
existing JPanel lies.

Any help would be very much appreciated.

Thanks.

Brian

Andrew Thompson

unread,
Mar 11, 2008, 12:51:38 AM3/11/08
to
On Mar 11, 3:23 pm, Brian <bce...@gmail.com> wrote:

> Sub: Replacing an existing jPanel with a new jPanel

There is no such class in the J2SE as jPanel.
Did you mean JPanel?

...


> I'm working on a project and basically I've developed a swing GUI
> using NetBeans Swing GUI builder.

That does not bode well. GUI builders
generally produce crap code unless the
person using them already understands
component layout. *

>.. On one form,

What is a 'form'? Please quote actual class names.

>...I have an existing


> JPanel that contains no other components.  Also on this form, I have a
> button that when pressed executes a function of another Class that
> creates a graph (using jFreeChart) and returns the graph on a JPanel.
> My question is:  Is it possible to replace the existing JPanel on the
> aforementioned form with the JPanel returned from the graphing method
> such that I can dynamically place the created graph on the form?

Sure.
- A CardLayout can contain both components
and flip between them.
- The code might remove the original JPanel,
add the new JPanel, then validate() the JFrame.
- If it makes any sense, you might also use a
JTabbedPane and add the new JPanel to a new tab.

* Start here..
<http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html>

--
Andrew T.
PhySci.org

Knute Johnson

unread,
Mar 11, 2008, 12:55:43 AM3/11/08
to

You've got a couple of options. You can remove the first JPanel and add
the second or you could just add the second to the first.

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

class test7 {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JPanel p1 = new JPanel();
p1.setBackground(Color.BLUE);
p1.setPreferredSize(new Dimension(640,480));
p1.setLayout(new GridBagLayout());
f.add(p1,BorderLayout.CENTER);

final JPanel p2 = new JPanel();
p2.setBackground(Color.GREEN);
p2.setPreferredSize(new Dimension(400,300));

JButton b = new JButton("Add Green Panel");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
p1.add(p2);
p1.validate();
p1.repaint();
}
});
f.add(b,BorderLayout.NORTH);

b = new JButton("Remove Green Panel");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
p1.remove(p2);
p1.validate();
p1.repaint();
}
});
f.add(b,BorderLayout.SOUTH);

f.pack();
f.setVisible(true);
}
});
}
}

--

Knute Johnson
email s/nospam/linux/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

0 new messages