473,411 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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.

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
7 3032
not sure if I was clear enough, really frustrated right now, let me know if I wasn't clear :\
Dec 11 '06 #2
r035198x
13,262 8TB
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
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
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
1,806 1GB
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
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
r035198x
13,262 8TB
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

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

Similar topics

4
by: Fereshteh Shojaei | last post by:
Hi, I wonder if somebody could help me. I would like to move the cursor from one JTextField to another one when I press <CR>. Regards, Fereshteh
1
by: Unebrion | last post by:
Alright im working on a program that prints out user imput in a frame, along with a barcode.. it is like the front of an envelope. Here is the description for the program. This...
8
evilmonkey
by: evilmonkey | last post by:
I am using swing to nest squares with in squares (256 times) and that part is working fine, but what i want to do is change the color of each line so that the red green and blue components each...
1
by: twin2003 | last post by:
need help with inventory part 5 here is what I have to do Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
11
by: saytri | last post by:
I have made this piece of code where the output should display a dialogue box asking for a password. Then if the user enters it correctly it displays "Valid" or else it displays "Not Valid". However...
6
by: gaya3 | last post by:
Hi, I need to make the frame invisible on action event in swings. I have the following code: public class sample_pgm extends JFrame{ public void sample() { JFrame jf =...
6
crystal2005
by: crystal2005 | last post by:
Hi there, I need some help to correct my code. I would like to create a menu bar with the implementation of OO. So, i think i can edit and change it easyly if i use some classes. package...
0
by: i4mdelhi | last post by:
the follwing is the program I wrote to create a NOTEPAD.... After writing this code I get 3 error all at the 85th line.... Layout.java:85: illegal start of expression void...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.