Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2008

Tip: Looking for answers? Try searching our database.

Replacing an existing jPanel with a new jPanel

Thread view: 
Brian - 11 Mar 2008 05:23 GMT
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 - 11 Mar 2008 05:51 GMT
> 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
[quoted text clipped - 3 lines]
> 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 - 11 Mar 2008 05:55 GMT
> Hi everybody,
>
[quoted text clipped - 18 lines]
>
> Brian

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);
            }
        });
    }
}

Signature

Knute Johnson
email s/nospam/linux/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.