JFrame help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Caution
    New Member
    • Oct 2006
    • 8

    #1

    JFrame help

    Ok.. bare with me, not sure how to word this.

    I am writing a program that stores airplane data. My problem right now is trying to figure out how to get the navigation to work.

    My program loads my "menu page". All I want to do is figure out how to load different content in the same container when I click a button.

    For instance, right now I am working on just getting displaySeating( ) to work. I have a button on my "menu page" that I want to clear all the menu buttons and show a "new page" that has seating information.

    I've tried to use removeAll() to remove all the components, but I haven't gotten it to work.

    Here is my code so far... it isn't close to finished, but here it is so far.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Airplane extends JFrame {
    	private static final int WIDTH = 400;
    	private static final int HEIGHT = 300;
    	
    	private JLabel menu, selOption, seatingMenu;
    	private JRadioButton newPlane, displaySeating, assignSeat, cancelSeat, removeSeats, exit;
    	private JButton submit, back;
    	private ButtonGroup menuOptions;
    	private SubmitButtonHandler subHandler;
    	private boolean npTF, dsTF, asTF, csTF, rsTF, eTF;
    	private JTextArea seatingMenuTA;
    
    	public Airplane(){
    	
    		Container c = getContentPane();
    		c.setLayout(null);
    
    		setTitle("");
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    	}
    
    	public void mainMenu(){
    		Container c = getContentPane();
    		c.setLayout(null);
    
    		//JLabels
    		menu = new JLabel("Main menu");
    		selOption = new JLabel("Select an option");
    		
    		//JRadioButtons
    		newPlane = new JRadioButton("Get new Plane");
    		displaySeating = new JRadioButton("Display seating");
    		assignSeat = new JRadioButton("Assign a seat");
    		cancelSeat = new JRadioButton("Cancel a seat");
    		removeSeats = new JRadioButton("Remove all seating");
    		exit = new JRadioButton("Exit program");
    		
    		//JButtons
    		submit = new JButton("Submit");
    		subHandler = new SubmitButtonHandler();
    		submit.addActionListener(subHandler);
    		
    		//set size
    		newPlane.setSize(150, 30);	
    		displaySeating.setSize(150, 30);	
    		assignSeat.setSize(150, 30);	
    		cancelSeat.setSize(150, 30);	
    		removeSeats.setSize(150, 30);	
    		exit.setSize(150, 30);
    		menu.setSize(150, 30);	
    		selOption.setSize(150,30);
    		submit.setSize(150, 30);
    		
    		//set location
    		newPlane.setLocation(100, 30);	
    		displaySeating.setLocation(100, 60);	
    		assignSeat.setLocation(100, 90);	
    		cancelSeat.setLocation(100, 120);	
    		removeSeats.setLocation(100, 150);	
    		exit.setLocation(100, 180);
    		menu.setLocation(0, 0);	
    		selOption.setLocation(0,30);
    		submit.setLocation(100, 210);
    		
    		//add graphic objects
    		c.add(newPlane);
    		c.add(displaySeating);
    		c.add(assignSeat);
    		c.add(cancelSeat);
    		c.add(removeSeats);
    		c.add(exit);
    		c.add(menu);
    		c.add(selOption);
    		c.add(submit);
    	
    		//button group
    		menuOptions = new ButtonGroup();
    		menuOptions.add(newPlane);
    		menuOptions.add(displaySeating);
    		menuOptions.add(assignSeat);
    		menuOptions.add(cancelSeat);
    		menuOptions.add(removeSeats);
    		menuOptions.add(exit);
    		
    		setTitle("Main Menu");
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    	}
    	
    	public void displaySeatingMenu(){
    	
    		Container c = getContentPane();
    		c.setLayout(null);
    		
    		seatingMenu = new JLabel("Seating Chart");
    		seatingMenu.setSize(150, 30);
    		seatingMenu.setLocation(150, 0);
    		c.add(seatingMenu);
    		
    		back = new JButton("Back to Menu");
    		back.setSize(150, 30);
    		back.setLocation(120, 210);
    		c.add(back);
    		
    		seatingMenuTA = new JTextArea(40, 10);
    		seatingMenuTA.setSize(150,150);
    		seatingMenuTA.setLocation(100, 30);
    		//seatingMenuTA.append("\n\n\n\n\n\n\n\n\n\n");
    			
    		
    		c.add(seatingMenuTA);
    		
    		setTitle("Seating Chart");
    		setSize(WIDTH, HEIGHT);
    		setVisible(true);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    	}//end displaySeating
    
    	
    	
    	private class SubmitButtonHandler implements ActionListener {
    		public void actionPerformed(ActionEvent e){
    			
    			if(npTF == true){
    			
    			}
    			if(asTF == true){
    			
    			}
    			if(dsTF == true){
    				
    				
    			}
    			if(csTF == true){
    			
    			}
    			if(rsTF == true){
    			
    			}
    			if(eTF == true){
    			
    			}
    			else{
    			}
    		}//end actionPerformed
    	}//end SubmitButtonHandler
    
    	public void itemStateChanged(ItemEvent e){
    		if(e.getSource() == newPlane){
    			npTF = true; dsTF = false; asTF = false; csTF = false; rsTF = false; eTF = false;
    		}
    		if(e.getSource() == displaySeating){
    		npTF = false; dsTF = true; asTF = false; csTF = false; rsTF = false; eTF = false; 
    		}
    		if(e.getSource() == assignSeat){
    			npTF = false; dsTF = false; asTF = true; csTF = false; rsTF = false; eTF = false;
    		}
    		if(e.getSource() == cancelSeat){
    			npTF = false; dsTF = false; asTF = false; csTF = true; rsTF = false; eTF = false;
    		}
    		if(e.getSource() == removeSeats){
    			npTF = false; dsTF = false; asTF = false; csTF = false; rsTF = true; eTF = false;
    		}
    		if(e.getSource() == exit){
    			npTF = false; dsTF = false; asTF = false; csTF = false; rsTF = false; eTF = true;
    		}
    	}//end itemStateChanged
    
    	public static void main(String[] args){
    		char[][] seating = new char[7][8];
    		
    		Airplane plane = new Airplane(); 
    		plane.mainMenu();
    		
    	}//end main
    }//end Airplane
  • Caution
    New Member
    • Oct 2006
    • 8

    #2
    not sure if I was clear enough, really frustrated right now, let me know if I wasn't clear :\

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by Caution
      not sure if I was clear enough, really frustrated right now, let me know if I wasn't clear :\
      I have to go now maybe someone else will help you out but I would advise you to use JPanels for laying out things

      Comment

      • Caution
        New Member
        • Oct 2006
        • 8

        #4
        I haven't learned anything about JPanels yet :\

        I just converted all of the JRadioButtons into JButtons. The button I used to test bringing up the second window works, but it isn't removing any of the previous content in the container.

        I used getContentPane( ).removeAll(); then called the method to load the next screen. It doesn;t look like removeAll() did anything...

        Comment

        • Caution
          New Member
          • Oct 2006
          • 8

          #5
          when I scroll over the container, the new buttons then overlap the old ones... I tried removeAll and removing them one by one, not working...

          Comment

          • DeMan
            Top Contributor
            • Nov 2006
            • 1799

            #6
            There is loads of online information about JPanels, if you are really interested (just search for jPanel)

            Basically, JPanel has a constructor that accepts a LayoutManager (of which you have several choices, but are generally easy to use). The LayoutManager defines how (or probably more accurately where) things are displayed on the screen. I think (And I'm sure someone will correct me if I'm wrong), that the most layout managers can work with %ages (like html can), so that size of objects can be relative and overlap isn't an issue. Check out the tute's you can find if you want exact detail.....

            Comment

            • Caution
              New Member
              • Oct 2006
              • 8

              #7
              I'm not sure how/if that's going to help me right now. I just need to understand why removeAll() isn't working.

              Here's where I am now, didn't change much, but the submit button at least calls displaySeatingM enu() correctly, the problem is components from mainMenu() don't get removed.
              Code:
              import javax.swing.*;
              import java.awt.*;
              import java.awt.event.*;
              
              public class Airplane extends JFrame {
              	private static final int WIDTH = 400;
              	private static final int HEIGHT = 300;
              	
              	private JLabel menu, selOption, seatingMenu;
              	private JButton submit, back, newPlane, displaySeating, assignSeat, cancelSeat, removeSeats, exit;
              	private ButtonGroup menuOptions;
              	private SubmitButtonHandler subHandler;
              	private boolean npTF, dsTF, asTF, csTF, rsTF, eTF;
              	private JTextArea seatingMenuTA;
              
              	public Airplane(){
              	
              		Container c = getContentPane();
              		c.setLayout(null);
              
              		setTitle("");
              		setSize(WIDTH, HEIGHT);
              		setVisible(true);
              		setDefaultCloseOperation(EXIT_ON_CLOSE);
              	}
              
              	public void mainMenu(){
              		Container c = getContentPane();
              		c.setLayout(null);
              
              		//JLabels
              		menu = new JLabel("Main menu");
              		selOption = new JLabel("Select an option");
              		
              		//JButtons
              		newPlane = new JButton("Get new Plane");
              		displaySeating = new JButton("Display seating");
              		assignSeat = new JButton("Assign a seat");
              		cancelSeat = new JButton("Cancel a seat");
              		removeSeats = new JButton("Remove all seating");
              		exit = new JButton("Exit program");
              		submit = new JButton("Submit");
              		subHandler = new SubmitButtonHandler();
              		submit.addActionListener(subHandler);
              		
              		//set size
              		newPlane.setSize(150, 30);	
              		displaySeating.setSize(150, 30);	
              		assignSeat.setSize(150, 30);	
              		cancelSeat.setSize(150, 30);	
              		removeSeats.setSize(150, 30);	
              		exit.setSize(150, 30);
              		menu.setSize(150, 30);	
              		selOption.setSize(150,30);
              		submit.setSize(150, 30);
              		
              		//set location
              		newPlane.setLocation(100, 30);	
              		displaySeating.setLocation(100, 60);	
              		assignSeat.setLocation(100, 90);	
              		cancelSeat.setLocation(100, 120);	
              		removeSeats.setLocation(100, 150);	
              		exit.setLocation(100, 180);
              		menu.setLocation(0, 0);	
              		selOption.setLocation(0,30);
              		submit.setLocation(100, 210);
              		
              		//add graphic objects
              		c.add(newPlane);
              		c.add(displaySeating);
              		c.add(assignSeat);
              		c.add(cancelSeat);
              		c.add(removeSeats);
              		c.add(exit);
              		c.add(menu);
              		c.add(selOption);
              		c.add(submit);
              		
              		setTitle("Main Menu");
              		setSize(WIDTH, HEIGHT);
              		setVisible(true);
              		setDefaultCloseOperation(EXIT_ON_CLOSE);
              		
              	}
              	
              	public void displaySeatingMenu(){
              	
              		Container c = getContentPane();
              		c.setLayout(null);
              		
              		seatingMenu = new JLabel("Seating Chart");
              		seatingMenu.setSize(150, 30);
              		seatingMenu.setLocation(150, 0);
              		c.add(seatingMenu);
              		
              		back = new JButton("Back to Menu");
              		back.setSize(150, 30);
              		back.setLocation(120, 210);
              		c.add(back);
              		
              		seatingMenuTA = new JTextArea(40, 10);
              		seatingMenuTA.setSize(150,150);
              		seatingMenuTA.setLocation(100, 30);
              		//seatingMenuTA.append("\n\n\n\n\n\n\n\n\n\n");
              			
              		
              		c.add(seatingMenuTA);
              		
              		setTitle("Seating Chart");
              		setSize(WIDTH, HEIGHT);
              		setVisible(true);
              		setDefaultCloseOperation(EXIT_ON_CLOSE);
              
              	}//end displaySeating
              
              	
              	
              	private class SubmitButtonHandler implements ActionListener {
              		public void actionPerformed(ActionEvent e){
              			getContentPane().removeAll();
              			displaySeatingMenu();
              		}//end actionPerformed
              	}//end SubmitButtonHandler
              
              
              	public static void main(String[] args){
              		char[][] seating = new char[7][8];
              		
              		Airplane plane = new Airplane(); 
              		plane.mainMenu();
              		
              	}//end main
              }//end Airplane

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by Caution
                I'm not sure how/if that's going to help me right now. I just need to understand why removeAll() isn't working.

                Here's where I am now, didn't change much, but the submit button at least calls displaySeatingM enu() correctly, the problem is components from mainMenu() don't get removed.
                Code:
                import javax.swing.*;
                import java.awt.*;
                import java.awt.event.*;
                 
                public class Airplane extends JFrame {
                	private static final int WIDTH = 400;
                	private static final int HEIGHT = 300;
                 
                	private JLabel menu, selOption, seatingMenu;
                	private JButton submit, back, newPlane, displaySeating, assignSeat, cancelSeat, removeSeats, exit;
                	private ButtonGroup menuOptions;
                	private SubmitButtonHandler subHandler;
                	private boolean npTF, dsTF, asTF, csTF, rsTF, eTF;
                	private JTextArea seatingMenuTA;
                 
                	public Airplane(){
                 
                		Container c = getContentPane();
                		c.setLayout(null);
                 
                		setTitle("");
                		setSize(WIDTH, HEIGHT);
                		setVisible(true);
                		setDefaultCloseOperation(EXIT_ON_CLOSE);
                	}
                 
                	public void mainMenu(){
                		Container c = getContentPane();
                		c.setLayout(null);
                 
                		//JLabels
                		menu = new JLabel("Main menu");
                		selOption = new JLabel("Select an option");
                 
                		//JButtons
                		newPlane = new JButton("Get new Plane");
                		displaySeating = new JButton("Display seating");
                		assignSeat = new JButton("Assign a seat");
                		cancelSeat = new JButton("Cancel a seat");
                		removeSeats = new JButton("Remove all seating");
                		exit = new JButton("Exit program");
                		submit = new JButton("Submit");
                		subHandler = new SubmitButtonHandler();
                		submit.addActionListener(subHandler);
                 
                		//set size
                		newPlane.setSize(150, 30);	
                		displaySeating.setSize(150, 30);	
                		assignSeat.setSize(150, 30);	
                		cancelSeat.setSize(150, 30);	
                		removeSeats.setSize(150, 30);	
                		exit.setSize(150, 30);
                		menu.setSize(150, 30);	
                		selOption.setSize(150,30);
                		submit.setSize(150, 30);
                 
                		//set location
                		newPlane.setLocation(100, 30);	
                		displaySeating.setLocation(100, 60);	
                		assignSeat.setLocation(100, 90);	
                		cancelSeat.setLocation(100, 120);	
                		removeSeats.setLocation(100, 150);	
                		exit.setLocation(100, 180);
                		menu.setLocation(0, 0);	
                		selOption.setLocation(0,30);
                		submit.setLocation(100, 210);
                 
                		//add graphic objects
                		c.add(newPlane);
                		c.add(displaySeating);
                		c.add(assignSeat);
                		c.add(cancelSeat);
                		c.add(removeSeats);
                		c.add(exit);
                		c.add(menu);
                		c.add(selOption);
                		c.add(submit);
                 
                		setTitle("Main Menu");
                		setSize(WIDTH, HEIGHT);
                		setVisible(true);
                		setDefaultCloseOperation(EXIT_ON_CLOSE);
                 
                	}
                 
                	public void displaySeatingMenu(){
                 
                		Container c = getContentPane();
                		c.setLayout(null);
                 
                		seatingMenu = new JLabel("Seating Chart");
                		seatingMenu.setSize(150, 30);
                		seatingMenu.setLocation(150, 0);
                		c.add(seatingMenu);
                 
                		back = new JButton("Back to Menu");
                		back.setSize(150, 30);
                		back.setLocation(120, 210);
                		c.add(back);
                 
                		seatingMenuTA = new JTextArea(40, 10);
                		seatingMenuTA.setSize(150,150);
                		seatingMenuTA.setLocation(100, 30);
                		//seatingMenuTA.append("\n\n\n\n\n\n\n\n\n\n");
                 
                 
                		c.add(seatingMenuTA);
                 
                		setTitle("Seating Chart");
                		setSize(WIDTH, HEIGHT);
                		setVisible(true);
                		setDefaultCloseOperation(EXIT_ON_CLOSE);
                 
                	}//end displaySeating
                 
                 
                 
                	private class SubmitButtonHandler implements ActionListener {
                		public void actionPerformed(ActionEvent e){
                			getContentPane().removeAll();
                			displaySeatingMenu();
                		}//end actionPerformed
                	}//end SubmitButtonHandler
                 
                 
                	public static void main(String[] args){
                		char[][] seating = new char[7][8];
                 
                		Airplane plane = new Airplane(); 
                		plane.mainMenu();
                 
                	}//end main
                }//end Airplane
                Since you seem new to swing, all I can say is that if you use JPanels you'll be able to see the remove working properly. I also do not think it's correct to learn how to use JButtons and JRadioButtons before you learn to use JPanel.

                Comment

                Working...