1

I have a problem with frames. I think my problem is logic one. I open menu frame and then when I click add passenger the frame doesn't open wel. I still can see previous frame. I used ideas posted on this forum to change setVisisble to (false) and I thought I will see correctly other frame but it didn't work well. Could you please give me some advice. Thank you

public class DriverProgram 
{ 
     public static void main (String [ ] argument) 
      { 
          Flight sw101 = new Flight(); 
          sw101.setVisible(true);
      } 
 } 

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

public class Flight extends JFrame
{
SeatingChart sc = new SeatingChart();
WaitingList wl = new WaitingList();
private String buttons[] = new String[5];
private final Font MENU_FONT = new Font("Serif", Font.BOLD, 20);
JButton b;
ActionListener listener1 = new MenuClickListener();
private JTextField input =null;  
private JTextField firstName;
private JTextField middleName;
private JTextField lastName; 
private final Font NEW_FONT = new Font("Serif", Font.BOLD + Font.ITALIC, 20);

public Flight()
{
    menu();
}

void menu( )
{ 
    uiFrameMenu();
    addPanelMenu();
}

void uiFrameMenu()
{
    setTitle( "MENU" );
    setSize( 400, 400 );
    setLocationRelativeTo( null );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
}

void addPanelMenu()
{
    setLayout( new BorderLayout() ); 
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout (5,1));

    buttons[0] = "Add Passenger";
    buttons[1] = "Remove Passenger";
    buttons[2] = "Seating Chart";
    buttons[3] = "Waiting List";
    buttons[4] = "Exit";


    for( int i = 0; i < buttons.length ; i++ )
    {
        b = new JButton( buttons[i]);
        b.setText( buttons[i] );
        b.setForeground(Color.BLACK);
        b.setFont(MENU_FONT);
        b.addActionListener(listener1);
        panel.add(b);
    }

    getContentPane();
    panel.setBackground(Color.MAGENTA);
    add(panel,"Center");
}

class MenuClickListener implements ActionListener
{
    MenuClickListener() { }

    @Override
    public void actionPerformed( ActionEvent event ) 
    {   
        if ( event.getActionCommand().equals(buttons[0]))   
        { 
            addPassenger();
        }
        else if ( event.getActionCommand().equals(buttons[1]))  
        { 

        }
        setVisible(true);
             }
}



void addPassenger( )
{
    this.setEnabled(false);
    uiFrameData();
    setLayout( new BorderLayout() ); 
    JPanel panel1 = new JPanel();
    panel1.setLayout( new GridLayout(3, 2) );
    JPanel panel2 = new JPanel();
    panel2.setLayout( new GridLayout(3, 1) );
    JLabel l1 = new JLabel("First Name");
    JLabel l2 = new JLabel("Middle Name");
    JLabel l3 = new JLabel("Last Name");
    firstName = new JTextField();
    middleName = new JTextField();
    lastName = new JTextField();
    panel1.add(l1);
    panel1.add(firstName);
    panel1.add(l2);
    panel1.add(middleName);
    panel1.add(l3);
    panel1.add(lastName);
    add(panel1);
}

void uiFrameData()
{
    setTitle( "FIRST, MIDDLE AND LAST NAME" );
    setSize( 400, 400 );
    //setLocationRelativeTo( null );
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
}
}
dbrin
  • 15,525
  • 4
  • 56
  • 83
user1282256
  • 183
  • 1
  • 6
  • 16
  • Please define and describe better "doesn't open *well*" and "it didn't work *well*". – Hovercraft Full Of Eels Mar 21 '12 at 02:40
  • Edit: I think I see your problem: you want to use a CardLayout. – Hovercraft Full Of Eels Mar 21 '12 at 02:44
  • doesn't open well" and "it didn't work well". It means that it opens first frame menu and then when I choose add passenger I still can see some part of the first frame and I don't see correctly second frame. – user1282256 Mar 21 '12 at 02:52
  • 1
    Again, look into using a CardLayout. The tutorial can be found [here](http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html). This will allow you to create and swap several JPanels, perhaps named menuPanel, addPassengerPanel,, removePassengerPanel, seatingChartPanel, and waitingListPanel. – Hovercraft Full Of Eels Mar 21 '12 at 03:00
  • 2
    crosspost: [java-forums.org: cant-close-previous-frame-see-well-new-frame-java](http://www.java-forums.org/new-java/57046-cant-close-previous-frame-see-well-new-frame-java.html) – Hovercraft Full Of Eels Mar 21 '12 at 03:02
  • 1
    See also [Multiple frames: good or bad practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Mar 21 '12 at 06:11

1 Answers1

3

Better apply CardLayout, for this scenario, as already told to you. Here I had modified your code, you are setting the Layout on your JFrame everytime, since it's the same Layout you are setting, why not do that only once. Why do add extra code everytime for no reasons :-) , Do revalidate() and repaint() on your JFrame after making changes to the GUI on the fly, so that they can be reciprocated to the viewer. Here I had made some change to the code.

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

public class DriverProgram 
{ 
     public static void main (String [ ] argument) 
      { 
          SwingUtilities.invokeLater(new Runnable()
          {
                public void run()
                {
                    Flight sw101 = new Flight(); 
                    sw101.setVisible(true);
                }
          });
      } 
 }

class Flight extends JFrame
{
    //SeatingChart sc = new SeatingChart();
    //WaitingList wl = new WaitingList();
    private String buttons[] = new String[5];
    private final Font MENU_FONT = new Font("Serif", Font.BOLD, 20);
    JButton b;
    ActionListener listener1 = new MenuClickListener();
    private JPanel panel; // changed this to instance field so it can be accessed, at button click.
    private JTextField input =null;  
    private JTextField firstName;
    private JTextField middleName;
    private JTextField lastName; 
    private final Font NEW_FONT = new Font("Serif", Font.BOLD + Font.ITALIC, 20);

    public Flight()
    {
        menu();
    }

    void menu( )
    { 
        uiFrameMenu();
        addPanelMenu();
    }

    void uiFrameMenu()
    {
        setTitle( "MENU" );
        setSize( 400, 400 );
        setLocationRelativeTo( null );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );   
    }

    void addPanelMenu()
    {
        setLayout( new BorderLayout() ); 
        panel = new JPanel();
        panel.setLayout(new GridLayout (5,1));

        buttons[0] = "Add Passenger";
        buttons[1] = "Remove Passenger";
        buttons[2] = "Seating Chart";
        buttons[3] = "Waiting List";
        buttons[4] = "Exit";


        for( int i = 0; i < buttons.length ; i++ )
        {
            b = new JButton( buttons[i]);
            b.setText( buttons[i] );
            b.setForeground(Color.BLACK);
            b.setFont(MENU_FONT);
            b.addActionListener(listener1);
            panel.add(b);
        }

        getContentPane();
        panel.setBackground(Color.MAGENTA);
        add(panel,"Center");
    }

    class MenuClickListener implements ActionListener
    {
        MenuClickListener() { }

        @Override
        public void actionPerformed( ActionEvent event ) 
        {   
            if ( event.getActionCommand().equals(buttons[0]))   
            { 
                addPassenger();
            }
            else if ( event.getActionCommand().equals(buttons[1]))  
            { 

            }       
        }
    }



    void addPassenger( )
    {
        //this.setEnabled(false);
        remove(panel);
        uiFrameData();
        JPanel panel1 = new JPanel();
        panel1.setLayout( new GridLayout(3, 2) );
        JPanel panel2 = new JPanel();
        panel2.setLayout( new GridLayout(3, 1) );
        JLabel l1 = new JLabel("First Name");
        JLabel l2 = new JLabel("Middle Name");
        JLabel l3 = new JLabel("Last Name");
        firstName = new JTextField();
        middleName = new JTextField();
        lastName = new JTextField();
        panel1.add(l1);
        panel1.add(firstName);
        panel1.add(l2);
        panel1.add(middleName);
        panel1.add(l3);
        panel1.add(lastName);
        add(panel1);

        revalidate(); // If your JDK is 1.7 else use getContentPane().revalidate(); for JDK 1.6 or below
        repaint();
    }

    void uiFrameData()
    {
        setTitle( "FIRST, MIDDLE AND LAST NAME" );
        setSize( 400, 400 ); 
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143