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. - 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
7 3032
not sure if I was clear enough, really frustrated right now, let me know if I wasn't clear :\
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
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...
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...
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.....
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. - 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
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. - 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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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...
| |