For my second programming assignment in my Java class, we have to create a Pizza Shop menu GUI. Everything appears on my GUI (including choices, boxes, radio buttons, etc.) except for the button ("Process Selection") you are supposed to click for calculating the total cost. The following is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PizzaShop extends JFrame {
private Topping t;
private PizzaSize ps;
private PizzaType pt;
private JPanel buttonPanel;
private JButton ProcessSelection;
public PizzaShop() {
super("Welcome To Home Pizza Shop");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
t = new Topping();
ps = new PizzaSize();
pt = new PizzaType();
createPanel();
add(t, BorderLayout.NORTH);
add(ps, BorderLayout.WEST);
add(pt, BorderLayout.CENTER);
setVisible(true);
}
private void createPanel() {
buttonPanel = new JPanel();
ProcessSelection = new JButton("Process Selection");
ProcessSelection.addActionListener(new calButton());
buttonPanel.add(ProcessSelection);
}
private class calButton implements ActionListener {
public void actionPerformed(ActionEvent e) {
double subtotal;
subtotal = t.getTopping() + ps.getPizzaSize();
JOptionPane.showMessageDialog(null, "Your Order \n" + "Pizza Type" + pt.getPizzaType() + "\n" + "Amount Due" + subtotal);
}
}
private class ExitButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
}
Did I forget to do add some code somewhere? I'm boggled.
You are not adding the buttonPanel to the main view
UPDATE: you should do this:
private void createPanel() {
buttonPanel = new JPanel();
ProcessSelection = new JButton("Process Selection");
ProcessSelection.addActionListener(new calButton());
buttonPanel.add(ProcessSelection);
add(buttonPanel, BorderLayout.SOUTH);
}
You need to add the button panel to the frame:
add(buttonPanel, BorderLayout.SOUTH);
Change your createPanel() to this:
private JPanel createPanel() {
buttonPanel = new JPanel();
ProcessSelection = new JButton("Process Selection");
ProcessSelection.addActionListener(new calButton());
buttonPanel.add(ProcessSelection);
return buttonPanel;
}
And add the code below to your PizzaShop() method:
add(createPanel(), BorderLayout.SOUTH);
Or you can just follow what Adel Boutros said.
Related
Please help me to understand how this works. I'm having difficulties to understand how, for example, JButton in one class can alter text in JTextArea that is in another class of a same package. I've made a simple app just to ask a question here, I need this for a bigger school project where I need to implement this to work with multiple classes.
When I put everything in the same class it works but I need it in separate classes.
Here is the simple code.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Button extends JPanel {
private JButton button;
private Panel panel;
public Button() {
button = new JButton("BUTTON");
panel = new Panel();
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton) e.getSource();
String input = clicked.getText();
panel.setTextArea(input);
//System.out.println(input);
}
});
}
}
class Panel extends JPanel {
private JTextArea textArea;
public Panel() {
setLayout(new BorderLayout());
textArea = new JTextArea();
add(textArea, BorderLayout.CENTER);
}
public JTextArea getTextArea() {
return textArea;
}
void setTextArea(String text) {
this.textArea.setText(text);
}
}
public class Java extends JFrame {
private Button dugme;
private JFrame frame;
private Panel panel;
public Java() {
frame = new JFrame();
dugme = new Button();
panel = new Panel();
//super("test");
frame.setLayout(new BorderLayout());
frame.setTitle("test");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(dugme, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Java app = new Java();
}
}
I want action listener to alter the text in the panel, sys-out works so the listener listens the button but I can't make it to alter the text in text area.
As already mentioned by #XtremeBaumer you have two different instances of Panel class. You need to remove the secode one.
public class Button extends JPanel {
private JButton button;
private Panel panel;
public Button(Panel panel) { // we need already created instance of panel here.
this.panel = panel;
button = new JButton("BUTTON");
// panel = new Panel(); <-- this line must be deleted.
// ...
}
}
public class Java extends JFrame {
private Button dugme;
private JFrame frame;
private Panel panel;
public Java(){
frame = new JFrame();
panel = new Panel();
dugme = new Button(panel);
// ...
}
}
Please also replace the line
add(textArea, BorderLayout.CENTER);
by
add(new JScrollPane(textArea), BorderLayout.CENTER);
This allows you to get the scrool bars when text goes larger than the text ara size.
Here is your reworked example
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class Button extends JPanel {
private JButton button;
private Panel panel;
public Button(Panel panel) {
this.panel = panel;
button = new JButton("BUTTON");
add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JButton clicked = (JButton) e.getSource();
String input = clicked.getText();
panel.setTextArea(input);
//System.out.println(input);
}
});
}
}
class Panel extends JPanel {
private JTextArea textArea;
public Panel() {
setLayout(new BorderLayout());
textArea = new JTextArea();
add(new JScrollPane(textArea), BorderLayout.CENTER);
}
public JTextArea getTextArea() {
return textArea;
}
void setTextArea(String text) {
this.textArea.setText(text);
}
}
public class Java extends JFrame {
private Button dugme;
private JFrame frame;
private Panel panel;
public Java() {
frame = new JFrame();
panel = new Panel();
dugme = new Button(panel);
//super("test");
frame.setLayout(new BorderLayout());
frame.setTitle("test");
frame.setSize(300, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(dugme, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
}
public static void main(String[] args) {
Java app = new Java();
}
}
There is 3 panels which I created as seen in the image. The first panel is the "From" panel, second is "To" panel, and third is the buttons panel. So the question is, how can I put a new line for the "Enter Temperature: [ ]" so that it will be under neath the radio buttons? I am very new to Java swing/awt please bear with me.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class TemperatureConversion extends JFrame{
// component
JTextField txtFromTemp, txtToTemp;
JLabel lblFromTemp, lblToTemp;
JRadioButton radFromCelsius, radFromFahrenheit, radFromKelvin;
JRadioButton radToCelsius, radToFahrenheit, radToKelvin;
JPanel pnlFromRadioButton, pnlToRadioButton, pnlFromTemp, pnlButton;
ButtonGroup bgFrom, bgTo;
JButton btnConvert, btnExit;
// constructor
public TemperatureConversion(){
super("Temperature");
// assign objects
radFromCelsius = new JRadioButton("Celsius", true);
radFromFahrenheit = new JRadioButton("Fahrenheit");
radFromKelvin = new JRadioButton("Kelvin");
lblFromTemp = new JLabel("Enter Temperature: ");
pnlFromTemp = new JPanel();
btnConvert = new JButton("Convert");
btnExit = new JButton("Exit");
pnlButton = new JPanel();
txtFromTemp = new JTextField(3);
lblToTemp = new JLabel("Comparable Temperature: ");
txtToTemp = new JTextField(3);
// register the button to a listener
btnExit.addActionListener(new MyButtonListener());
btnConvert.addActionListener(new MyButtonListener());
// make the multiple choice exclusive but not a container
bgFrom = new ButtonGroup();
bgFrom.add(radFromCelsius);
bgFrom.add(radFromFahrenheit);
bgFrom.add(radFromKelvin);
// radio buttons
radToCelsius = new JRadioButton("Celsius");
radToFahrenheit = new JRadioButton("Fahrenheit", true);
radToKelvin = new JRadioButton("Kelvin");
// make the multiple choice exclusive
bgTo = new ButtonGroup();
bgTo.add(radToCelsius);
bgTo.add(radToFahrenheit);
bgTo.add(radToKelvin);
pnlFromRadioButton = new JPanel();
pnlToRadioButton = new JPanel();
// decorate the panel
pnlFromRadioButton.setBorder(BorderFactory.createTitledBorder("From"));
pnlToRadioButton.setBorder(BorderFactory.createTitledBorder("To"));
// add radiobutton to panel
pnlFromRadioButton.add(radFromCelsius);
pnlFromRadioButton.add(radFromFahrenheit);
pnlFromRadioButton.add(radFromKelvin);
pnlToRadioButton.add(radToCelsius);
pnlToRadioButton.add(radToFahrenheit);
pnlToRadioButton.add(radToKelvin);
// add button to panel
pnlButton.add(btnConvert);
pnlButton.add(btnExit);
// add label and txt field to panel
pnlFromRadioButton.add(lblFromTemp);
pnlFromRadioButton.add(txtFromTemp);
pnlToRadioButton.add(lblToTemp);
txtToTemp.setEditable(false);
pnlToRadioButton.add(txtToTemp);
// add panels to the frame
add(pnlFromRadioButton, BorderLayout.NORTH);
add(pnlToRadioButton, BorderLayout.CENTER);
add(pnlButton, BorderLayout.SOUTH);
setVisible(true);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
// private inner class to handle button event
private class MyButtonListener implements ActionListener {
// must override actionPerformed method
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnConvert) {
if (radFromCelsius.isSelected())
System.out.print("exit");
} else if (e.getSource() == btnExit) {
System.exit(0);
}
}
}
public static void main(String[] args) {
new TemperatureConversion();
}
}
Nest more JPanels and use layout managers
For instance, in the JPanel where you want two lines, give it a BoxLayout oriented along the BoxLayout.PAGE_AXIS, and then add two more JPanels to this BoxLayout-using, a top JPanel with the radio buttons and bottom JPanel with the JLabel and JTextField (or whatever else you want in it).
Side note: this would be a great place to use an enum one called TempScale that had three values: CELSIUS, FAHRENHEIT, KELVIN. You could even give the enum the formulas for conversion to and from Kelvin.
For example:
import java.awt.Component;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
import javax.swing.*;
#SuppressWarnings("serial")
public class TempConversion2 extends JPanel {
private ToFromPanel fromPanel = new ToFromPanel("From", true);
private ToFromPanel toPanel = new ToFromPanel("To", false);
private ButtonPanel buttonPanel = new ButtonPanel(fromPanel, toPanel);
public TempConversion2() {
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(fromPanel);
add(toPanel);
add(buttonPanel);
}
private static void createAndShowGui() {
TempConversion2 mainPanel = new TempConversion2();
JFrame frame = new JFrame("Temp Convert");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
#SuppressWarnings("serial")
class ButtonPanel extends JPanel {
public ButtonPanel(ToFromPanel fromPanel, ToFromPanel toPanel) {
add(new JButton(new ConvertAction("Convert", fromPanel, toPanel)));
add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
}
}
#SuppressWarnings("serial")
class ConvertAction extends AbstractAction {
private ToFromPanel fromPanel;
private ToFromPanel toPanel;
public ConvertAction(String name, ToFromPanel fromPanel, ToFromPanel toPanel) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
this.fromPanel = fromPanel;
this.toPanel = toPanel;
}
#Override
public void actionPerformed(ActionEvent e) {
String text = fromPanel.getText();
try {
double fromTemp = Double.parseDouble(text.trim());
TempScale fromScale = fromPanel.getTempScalesPanel().getSelectedTempScale();
double kelvinValue = fromScale.convertToKelvin(fromTemp);
TempScale toScale = toPanel.getTempScalesPanel().getSelectedTempScale();
double toValue = toScale.convertFromKelvin(kelvinValue);
String toValueString = String.format("%.2f", toValue);
toPanel.setText(toValueString);
} catch (NumberFormatException e1) {
Component parentComponent = fromPanel;
String message = "Text must be a valid number: " + text;
String title = "Invalid Text Entered";
int messageType = JOptionPane.ERROR_MESSAGE;
JOptionPane.showMessageDialog(parentComponent, message, title, messageType);
fromPanel.setText("");
}
}
}
#SuppressWarnings("serial")
class ExitAction extends AbstractAction {
public ExitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}
#SuppressWarnings("serial")
class ToFromPanel extends JPanel {
private String title;
private TempScalesPanel tempScalesPanel = new TempScalesPanel();
private JTextField tempTextField = new JTextField(3);
public ToFromPanel(String title, boolean textFieldEnabled) {
this.title = title;
tempTextField.setFocusable(textFieldEnabled);
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.LINE_AXIS));
bottomPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
bottomPanel.add(new JLabel("Temperature:"));
bottomPanel.add(Box.createHorizontalStrut(8));
bottomPanel.add(tempTextField);
setBorder(BorderFactory.createTitledBorder(title));
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
add(tempScalesPanel);
add(bottomPanel);
}
public String getTitle() {
return title;
}
public TempScalesPanel getTempScalesPanel() {
return tempScalesPanel;
}
public String getText() {
return tempTextField.getText();
}
public void setText(String text) {
tempTextField.setText(text);
}
}
#SuppressWarnings("serial")
class TempScalesPanel extends JPanel {
private ButtonGroup buttonGroup = new ButtonGroup();
private Map<ButtonModel, TempScale> buttonTempMap = new HashMap<>();
public TempScalesPanel() {
for (TempScale tempScale : TempScale.values()) {
JRadioButton radioButton = new JRadioButton(tempScale.getName());
add(radioButton);
buttonGroup.add(radioButton);
buttonTempMap.put(radioButton.getModel(), tempScale);
// set first button as selected by default
if (buttonGroup.getSelection() == null) {
buttonGroup.setSelected(radioButton.getModel(), true);
}
}
}
public TempScale getSelectedTempScale() {
ButtonModel model = buttonGroup.getSelection();
return buttonTempMap.get(model);
}
}
This is the enum that I was talking about. Note that if you change the enum, and for instance add another temperature scale element, the program will automatically include it in the GUI and in the calculations. God I love Java and OOP.
public enum TempScale {
CELSIUS("Celsius", 1.0, -273.15),
FAHRENHEIT("Fahrenheit", 5.0 / 9.0, -459.67),
KELVIN("Kelvin", 1.0, 0.0);
private TempScale(String name, double ratioToKelvin, double absZero) {
this.name = name;
this.ratioToKelvin = ratioToKelvin;
this.absZero = absZero;
}
private String name;
private double ratioToKelvin;
private double absZero;
public String getName() {
return name;
}
public double getRatioToKelvin() {
return ratioToKelvin;
}
public double getAbsZero() {
return absZero;
}
public double convertToKelvin(double value) {
return (value - absZero) * ratioToKelvin;
}
public double convertFromKelvin(double kelvinValue) {
return (kelvinValue / ratioToKelvin) + absZero;
}
}
Always consider posting an MCVE.
For example you layout can be simplified and demonstrated with :
import java.awt.BorderLayout;
import java.awt.Label;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TemperatureConversion extends JFrame{
JPanel pnlFromRadioButton, pnlToRadioButton, pnlFromTemp, pnlButton;
// constructor
public TemperatureConversion(){
pnlFromRadioButton = new JPanel();
pnlFromRadioButton.add(new Label("From Panel"));
pnlToRadioButton = new JPanel();
pnlToRadioButton.add(new Label("To Panel"));
pnlButton = new JPanel();
pnlButton.add(new Label("Buttons Panel"));
// add panels to the frame
add(pnlFromRadioButton, BorderLayout.NORTH);
add(pnlToRadioButton, BorderLayout.CENTER);
add(pnlButton, BorderLayout.SOUTH);
setVisible(true);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new TemperatureConversion();
}
}
Suppose you want to an "Enter Temperature: [ ]" label to show in a different "line" under From buttons, your constructor will change to :
public TemperatureConversion(){
//set a layout manger. You could use grid layout
//GridLayout gridLayout = new GridLayout(4, 1);
//Or BoxLayout
BoxLayout boxLayout = new BoxLayout(getContentPane(), BoxLayout.Y_AXIS); // top to bottom
setLayout(boxLayout);
pnlFromRadioButton = new JPanel();
pnlFromRadioButton.add(new Label("From Panel"));
//create a panel to hold the desired label
pnlFromTemp = new JPanel();
pnlFromTemp.add(new JLabel("Enter Temperature: [ ]"));//add label
pnlToRadioButton = new JPanel();
pnlToRadioButton.add(new Label("To Panel"));
pnlButton = new JPanel();
pnlButton.add(new Label("Buttons Panel"));
// add panels to the frame
//the panel will show in the order added
add(pnlFromRadioButton);
add(pnlFromTemp);
add(pnlToRadioButton);
add(pnlButton);
setVisible(true);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
Sorry I am new to forums and am not familiar with posting etiquette for codes #c0der. But using GridLayout solved my problem and I want to show you what I did, it is a big mess but here it is. Here is my bizarre code but don't know how to reduce it any further. This is how it is suppose to look as I wanted and because now I understand what you mean by "Nest more JPanels and use layout managers" #Hovercraft Full of Eels:
my temperature program
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class TemperatureConversion extends JFrame {
// component
JTextField txtFromTemp, txtToTemp;
JLabel lblFromTemp, lblToTemp, lblToTempbox;
JRadioButton radFromCelsius, radFromFahrenheit, radFromKelvin;
JRadioButton radToCelsius, radToFahrenheit, radToKelvin;
JPanel pnlFromRadioButton, pnlToRadioButton, pnlFrom, pnlTo, pnlButton;
JPanel pnlEnterTemp, pnlComparableTemp;
ButtonGroup bgFrom, bgTo;
JButton btnConvert, btnExit;
// constructor
public TemperatureConversion() {
super("Temperature");
// assign objects
radFromCelsius = new JRadioButton("Celsius", true);
radFromFahrenheit = new JRadioButton("Fahrenheit");
radFromKelvin = new JRadioButton("Kelvin");
lblFromTemp = new JLabel("Enter Temperature: ");
pnlFrom = new JPanel();
btnConvert = new JButton("Convert");
btnExit = new JButton("Exit");
pnlButton = new JPanel();
txtFromTemp = new JTextField(3);
lblToTemp = new JLabel("Comparable Temperature: ");
txtToTemp = new JTextField(3);
pnlTo = new JPanel();
pnlEnterTemp = new JPanel();
pnlComparableTemp = new JPanel();
pnlFromRadioButton = new JPanel();
pnlToRadioButton = new JPanel();
// register the button to a listener
btnExit.addActionListener(new MyButtonListener());
btnConvert.addActionListener(new MyButtonListener());
// make the multiple choice exclusive but not a container
bgFrom = new ButtonGroup();
bgFrom.add(radFromCelsius);
bgFrom.add(radFromFahrenheit);
bgFrom.add(radFromKelvin);
// radio buttons
radToCelsius = new JRadioButton("Celsius");
radToFahrenheit = new JRadioButton("Fahrenheit", true);
radToKelvin = new JRadioButton("Kelvin");
// make the multiple choice exclusive
bgTo = new ButtonGroup();
bgTo.add(radToCelsius);
bgTo.add(radToFahrenheit);
bgTo.add(radToKelvin);
pnlFrom.setLayout(new GridLayout(2, 1));
pnlFrom.add(pnlFromRadioButton);
pnlFrom.add(pnlEnterTemp);
pnlTo.setLayout(new GridLayout(2, 1));
pnlTo.add(pnlToRadioButton);
pnlTo.add(pnlComparableTemp);
// decorate the panel
pnlFrom.setBorder(BorderFactory.createTitledBorder("From"));
pnlTo.setBorder(BorderFactory.createTitledBorder("To"));
// add radiobutton to panel
pnlFromRadioButton.add(radFromCelsius);
pnlFromRadioButton.add(radFromFahrenheit);
pnlFromRadioButton.add(radFromKelvin);
pnlToRadioButton.add(radToCelsius);
pnlToRadioButton.add(radToFahrenheit);
pnlToRadioButton.add(radToKelvin);
// add button to panel
pnlButton.add(btnConvert);
pnlButton.add(btnExit);
// add label and txt field to panel
pnlEnterTemp.add(lblFromTemp);
pnlEnterTemp.add(txtFromTemp);
pnlComparableTemp.add(lblToTemp);
txtToTemp.setEditable(false);
pnlComparableTemp.add(txtToTemp);
// add panels to the frame
add(pnlFrom, BorderLayout.NORTH);
add(pnlTo, BorderLayout.CENTER);
add(pnlButton, BorderLayout.SOUTH);
setVisible(true);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
// private inner class to handle button event
private class MyButtonListener implements ActionListener {
// must override actionPerformed method
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnConvert) {
if (radFromCelsius.isSelected() && radToFahrenheit.isSelected()) {
int strInput = Integer.parseInt(txtFromTemp.getText());
int celsius = strInput * 9 / 5 + 32;
txtToTemp.setText(Integer.toString(celsius));
} else if (radFromCelsius.isSelected() && radToCelsius.isSelected() ||
radFromFahrenheit.isSelected() && radToFahrenheit.isSelected() ||
radFromKelvin.isSelected() && radToKelvin.isSelected()) {
txtToTemp.setText(txtFromTemp.getText());
} else if (radToCelsius.isSelected() && radFromFahrenheit.isSelected()) {
int strInput = Integer.parseInt(txtFromTemp.getText());
int fahrenheit = (strInput - 32) * 5 / 9;
txtToTemp.setText(Integer.toString(fahrenheit));
} else if (radFromKelvin.isSelected() && radToCelsius.isSelected()) {
double strInput = Integer.parseInt(txtFromTemp.getText());
double celsius = strInput - 273.15;
txtToTemp.setText(Double.toString(celsius));
} else if (radFromKelvin.isSelected() && radToFahrenheit.isSelected()) {
double strInput = Integer.parseInt(txtFromTemp.getText());
double fahrenheit = strInput - 459.67;
txtToTemp.setText(Double.toString(fahrenheit));
} else if (radFromCelsius.isSelected() && radToKelvin.isSelected()) {
double strInput = Integer.parseInt(txtFromTemp.getText());
double celsius = strInput + 273.15;
txtToTemp.setText(Double.toString(celsius));
} else if (radFromFahrenheit.isSelected() && radToKelvin.isSelected()) {
double strInput = Integer.parseInt(txtFromTemp.getText());
double fahrenheit = strInput + 255.37;
txtToTemp.setText(Double.toString(fahrenheit));
}
} else if (e.getSource() == btnExit) {
System.exit(0);
}
}
}
public static void main(String[] args) {
new TemperatureConversion();
}
}
By the way #Hovercraft Full Of Eels, your solution is way more efficient and advanced than my level of thinking. I am newbie to programming in general so bear with me on my messy code and organization lol. I have barely dipped my feet into OOP. I do have a sense of how you used enum for TempScale and I thank you for your suggestion. I will keep these in my notes as references.
I have a class here that when I try to run gives me the error:
The type GUI must implement the inherited abstract method ActionListener.actionPerformed(actionEvent)
void is an invalid type for the variable actionPerformed
Syntax error on token "(", ; expected
Syntax error on token ")", ; expected
I don't understand what I'm doing wrong at this point. I have my GUI class implementing ActionListener. Any help would be greatly appreciated. Thanks!
GUI Class:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Enumeration;
public class GUI extends JFrame implements ActionListener {
// RadioButtons & ButtonGroup
private JRadioButton jrbStudent = new JRadioButton("Student");
private JRadioButton jrbGraduate = new JRadioButton("Graduate Student");
private ButtonGroup group = new ButtonGroup();
// JTextFields
private JTextField name = new JTextField(20);
private JTextField address = new JTextField(20);
private JTextField balance = new JTextField(20);
private JTextField major = new JTextField(20);
// Submit Button
private JButton jbtSubmit = new JButton("Submit");
// echoStudent output area
private JTextArea echoStudent = new JTextArea();
// Specific Buttons
private JButton printStudentNames = new JButton("Print Student's Names");
private JButton printGradStudentNames = new JButton(
"Print Graduate Student's Names");
private JButton calcBalance = new JButton(
"Calculate Average Balance of All Students");
private JButton compSciMajor = new JButton(
"Displays Computer Science Major Students");
// ScrollPane
private JScrollPane scrollPane = new JScrollPane(echoStudent);
private Manager m1 = new Manager();
public GUI () {
super("College Admission Information Interface");
// Creates panel P1 and adds the components
JPanel p1 = new JPanel(new GridLayout(9, 1, 10, 10));
p1.add(new JLabel("Name: "));
p1.add(name);
p1.add(new JLabel("Address: "));
p1.add(address);
p1.add(new JLabel("Balance: "));
p1.add(balance);
p1.add(new JLabel("Major: "));
p1.add(major);
p1.add(jrbStudent);
p1.add(jrbGraduate);
p1.add(new JLabel("Submit Button: "));
p1.add(jbtSubmit);
p1.add(printStudentNames);
p1.add(printGradStudentNames);
p1.add(calcBalance);
p1.add(compSciMajor);
p1.add(new JLabel("Submitted Text: "));
// Creates a radio-button group to group both buttons
group.add(jrbStudent);
group.add(jrbGraduate);
// Registers listeners
jrbStudent.addActionListener(this);
jrbGraduate.addActionListener(this);
jbtSubmit.addActionListener(this);
printStudentNames.addActionListener(this);
printGradStudentNames.addActionListener(this);
calcBalance.addActionListener(this);
compSciMajor.addActionListener(this);
// GUI settings
setTitle("Information Interface");
setSize(1200, 900);
setLocationRelativeTo(null); // Center the frame
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
// Adds the panel and text area to the frame
add(p1);
p1.add(echoStudent);
echoStudent.setEditable(false);
public void actionPerformed(ActionEvent event) {
if(event.getSource() == jbtSubmit) {
if(jrbStudent.isSelected()) {
Student s1 = Manager.instance().createStudent(name.getText(),
address.getText(), major.getText(),
Double.parseDouble(balance.getText()));
echoStudent.append("\n\nCreated Student: " + s1.toString());
}
else if(jrbGraduate.isSelected()) {
GradStudent g1 = Manager.instance().createGradStudent(name.getText(),
address.getText(), major.getText(),
Double.parseDouble(balance.getText()));
echoStudent.append("\n\nCreated Graduate Student: " + g1.toString());
}
}
else if(event.getSource() == printStudentNames) {
echoStudent.append(Manager.instance().listStudents());
}
else if(event.getSource() == printGradStudentNames) {
echoStudent.append(Manager.instance().listGrads());
}
else if(event.getSource() == calcBalance) {
echoStudent.append(Manager.instance().aveBalance());
}
else if(event.getSource() == compSciMajor) {
echoStudent.append(Manager.instance().listCsci());
}
}
}
public static void main(String[] args) {
new GUI();
}
}
You need to move your actionPerformed declaration outside of your constructor.
Currently you have this:
public class GUI extends JFrame implements ActionListener {
...
public GUI() {
...
public void actionPerformed(ActionEvent event) {
...
}
}
}
It needs to be like:
public class GUI extends JFrame implements ActionListener {
...
public GUI() {
...
}
public void actionPerformed(ActionEvent event) {
...
}
}
I have not tested your code but that is the first obvious problem.
I'd like to create a wizard like app in java. I came to a point where I draw a frame with 3 different layouts, each containing a label and a 'next' button, but when I click the button on my first frame I see an empty frame instead.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class MainFrame extends JFrame implements ActionListener{
private BorderLayout layout;
public MainFrame() {
super("MyWizz");
setSize(500, 500);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setLocationRelativeTo(null);
}
protected ImageIcon createImageIcon(String path,
String description) {
java.net.URL imgURL = getClass().getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
public void DrawFirstPage(){
layout = new BorderLayout();
setLayout(layout);
ImageIcon icon = createImageIcon("/images/icon.png","desc");
add(new JLabel("text",icon,JLabel.CENTER),BorderLayout.CENTER);
JButton bn = new JButton("next");
add(bn,BorderLayout.PAGE_END);
setVisible(true);
bn.setActionCommand("goto2");
bn.addActionListener(this);
revalidate();
}
public void DrawBrowsePage(){
System.out.println(getContentPane().getComponentCount());
getContentPane().removeAll();
System.out.println(getContentPane().getComponentCount());
repaint();
revalidate();
layout = new BorderLayout();
JButton bn = new JButton("next");
add(new JLabel("text",JLabel.CENTER),BorderLayout.CENTER);
add(bn,BorderLayout.PAGE_END);
bn.setActionCommand("goto3");
bn.addActionListener(this);
setLayout(layout);
setVisible(true);
System.out.println(getContentPane().getComponentCount());
repaint();
revalidate();
}
public void DrawLoadingPage(){
getContentPane().removeAll();
repaint();
revalidate();
layout = new BorderLayout();
setLayout(layout);
setVisible(true);
JButton bn = new JButton("next");
add(new JLabel("text",JLabel.CENTER),BorderLayout.CENTER);
add(bn,BorderLayout.PAGE_END);
repaint();
revalidate();
}
public void actionPerformed(ActionEvent e) {
if("goto2".equals(e.getActionCommand())) DrawBrowsePage();
if("goto3".equals(e.getActionCommand())) DrawLoadingPage();
}
}
From those few System.out.println functions, I can see, that DrawBrowsePage() executes, old elements are removed and new ones added. Would apprecieate any help you can spare.
FAQ: No. I can't use jwizz.
Instead of remove and add and then revalidate()/repaint(), you can simply use a CardLayout.
Short explanation of what I mean :
Simply create a stack or list (using Collections or Array) of JPanels, each representing different views. At the click of the next button, simply show the next JPanel by using cardLayout.show(container, "StringNameOfThePanelYouWantToShow"). More info can be found on CardLayout API, A small example for help, to see it functioning :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* Here we are first declaring our class that will act as the
* base for other panels or in other terms the base for CardLayout.
*/
public class CardLayoutTest {
private static final String CARD_JBUTTON = "Card JButton";
private static final String CARD_JTEXTFIELD = "Card JTextField";
private static final String CARD_JRADIOBUTTON = "Card JRadioButton";
private static void createAndShowGUI() {
JFrame frame = new JFrame("Card Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// This JPanel is the base for CardLayout for other JPanels.
final JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout(20, 20));
/* Here we be making objects of the Window Series classes
* so that, each one of them can be added to the JPanel
* having CardLayout.
*/
Window1 win1 = new Window1();
contentPane.add(win1, CARD_JBUTTON);
Window2 win2 = new Window2();
contentPane.add(win2, CARD_JTEXTFIELD);
Window3 win3 = new Window3();
contentPane.add(win3, CARD_JRADIOBUTTON);
/* We need two JButtons to go to the next Card
* or come back to the previous Card, as and when
* desired by the User.
*/
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("PREVIOUS");
previousButton.setBackground(Color.BLACK);
previousButton.setForeground(Color.WHITE);
final JButton nextButton = new JButton("NEXT");
nextButton.setBackground(Color.RED);
nextButton.setForeground(Color.WHITE);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
/* Adding the ActionListeners to the JButton,
* so that the user can see the next Card or
* come back to the previous Card, as desired.
*/
previousButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.previous(contentPane);
}
});
nextButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
// Adding the contentPane (JPanel) and buttonPanel to JFrame.
frame.add(contentPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
createAndShowGUI();
}
});
}
}
class Window1 extends JPanel {
/*
* Here this is our first Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JButtons.
*/
private ActionListener action;
public Window1() {
init();
}
private void init() {
final JButton clickButton = new JButton("CLICK ME");
final JButton dontClickButton = new JButton("DON\'T CLICK ME");
action = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == clickButton) {
JOptionPane.showMessageDialog(null, "Hello there dude!"
, "Right Button", JOptionPane.INFORMATION_MESSAGE);
} else if (ae.getSource() == dontClickButton) {
JOptionPane.showMessageDialog(null, "I told you not to click me!"
, "Wrong Button", JOptionPane.PLAIN_MESSAGE);
}
}
};
clickButton.addActionListener(action);
dontClickButton.addActionListener(action);
add(clickButton);
add(dontClickButton);
}
}
class Window2 extends JPanel implements ActionListener {
/*
* Here this is our second Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of a JLabel and a JTextField
* with GridLayout.
*/
private JTextField textField;
public Window2() {
init();
}
private void init() {
setLayout(new GridLayout(1, 2));
JLabel userLabel = new JLabel("Your Name : ");
textField = new JTextField();
textField.addActionListener(this);
add(userLabel);
add(textField);
}
#Override
public void actionPerformed(ActionEvent e) {
if (textField.getDocument().getLength() > 0)
JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
, "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
}
}
class Window3 extends JPanel {
/*
* Here this is our third Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JLabels and two JCheckBox
* with GridLayout.
*/
private ActionListener state;
public Window3() {
init();
}
public void init() {
setLayout(new GridLayout(2, 2));
JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
final JCheckBox maleBox = new JCheckBox();
JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
final JCheckBox femaleBox = new JCheckBox();
state = new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
if (maleBox == (JCheckBox) ae.getSource()) {
femaleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Male"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
} else if (femaleBox == (JCheckBox) ae.getSource()) {
maleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Female"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
}
};
maleBox.addActionListener(state);
femaleBox.addActionListener(state);
add(maleLabel);
add(maleBox);
add(femaleLabel);
add(femaleBox);
}
}
I've got 3 windows in 3 separate classes and I would like to use cardLayout so that when you click the next button, the next window will appear. How do I add JPanels containing different elements to one cardLayout? This is the first window: (the only difference is the background though - but it represents the idea of how I got it actually)
public class Window1 extends JPanel implements ActionListener {
static CardLayout cardLayout = new CardLayout();
public Window1() {
init();
}
private void init() {
JPanel jp = new JPanel(new BorderLayout());
JPanel jp2 = new Window2();
//JPanel jp3 = new Window3();
JLabel textLabel = new JLabel("Window1");
jp.setBackground(Color.GREEN);
jp.add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
jp.add(nextButton, BorderLayout.EAST);
setLayout(cardLayout);
add(jp, "string");
add(jp2, "string");
//add(jp3, "string");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window
cardLayout.next(this);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("test");
frame.getContentPane().setLayout(Window1.cardLayout);
frame.getContentPane().add(new Window1(), "Center");
frame.getContentPane().add(new Window2(), "Center");
frame.getContentPane().add(new Window3(), "Center");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The second window:
public class Window2 extends JPanel implements ActionListener {
//static CardLayout cardLayout = new CardLayout();
public Window2() {
init();
}
private void init() {
setLayout(new BorderLayout());
JLabel textLabel = new JLabel("Window2");
setBackground(Color.RED);
add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
add(nextButton, BorderLayout.EAST);
//setLayout(cardLayout);
//JPanel jp3 = new Window3();
//add(jp3, "string");
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window??
System.out.println("window2");
Window1.cardLayout.next(this);
}
}
}
And the last one:
public class Window3 extends JPanel implements ActionListener {
public Window3() {
init();
}
private void init() {
setLayout(new BorderLayout());
JLabel textLabel = new JLabel("Window3");
setBackground(Color.BLUE);
add(textLabel, BorderLayout.CENTER);
JButton nextButton = new JButton("NEXT");
nextButton.setActionCommand("next");
nextButton.addActionListener(this);
add(nextButton, BorderLayout.EAST);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equalsIgnoreCase("next")) {
// go to the next window
// Window1.cardLayout.next(this);
}
}
}
I had made a small program, hopefully the comments written in the program, might be able to guide you, to understand how to use CardLayout.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/* Here we are first declaring our class that will act as the
* base for other panels or in other terms the base for CardLayout.
*/
public class CardLayoutTest
{
private static final String CARD_JBUTTON = "Card JButton";
private static final String CARD_JTEXTFIELD = "Card JTextField";
private static final String CARD_JRADIOBUTTON = "Card JRadioButton";
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Card Layout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
// This JPanel is the base for CardLayout for other JPanels.
final JPanel contentPane = new JPanel();
contentPane.setLayout(new CardLayout(20, 20));
/* Here we be making objects of the Window Series classes
* so that, each one of them can be added to the JPanel
* having CardLayout.
*/
Window1 win1 = new Window1();
contentPane.add(win1, CARD_JBUTTON);
Window2 win2 = new Window2();
contentPane.add(win2, CARD_JTEXTFIELD);
Window3 win3 = new Window3();
contentPane.add(win3, CARD_JRADIOBUTTON);
/* We need two JButtons to go to the next Card
* or come back to the previous Card, as and when
* desired by the User.
*/
JPanel buttonPanel = new JPanel();
final JButton previousButton = new JButton("PREVIOUS");
previousButton.setBackground(Color.BLACK);
previousButton.setForeground(Color.WHITE);
final JButton nextButton = new JButton("NEXT");
nextButton.setBackground(Color.RED);
nextButton.setForeground(Color.WHITE);
buttonPanel.add(previousButton);
buttonPanel.add(nextButton);
/* Adding the ActionListeners to the JButton,
* so that the user can see the next Card or
* come back to the previous Card, as desired.
*/
previousButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.previous(contentPane);
}
});
nextButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
CardLayout cardLayout = (CardLayout) contentPane.getLayout();
cardLayout.next(contentPane);
}
});
// Adding the contentPane (JPanel) and buttonPanel to JFrame.
frame.add(contentPane, BorderLayout.CENTER);
frame.add(buttonPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
class Window1 extends JPanel
{
/*
* Here this is our first Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JButtons.
*/
private ActionListener action;
public Window1()
{
init();
}
private void init()
{
final JButton clickButton = new JButton("CLICK ME");
final JButton dontClickButton = new JButton("DON\'T CLICK ME");
action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == clickButton)
{
JOptionPane.showMessageDialog(null, "Hello there dude!"
, "Right Button", JOptionPane.INFORMATION_MESSAGE);
}
else if (ae.getSource() == dontClickButton)
{
JOptionPane.showMessageDialog(null, "I told you not to click me!"
, "Wrong Button", JOptionPane.PLAIN_MESSAGE);
}
}
};
clickButton.addActionListener(action);
dontClickButton.addActionListener(action);
add(clickButton);
add(dontClickButton);
}
}
class Window2 extends JPanel implements ActionListener
{
/*
* Here this is our second Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of a JLabel and a JTextField
* with GridLayout.
*/
private JTextField textField;
public Window2()
{
init();
}
private void init()
{
setLayout(new GridLayout(1, 2));
JLabel userLabel = new JLabel("Your Name : ");
textField = new JTextField();
textField.addActionListener(this);
add(userLabel);
add(textField);
}
public void actionPerformed(ActionEvent e)
{
if (textField.getDocument().getLength() > 0)
JOptionPane.showMessageDialog(null, "Your Name is : " + textField.getText()
, "User\'s Name : ", JOptionPane.QUESTION_MESSAGE);
}
}
class Window3 extends JPanel
{
/*
* Here this is our third Card of CardLayout, which will
* be added to the contentPane object of JPanel, which
* has the LayoutManager set to CardLayout.
* This card consists of Two JLabels and two JCheckBox
* with GridLayout.
*/
private ActionListener state;
public Window3()
{
init();
}
public void init()
{
setLayout(new GridLayout(2, 2));
JLabel maleLabel = new JLabel("MALE", JLabel.CENTER);
final JCheckBox maleBox = new JCheckBox();
JLabel femaleLabel = new JLabel("FEMALE", JLabel.CENTER);
final JCheckBox femaleBox = new JCheckBox();
state = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (maleBox == (JCheckBox) ae.getSource())
{
femaleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Male"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
else if (femaleBox == (JCheckBox) ae.getSource())
{
maleBox.setSelected(false);
JOptionPane.showMessageDialog(null, "Congrats you are a Female"
, "Gender : ", JOptionPane.INFORMATION_MESSAGE);
}
}
};
maleBox.addActionListener(state);
femaleBox.addActionListener(state);
add(maleLabel);
add(maleBox);
add(femaleLabel);
add(femaleBox);
}
}
There are a couple things:
As your program can only run 1 main method, each class doesnt need one, but whichever you do run should create an instance of each panel you want to add to your CardLayout.
You also do not seem to add your Windows to your CardLayout at all. You could try the following (uncomplied). This would only be needed in one class:
private static void createAndShowGUI() {
JFrame frame = new JFrame("test");
frame.getContentPane().setLayout(Window1.cardLayout);
frame.getContentPane().add(new Window1());
frame.getContentPane().add(new Window2());
frame.getContentPane().add(new Window3());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 450);
frame.setVisible(true);
}
Further (and this might just be due to the simplicity of your example), I would just have 1 class, and it would take the name of the panel and the color of the background to the constructor. These could be passed into your init() method and your design would be somewhat streamlined.