Multiple Panels

Discussion in 'Java' started by michael.miceli88@gmail.com, Apr 21, 2008.

  1. Guest

    Hi,
    I am programming a GUI for a Java program I wrote, and have a
    question. I have one Frame with some buttons and labels on them, but
    when I am done I want the user to be able to hit next and get to more
    information. How do programs do this in general. I could create a
    new panel and hide the other one, but is this the best solution?

    Thanks
    Michael
     
    , Apr 21, 2008
    #1
    1. Advertising

  2. wrote:
    > Hi,
    > I am programming a GUI for a Java program I wrote, and have a
    > question. I have one Frame with some buttons and labels on them, but
    > when I am done I want the user to be able to hit next and get to more
    > information. How do programs do this in general. I could create a
    > new panel and hide the other one, but is this the best solution?
    >
    > Thanks
    > Michael


    When you press the "Next" button, remove the panel with the buttons and
    labels and add the panel with the other information. Remember to call
    validate() on the Frame after you add your new panel.

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

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

    final JPanel p2 = new JPanel();
    p2.add(new JLabel("Info Panel"));

    final JPanel p1 = new JPanel(new BorderLayout());
    p1.add(new JLabel("Label"),BorderLayout.NORTH);
    JButton b = new JButton("Next");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    f.remove(p1);
    f.add(p2,BorderLayout.CENTER);
    f.validate();
    }
    });
    p1.add(b,BorderLayout.SOUTH);

    f.add(p1,BorderLayout.CENTER);

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

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
     
    Knute Johnson, Apr 21, 2008
    #2
    1. Advertising

  3. Mark Space Guest

    wrote:
    > Hi,
    > I am programming a GUI for a Java program I wrote, and have a
    > question. I have one Frame with some buttons and labels on them, but
    > when I am done I want the user to be able to hit next and get to more
    > information. How do programs do this in general. I could create a
    > new panel and hide the other one, but is this the best solution?
    >
    > Thanks
    > Michael


    Besides Knute's excellent demonstration, you should also look at the
    CardLayout, which lets you switch panels some what automagically.

    <http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html>
     
    Mark Space, Apr 21, 2008
    #3
  4. Guest

    On Apr 21, 1:35 am, Knute Johnson <>
    wrote:
    > wrote:
    > > Hi,
    > > I am programming a GUI for a Java program I wrote, and have a
    > > question. I have one Frame with some buttons and labels on them, but
    > > when I am done I want the user to be able to hit next and get to more
    > > information. How do programs do this in general. I could create a
    > > new panel and hide the other one, but is this the best solution?

    >
    > > Thanks
    > > Michael

    >
    > When you press the "Next" button, remove the panel with the buttons and
    > labels and add the panel with the other information. Remember to call
    > validate() on the Frame after you add your new panel.
    >
    > import java.awt.*;
    > import java.awt.event.*;
    > import javax.swing.*;
    >
    > public class test {
    > public static void main(String[] args) {
    > EventQueue.invokeLater(new Runnable() {
    > public void run() {
    > final JFrame f = new JFrame();
    > f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    >
    > final JPanel p2 = new JPanel();
    > p2.add(new JLabel("Info Panel"));
    >
    > final JPanel p1 = new JPanel(new BorderLayout());
    > p1.add(new JLabel("Label"),BorderLayout.NORTH);
    > JButton b = new JButton("Next");
    > b.addActionListener(new ActionListener() {
    > public void actionPerformed(ActionEvent ae) {
    > f.remove(p1);
    > f.add(p2,BorderLayout.CENTER);
    > f.validate();
    > }
    > });
    > p1.add(b,BorderLayout.SOUTH);
    >
    > f.add(p1,BorderLayout.CENTER);
    >
    > f.pack();
    > f.setVisible(true);
    > }
    > });
    > }
    >
    > }
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/linux/
    >
    > --
    > Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    > ------->>>>>>http://www.NewsDem


    Thanks a lot :-D
     
    , Apr 21, 2008
    #4
  5. Mark Space wrote:
    > wrote:
    >> Hi,
    >> I am programming a GUI for a Java program I wrote, and have a
    >> question. I have one Frame with some buttons and labels on them, but
    >> when I am done I want the user to be able to hit next and get to more
    >> information. How do programs do this in general. I could create a
    >> new panel and hide the other one, but is this the best solution?
    >>
    >> Thanks
    >> Michael

    >
    > Besides Knute's excellent demonstration, you should also look at the
    > CardLayout, which lets you switch panels some what automagically.
    >
    > <http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html>

    hi
    a good thing about CardLayout is you can implement a "back" button and
    you'll have all the inputs that the user made
     
    CHAFIK Wassime, Apr 23, 2008
    #5
  6. CHAFIK Wassime wrote:
    > Mark Space wrote:
    >> wrote:
    >>> Hi,
    >>> I am programming a GUI for a Java program I wrote, and have a
    >>> question. I have one Frame with some buttons and labels on them, but
    >>> when I am done I want the user to be able to hit next and get to more
    >>> information. How do programs do this in general. I could create a
    >>> new panel and hide the other one, but is this the best solution?
    >>>
    >>> Thanks
    >>> Michael

    >>
    >> Besides Knute's excellent demonstration, you should also look at the
    >> CardLayout, which lets you switch panels some what automagically.
    >>
    >> <http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html>

    > hi
    > a good thing about CardLayout is you can implement a "back" button and
    > you'll have all the inputs that the user made


    CardLayout is really slick but there are some interesting issues that
    can come up. Note in the rewritten example below, I set the layout
    manager on the JFrame to a CardLayout. JFrame.setLayout() is overridden
    in version 1.5+ to forward this call to the content pane. But in the
    CardLayout.next() and .previous calls, I have to specify the content
    pane of the JFrame as the container not just the JFrame. There are some
    significant advantages to using CardLayout. When the "cards" are added
    and the container packed, the container will be sized to fit the largest
    of the "cards". Random access to the cards is very simple too with the
    show() and the constraints object. Check out the docs for description
    of the API but look around the net for examples of how to use CardLayout.

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

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

    final CardLayout cl = new CardLayout();
    f.setLayout(cl);

    final JPanel p1 = new JPanel(new BorderLayout());
    final JPanel p2 = new JPanel(new BorderLayout());

    p1.add(new JLabel(
    "Labels and Buttons"),BorderLayout.NORTH);
    JButton b = new JButton("Next");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    cl.next(f.getContentPane());
    }
    });
    p1.add(b,BorderLayout.SOUTH);
    f.add(p1,"First");

    p2.add(new JLabel("Info Panel"),BorderLayout.NORTH);
    b = new JButton("Previous");
    b.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae) {
    // cl.previous(f.getContentPane());
    cl.show(f.getContentPane(),"First");
    }
    });
    p2.add(b,BorderLayout.SOUTH);
    f.add(p2,"Second");

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

    --

    Knute Johnson
    email s/nospam/linux/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDem
     
    Knute Johnson, Apr 23, 2008
    #6
    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. Chris Nunciato

    Problem with Validation Using Panels

    Chris Nunciato, Jul 1, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    326
    Chris Nunciato
    Jul 1, 2003
  2. Jonas
    Replies:
    3
    Views:
    483
    Jonas
    Jan 26, 2005
  3. flupke
    Replies:
    1
    Views:
    4,514
    enoola
    Feb 9, 2009
  4. Brybot

    Validate multiple Panels

    Brybot, Jul 11, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    470
    Brybot
    Jul 11, 2007
  5. Jonas

    Page validation with multiple panels

    Jonas, Jan 25, 2005, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    143
    Jonas
    Jan 26, 2005
Loading...

Share This Page