Reputation: 4050
I have a JFrame with two JPanel. Two buttons. The app starts with no panels, just buttons. Upon pressing a button I want I display one panel, and then upon pressing another button I substitute one panel with another one and vice versa. I have this code, but it doesn't really work. The panels (when one button is clicked and then another) appear on top of each other. Any help would be appreciated! Thank you in advance!
Main.java
import javax.swing.*;
import javax.swing.text.View;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main {
static JButton enterDataButton = new JButton("Enter Data");
static JButton viewDataButton = new JButton("View Data");
static int yPosition = 10;
static int xLabelPosition = 20;
static int xFieldPosition = 20;
static int labelWidth = 200;
static int fieldWidth = 200;
public static void main (String[] args) {
final JFrame frame = new JFrame("SimpleTrans Main Window");
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setBounds(100, 100, 1200, 800);
frame.getContentPane().setLayout(null);
JPanel mainPanel = new JPanel();
enterDataButton.setBounds(xFieldPosition, yPosition, fieldWidth, 30);
enterDataButton.setForeground(Color.DARK_GRAY);
final InputDataArea inputDataArea = new InputDataArea();
final ViewDataArea viewDataArea = new ViewDataArea();
enterDataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("enterData Button Pressed!");
frame.remove(viewDataArea);
inputDataArea.InputDataArea(frame.getContentPane());
frame.add(inputDataArea);
frame.getContentPane().invalidate();
frame.getContentPane().validate();
frame.getContentPane().repaint();
}
});
viewDataButton.setBounds(xFieldPosition + fieldWidth, yPosition, fieldWidth, 30);
viewDataButton.setForeground(Color.DARK_GRAY);
viewDataButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("viewData Button Pressed!");
frame.remove(inputDataArea);
viewDataArea.ViewDataArea(frame.getContentPane());
frame.add(viewDataArea);
frame.getContentPane().revalidate();
frame.getContentPane().validate();
frame.getContentPane().repaint();
}
});
frame.getContentPane().add(enterDataButton);
frame.getContentPane().add(viewDataButton);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
InputDataArea.java
import javax.swing.*;
import java.awt.*;
public class InputDataArea extends JPanel {
private DataTextField textFieldTripNumber = new DataTextField();
private DataTextField textFieldExportCountry = new DataTextField();
private DataTextField textFieldDestinationCountry = new DataTextField();
private DataTextField textFieldPriceFixed = new DataTextField();
JLabel textFieldLabelTripNumber = new JLabel("Trip Number:");
JLabel textFieldLabelExportCountry = new JLabel("Export Country:");
JLabel textFieldLabelDestinationCountry = new JLabel("Destination Country:");
JLabel textFieldLabelPriceFixed = new JLabel("Price Fixed:");
JButton saveButton = new JButton("Save");
JButton emptyButton = new JButton("Empty");
int yPosition = 60;
int xLabelPosition = 20;
int xFieldPosition = 220;
int labelWidth = 200;
int fieldWidth = 200;
public void InputDataArea(Container pane) {
textFieldLabelTripNumber.setBounds(xLabelPosition, yPosition, labelWidth, 20);
pane.add(textFieldLabelTripNumber);
textFieldTripNumber.setColumns(20);
textFieldTripNumber.setBounds(xFieldPosition, yPosition, fieldWidth, 20);
pane.add(textFieldTripNumber);
textFieldLabelExportCountry.setBounds(xLabelPosition, yPosition + 30, labelWidth, 20);
pane.add(textFieldLabelExportCountry);
textFieldExportCountry.setColumns(20);
textFieldExportCountry.setBounds(xFieldPosition, yPosition + 30, fieldWidth, 20);
pane.add(textFieldExportCountry);
textFieldLabelDestinationCountry.setBounds(xLabelPosition, yPosition + 60, labelWidth, 20);
pane.add(textFieldLabelDestinationCountry);
textFieldDestinationCountry.setColumns(20);
textFieldDestinationCountry.setBounds(xFieldPosition, yPosition + 60, fieldWidth, 20);
pane.add(textFieldDestinationCountry);
textFieldLabelPriceFixed.setBounds(xLabelPosition, yPosition + 90, labelWidth, 20);
pane.add(textFieldLabelPriceFixed);
textFieldPriceFixed.setColumns(20);
textFieldPriceFixed.setBounds(xFieldPosition, yPosition + 90, fieldWidth, 20);
pane.add(textFieldPriceFixed);
saveButton.setBounds(xFieldPosition, yPosition + 130, fieldWidth, 50);
saveButton.setForeground(Color.GREEN);
pane.add(saveButton);
emptyButton.setBounds(xFieldPosition + fieldWidth, yPosition + 130, fieldWidth, 50);
emptyButton.setForeground(Color.RED);
pane.add(emptyButton);
}
}
ViewDataArea.java
import javax.swing.*;
import java.awt.*;
public class ViewDataArea extends JPanel {
private DataTextField textFieldTripNumber = new DataTextField();
private DataTextField textFieldExportCountry = new DataTextField();
private DataTextField textFieldDestinationCountry = new DataTextField();
private DataTextField textFieldPriceFixed = new DataTextField();
JLabel textFieldLabelViewData = new JLabel("ViewData");
JButton saveButton = new JButton("Save");
JButton emptyButton = new JButton("Empty");
int yPosition = 60;
int xLabelPosition = 20;
int xFieldPosition = 220;
int labelWidth = 200;
int fieldWidth = 200;
public void ViewDataArea(Container pane) {
textFieldLabelViewData.setBounds(xLabelPosition, yPosition, labelWidth, 20);
pane.add(textFieldLabelViewData);
}
}
DataTextField.java
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
public class DataTextField extends JTextField {
private List<JTextField> textFields = new ArrayList<JTextField>();
public void addTextField(JTextField textField) {
textFields.add(textField);
}
}
Upvotes: 0
Views: 99
Reputation: 324118
YOu should be using a Card Layout
. A Card Layout
is specifically designed to display multiple components in the same place. The Card Layout
will reserve space for the largest component and then you just swap components as required.
Read the section from the Swing tutorial on How to Use Card Layout for more information at a working example.
Upvotes: 2