Library tutorials & articles

Programming with Swing

JFrame and JPanel

JFrame

Remember to import these three packages when working with Swing:

javax.swing.*  
java.awt.*      
java.awt.event.*

A JFrame object is the physical window that you'll be working with in the Swing API. Making a window appear on the screen is easy enough. Here's the code:

JFrame frame = new JFrame("Title Bar Text");        
frame.setSize(256,256);                            
frame.pack();                                      
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.show();                                      

I think that code is relatively simple, all it does is create a new JFrame with a size of 256x256, tell Java to exit when the JFrame is closed and shows the window.

JPanel

You probably want to get started putting components on your brand-new form. Maybe you've even figured out how to and you've discovered that is not what you bargained for. Well, what you don't know is that you need to add a JPanel to the JFrame's contentpane before adding components to the JPanel. Here's what the code looks like:

JFrame frame = new JFrame("Title Bar Text");
JPanel panel = new JPanel();
frame.setSize(256,256);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel);
frame.pack();
frame.show();

Now we are getting somewhere... You probably have already ran this code, so you've discovered that it doesn't look any different than the last example, and that's because JPanels are just containers for components, the different types of panes are added to the JFrame's contentpane and then components are added to the JPanel. Other than Layout objects which I tell you about later, that's all there is for your basic JPanel.

Comments

  1. 14 Nov 2005 at 15:01

    Hello,
     I want to add a closing button to the internal frame. I am not able to do it. Plz help.javascript:smilie('')
    confused

  2. 28 Oct 2004 at 07:05

    Hi.


    I have an ChildFrame inside of an DesktopFrame. I want to insert a JButton into the ChildFrame. But this JButton turns out to be very  tiny in that child frame. If I maximize the child frame, the button does not change size.
    I used this a source for my codings:


    http://www.rz.fhtw-berlin.de/hjp3/k100230.html


    I only added a JButton into my chid frame, I used a GridBagLayout. button.setSize() does  not seem to work in  that child frame.


    Would be very thankful, if anybody could help.


    Burkhardt


  3. 13 Sep 2004 at 04:21

    jho....


    just call your child frames as follows


    public class MainFrame extends JFrame
    {
     ChildFrame1 child1;
    public MainFrame()
    {
     final JDesktopPane desktop = new JDesktopPane();
     getContentPane().add(desktop);
     
     JMenuBar bar = new JMenuBar();
    JMenu show = new JMenu("Show");
    JMenuItem showchild1 = new JMenuItem("Show Child1");
    //*******
    // here this represents the MainFrame which can be passes as a message to the child frame.
             child1 = new ChildFrame1(this);
    //
    *******
    desktop.add(child1);
    child1.setVisible(true);
     
    bar.add(show);
    show.add(showchild1);
    }
    }



    and in ChildFrame set the constructor as


    MainFrame  mainFrame;
    ChildFrame(MainFrame  mainFrame)
    {
    this.mainFrame = mainFrame;
    }


    actionPerformed in ChildFrame1()  {
     if(button clicked) {
        ChilFrame2 childFrame2 = new ChildFrame2();
         mainFrame.desktop.add(childFrame2);
         this.dispose(); // or this.hide();
     }
    }


    hope this code helps!


  4. 05 Aug 2004 at 01:44

    i have a JFrame named MainFrame which also uses JDesktoPane. I created 2 JInternalFrame class named ChildFrame1 and ChildFrame2. When I can click the menu on the mainframe, it shows the childframe1. I must load the Childframe2 on the desktopPane on the mainFrame by clicking a button inside the childframe1. How can I do this??? My problem is that the desktopPane becomes foreign to childFrame2 since it is another class. here is the sample prog...


    public class MainFrame extends JFrame
    {
      ChildFrame1 child1 = new ChildFrame1();
    public MainFrame()
    {
      final JDesktopPane desktop = new JDesktopPane();
      getContentPane().add(desktop);
     
      JMenuBar bar = new JMenuBar();
     JMenu show = new JMenu("Show");
     JMenuItem showchild1 = new JMenuItem("Show Child1");
     desktop.add(child1);
     child1.setVisible(true);
     
    bar.add(show);
    show.add(showchild1);



    }


    }


    i still have another JInternalFrame class named childFrame2 but it will only be visible when i click a button in childFrame1 (child1).


    how can i do this?????


    i cannot use the jDESKTOPANE "desktop" because that class does not recognize the "desktop"



    PLEASE HELP ME!!! I WILL DEFINITELY APPRECIATE YOUR HELP! THANKS A LOT!!!

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Programming with Swing.

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • Java Posse #218 - Newscast for Nov 21st 2008

    Newscast for Nov 21st 2008Fully formatted shownotes can always be found at http://javaposse.comDick and Carl at Devoxx - http://www.devoxx.com/display/JV08/Home Dick at the University of Kent - http://www.cs.kent.ac.uk/coe/Questions? Feedback? Try our new moderator site: http://tinyurl.com/q4java...

Related jobs

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!