By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

JFrame help

P: 8
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.

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Airplane extends JFrame {
  6.     private static final int WIDTH = 400;
  7.     private static final int HEIGHT = 300;
  8.  
  9.     private JLabel menu, selOption, seatingMenu;
  10.     private JRadioButton newPlane, displaySeating, assignSeat, cancelSeat, removeSeats, exit;
  11.     private JButton submit, back;
  12.     private ButtonGroup menuOptions;
  13.     private SubmitButtonHandler subHandler;
  14.     private boolean npTF, dsTF, asTF, csTF, rsTF, eTF;
  15.     private JTextArea seatingMenuTA;
  16.  
  17.     public Airplane(){
  18.  
  19.         Container c = getContentPane();
  20.         c.setLayout(null);
  21.  
  22.         setTitle("");
  23.         setSize(WIDTH, HEIGHT);
  24.         setVisible(true);
  25.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  26.     }
  27.  
  28.     public void mainMenu(){
  29.         Container c = getContentPane();
  30.         c.setLayout(null);
  31.  
  32.         //JLabels
  33.         menu = new JLabel("Main menu");
  34.         selOption = new JLabel("Select an option");
  35.  
  36.         //JRadioButtons
  37.         newPlane = new JRadioButton("Get new Plane");
  38.         displaySeating = new JRadioButton("Display seating");
  39.         assignSeat = new JRadioButton("Assign a seat");
  40.         cancelSeat = new JRadioButton("Cancel a seat");
  41.         removeSeats = new JRadioButton("Remove all seating");
  42.         exit = new JRadioButton("Exit program");
  43.  
  44.         //JButtons
  45.         submit = new JButton("Submit");
  46.         subHandler = new SubmitButtonHandler();
  47.         submit.addActionListener(subHandler);
  48.  
  49.         //set size
  50.         newPlane.setSize(150, 30);    
  51.         displaySeating.setSize(150, 30);    
  52.         assignSeat.setSize(150, 30);    
  53.         cancelSeat.setSize(150, 30);    
  54.         removeSeats.setSize(150, 30);    
  55.         exit.setSize(150, 30);
  56.         menu.setSize(150, 30);    
  57.         selOption.setSize(150,30);
  58.         submit.setSize(150, 30);
  59.  
  60.         //set location
  61.         newPlane.setLocation(100, 30);    
  62.         displaySeating.setLocation(100, 60);    
  63.         assignSeat.setLocation(100, 90);    
  64.         cancelSeat.setLocation(100, 120);    
  65.         removeSeats.setLocation(100, 150);    
  66.         exit.setLocation(100, 180);
  67.         menu.setLocation(0, 0);    
  68.         selOption.setLocation(0,30);
  69.         submit.setLocation(100, 210);
  70.  
  71.         //add graphic objects
  72.         c.add(newPlane);
  73.         c.add(displaySeating);
  74.         c.add(assignSeat);
  75.         c.add(cancelSeat);
  76.         c.add(removeSeats);
  77.         c.add(exit);
  78.         c.add(menu);
  79.         c.add(selOption);
  80.         c.add(submit);
  81.  
  82.         //button group
  83.         menuOptions = new ButtonGroup();
  84.         menuOptions.add(newPlane);
  85.         menuOptions.add(displaySeating);
  86.         menuOptions.add(assignSeat);
  87.         menuOptions.add(cancelSeat);
  88.         menuOptions.add(removeSeats);
  89.         menuOptions.add(exit);
  90.  
  91.         setTitle("Main Menu");
  92.         setSize(WIDTH, HEIGHT);
  93.         setVisible(true);
  94.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  95.  
  96.     }
  97.  
  98.     public void displaySeatingMenu(){
  99.  
  100.         Container c = getContentPane();
  101.         c.setLayout(null);
  102.  
  103.         seatingMenu = new JLabel("Seating Chart");
  104.         seatingMenu.setSize(150, 30);
  105.         seatingMenu.setLocation(150, 0);
  106.         c.add(seatingMenu);
  107.  
  108.         back = new JButton("Back to Menu");
  109.         back.setSize(150, 30);
  110.         back.setLocation(120, 210);
  111.         c.add(back);
  112.  
  113.         seatingMenuTA = new JTextArea(40, 10);
  114.         seatingMenuTA.setSize(150,150);
  115.         seatingMenuTA.setLocation(100, 30);
  116.         //seatingMenuTA.append("\n\n\n\n\n\n\n\n\n\n");
  117.  
  118.  
  119.         c.add(seatingMenuTA);
  120.  
  121.         setTitle("Seating Chart");
  122.         setSize(WIDTH, HEIGHT);
  123.         setVisible(true);
  124.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  125.  
  126.     }//end displaySeating
  127.  
  128.  
  129.  
  130.     private class SubmitButtonHandler implements ActionListener {
  131.         public void actionPerformed(ActionEvent e){
  132.  
  133.             if(npTF == true){
  134.  
  135.             }
  136.             if(asTF == true){
  137.  
  138.             }
  139.             if(dsTF == true){
  140.  
  141.  
  142.             }
  143.             if(csTF == true){
  144.  
  145.             }
  146.             if(rsTF == true){
  147.  
  148.             }
  149.             if(eTF == true){
  150.  
  151.             }
  152.             else{
  153.             }
  154.         }//end actionPerformed
  155.     }//end SubmitButtonHandler
  156.  
  157.     public void itemStateChanged(ItemEvent e){
  158.         if(e.getSource() == newPlane){
  159.             npTF = true; dsTF = false; asTF = false; csTF = false; rsTF = false; eTF = false;
  160.         }
  161.         if(e.getSource() == displaySeating){
  162.         npTF = false; dsTF = true; asTF = false; csTF = false; rsTF = false; eTF = false; 
  163.         }
  164.         if(e.getSource() == assignSeat){
  165.             npTF = false; dsTF = false; asTF = true; csTF = false; rsTF = false; eTF = false;
  166.         }
  167.         if(e.getSource() == cancelSeat){
  168.             npTF = false; dsTF = false; asTF = false; csTF = true; rsTF = false; eTF = false;
  169.         }
  170.         if(e.getSource() == removeSeats){
  171.             npTF = false; dsTF = false; asTF = false; csTF = false; rsTF = true; eTF = false;
  172.         }
  173.         if(e.getSource() == exit){
  174.             npTF = false; dsTF = false; asTF = false; csTF = false; rsTF = false; eTF = true;
  175.         }
  176.     }//end itemStateChanged
  177.  
  178.     public static void main(String[] args){
  179.         char[][] seating = new char[7][8];
  180.  
  181.         Airplane plane = new Airplane(); 
  182.         plane.mainMenu();
  183.  
  184.     }//end main
  185. }//end Airplane
Dec 11 '06 #1
Share this Question
Share on Google+
7 Replies


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

10K+
P: 13,264
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
Dec 11 '06 #3

P: 8
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...
Dec 11 '06 #4

P: 8
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...
Dec 11 '06 #5

DeMan
100+
P: 1,806
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.....
Dec 11 '06 #6

P: 8
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 displaySeatingMenu() correctly, the problem is components from mainMenu() don't get removed.
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Airplane extends JFrame {
  6.     private static final int WIDTH = 400;
  7.     private static final int HEIGHT = 300;
  8.  
  9.     private JLabel menu, selOption, seatingMenu;
  10.     private JButton submit, back, newPlane, displaySeating, assignSeat, cancelSeat, removeSeats, exit;
  11.     private ButtonGroup menuOptions;
  12.     private SubmitButtonHandler subHandler;
  13.     private boolean npTF, dsTF, asTF, csTF, rsTF, eTF;
  14.     private JTextArea seatingMenuTA;
  15.  
  16.     public Airplane(){
  17.  
  18.         Container c = getContentPane();
  19.         c.setLayout(null);
  20.  
  21.         setTitle("");
  22.         setSize(WIDTH, HEIGHT);
  23.         setVisible(true);
  24.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  25.     }
  26.  
  27.     public void mainMenu(){
  28.         Container c = getContentPane();
  29.         c.setLayout(null);
  30.  
  31.         //JLabels
  32.         menu = new JLabel("Main menu");
  33.         selOption = new JLabel("Select an option");
  34.  
  35.         //JButtons
  36.         newPlane = new JButton("Get new Plane");
  37.         displaySeating = new JButton("Display seating");
  38.         assignSeat = new JButton("Assign a seat");
  39.         cancelSeat = new JButton("Cancel a seat");
  40.         removeSeats = new JButton("Remove all seating");
  41.         exit = new JButton("Exit program");
  42.         submit = new JButton("Submit");
  43.         subHandler = new SubmitButtonHandler();
  44.         submit.addActionListener(subHandler);
  45.  
  46.         //set size
  47.         newPlane.setSize(150, 30);    
  48.         displaySeating.setSize(150, 30);    
  49.         assignSeat.setSize(150, 30);    
  50.         cancelSeat.setSize(150, 30);    
  51.         removeSeats.setSize(150, 30);    
  52.         exit.setSize(150, 30);
  53.         menu.setSize(150, 30);    
  54.         selOption.setSize(150,30);
  55.         submit.setSize(150, 30);
  56.  
  57.         //set location
  58.         newPlane.setLocation(100, 30);    
  59.         displaySeating.setLocation(100, 60);    
  60.         assignSeat.setLocation(100, 90);    
  61.         cancelSeat.setLocation(100, 120);    
  62.         removeSeats.setLocation(100, 150);    
  63.         exit.setLocation(100, 180);
  64.         menu.setLocation(0, 0);    
  65.         selOption.setLocation(0,30);
  66.         submit.setLocation(100, 210);
  67.  
  68.         //add graphic objects
  69.         c.add(newPlane);
  70.         c.add(displaySeating);
  71.         c.add(assignSeat);
  72.         c.add(cancelSeat);
  73.         c.add(removeSeats);
  74.         c.add(exit);
  75.         c.add(menu);
  76.         c.add(selOption);
  77.         c.add(submit);
  78.  
  79.         setTitle("Main Menu");
  80.         setSize(WIDTH, HEIGHT);
  81.         setVisible(true);
  82.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  83.  
  84.     }
  85.  
  86.     public void displaySeatingMenu(){
  87.  
  88.         Container c = getContentPane();
  89.         c.setLayout(null);
  90.  
  91.         seatingMenu = new JLabel("Seating Chart");
  92.         seatingMenu.setSize(150, 30);
  93.         seatingMenu.setLocation(150, 0);
  94.         c.add(seatingMenu);
  95.  
  96.         back = new JButton("Back to Menu");
  97.         back.setSize(150, 30);
  98.         back.setLocation(120, 210);
  99.         c.add(back);
  100.  
  101.         seatingMenuTA = new JTextArea(40, 10);
  102.         seatingMenuTA.setSize(150,150);
  103.         seatingMenuTA.setLocation(100, 30);
  104.         //seatingMenuTA.append("\n\n\n\n\n\n\n\n\n\n");
  105.  
  106.  
  107.         c.add(seatingMenuTA);
  108.  
  109.         setTitle("Seating Chart");
  110.         setSize(WIDTH, HEIGHT);
  111.         setVisible(true);
  112.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  113.  
  114.     }//end displaySeating
  115.  
  116.  
  117.  
  118.     private class SubmitButtonHandler implements ActionListener {
  119.         public void actionPerformed(ActionEvent e){
  120.             getContentPane().removeAll();
  121.             displaySeatingMenu();
  122.         }//end actionPerformed
  123.     }//end SubmitButtonHandler
  124.  
  125.  
  126.     public static void main(String[] args){
  127.         char[][] seating = new char[7][8];
  128.  
  129.         Airplane plane = new Airplane(); 
  130.         plane.mainMenu();
  131.  
  132.     }//end main
  133. }//end Airplane
Dec 11 '06 #7

10K+
P: 13,264
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 displaySeatingMenu() correctly, the problem is components from mainMenu() don't get removed.
Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class Airplane extends JFrame {
  6.     private static final int WIDTH = 400;
  7.     private static final int HEIGHT = 300;
  8.  
  9.     private JLabel menu, selOption, seatingMenu;
  10.     private JButton submit, back, newPlane, displaySeating, assignSeat, cancelSeat, removeSeats, exit;
  11.     private ButtonGroup menuOptions;
  12.     private SubmitButtonHandler subHandler;
  13.     private boolean npTF, dsTF, asTF, csTF, rsTF, eTF;
  14.     private JTextArea seatingMenuTA;
  15.  
  16.     public Airplane(){
  17.  
  18.         Container c = getContentPane();
  19.         c.setLayout(null);
  20.  
  21.         setTitle("");
  22.         setSize(WIDTH, HEIGHT);
  23.         setVisible(true);
  24.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  25.     }
  26.  
  27.     public void mainMenu(){
  28.         Container c = getContentPane();
  29.         c.setLayout(null);
  30.  
  31.         //JLabels
  32.         menu = new JLabel("Main menu");
  33.         selOption = new JLabel("Select an option");
  34.  
  35.         //JButtons
  36.         newPlane = new JButton("Get new Plane");
  37.         displaySeating = new JButton("Display seating");
  38.         assignSeat = new JButton("Assign a seat");
  39.         cancelSeat = new JButton("Cancel a seat");
  40.         removeSeats = new JButton("Remove all seating");
  41.         exit = new JButton("Exit program");
  42.         submit = new JButton("Submit");
  43.         subHandler = new SubmitButtonHandler();
  44.         submit.addActionListener(subHandler);
  45.  
  46.         //set size
  47.         newPlane.setSize(150, 30);    
  48.         displaySeating.setSize(150, 30);    
  49.         assignSeat.setSize(150, 30);    
  50.         cancelSeat.setSize(150, 30);    
  51.         removeSeats.setSize(150, 30);    
  52.         exit.setSize(150, 30);
  53.         menu.setSize(150, 30);    
  54.         selOption.setSize(150,30);
  55.         submit.setSize(150, 30);
  56.  
  57.         //set location
  58.         newPlane.setLocation(100, 30);    
  59.         displaySeating.setLocation(100, 60);    
  60.         assignSeat.setLocation(100, 90);    
  61.         cancelSeat.setLocation(100, 120);    
  62.         removeSeats.setLocation(100, 150);    
  63.         exit.setLocation(100, 180);
  64.         menu.setLocation(0, 0);    
  65.         selOption.setLocation(0,30);
  66.         submit.setLocation(100, 210);
  67.  
  68.         //add graphic objects
  69.         c.add(newPlane);
  70.         c.add(displaySeating);
  71.         c.add(assignSeat);
  72.         c.add(cancelSeat);
  73.         c.add(removeSeats);
  74.         c.add(exit);
  75.         c.add(menu);
  76.         c.add(selOption);
  77.         c.add(submit);
  78.  
  79.         setTitle("Main Menu");
  80.         setSize(WIDTH, HEIGHT);
  81.         setVisible(true);
  82.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  83.  
  84.     }
  85.  
  86.     public void displaySeatingMenu(){
  87.  
  88.         Container c = getContentPane();
  89.         c.setLayout(null);
  90.  
  91.         seatingMenu = new JLabel("Seating Chart");
  92.         seatingMenu.setSize(150, 30);
  93.         seatingMenu.setLocation(150, 0);
  94.         c.add(seatingMenu);
  95.  
  96.         back = new JButton("Back to Menu");
  97.         back.setSize(150, 30);
  98.         back.setLocation(120, 210);
  99.         c.add(back);
  100.  
  101.         seatingMenuTA = new JTextArea(40, 10);
  102.         seatingMenuTA.setSize(150,150);
  103.         seatingMenuTA.setLocation(100, 30);
  104.         //seatingMenuTA.append("\n\n\n\n\n\n\n\n\n\n");
  105.  
  106.  
  107.         c.add(seatingMenuTA);
  108.  
  109.         setTitle("Seating Chart");
  110.         setSize(WIDTH, HEIGHT);
  111.         setVisible(true);
  112.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  113.  
  114.     }//end displaySeating
  115.  
  116.  
  117.  
  118.     private class SubmitButtonHandler implements ActionListener {
  119.         public void actionPerformed(ActionEvent e){
  120.             getContentPane().removeAll();
  121.             displaySeatingMenu();
  122.         }//end actionPerformed
  123.     }//end SubmitButtonHandler
  124.  
  125.  
  126.     public static void main(String[] args){
  127.         char[][] seating = new char[7][8];
  128.  
  129.         Airplane plane = new Airplane(); 
  130.         plane.mainMenu();
  131.  
  132.     }//end main
  133. }//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.
Dec 12 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.