Graphical User Interface and Main Method needs sorting - java

Im trying to sort my GUI so it will be able to use the methods from the other classes
in the JProject i also want to call it from the main.
The action listeners i had just learnt today so i need feedback on what to do there as well.
Any Ideas??
It would be a great help to get some insight on this!
(I am new to stack so my code posted in the explanation and in the actual script idk
why).
package test;
import javax.swing.JPanel;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import java.awt.Container;
import java.awt.SystemColor;
import java.awt.Color;
import javax.swing.JRadioButton;
import java.awt.Font;
import java.io.File;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.util.ArrayList;
import java.util.Scanner;
//Main Method
public class TextUI{
protected StormTracker var;
public TextUI(StormTracker var)
{
this.var=var;
}
public static void main(String[] args)
{
ShowGUI gui = new SHOWGUI();
while(gui.running)
if(gui.buttonPress == -1)
continue;
StormTracker obj = new StormTracker("config.txt");
WeatherAuthority wa = new WeatherAuthority("stormfile.txt");
int xcoord=0;
int ycoord=0;
TextUI uiobj = new TextUI(obj);
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("---------------------------------------------------------------");
System.out.println("Welcome to the StormTracker Application.");
System.out.println("Type the following commands to carry out the associated actions.");
System.out.println("\n");
System.out.println("----------------------{Commands Menu}---------------------
-------");
System.out.println("(1) NEW STORM x y");
System.out.println("(2) UPGRADE <name of storm>");
System.out.println("(3) DOWNGRADE <name of storm>");
System.out.println("(4) TICK");
System.out.println("(5) EXIT");
System.out.println("----------------------------------------------------------------");
String option=scan.nextLine();
if (option.substring(0,4).equals("TICK")){
obj.tick();
continue;
}
if (option.substring(0,9).equals("NEW STORM")){
if(option.length()==13){
xcoord = Integer.parseInt(option.substring(10,11));
ycoord = Integer.parseInt(option.substring(12,13));
}
else if (option.length()==14){
if (option.substring(11,12).equals(" ")){
xcoord = Integer.parseInt(option.substring(10,11));
ycoord = Integer.parseInt(option.substring(12,14));
}
else{
xcoord = Integer.parseInt(option.substring(10,12));
ycoord = Integer.parseInt(option.substring(13,14));
}
}
else if (option.length()==15){
xcoord = Integer.parseInt(option.substring(10,12));
ycoord = Integer.parseInt(option.substring(13,15));
}
obj.newstorm(xcoord,ycoord);
}
if (option.substring(0,7).equals("UPGRADE")){
String storm_name = option.substring(8);
obj.upgrade(storm_name);
}
if (option.substring(0,9).equals("DOWNGRADE")){
String storm_name = option.substring(10);
obj.downgrade(storm_name);
}
}
}
//GUI Class
public ShowGUI extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
System.out.println("GUI Successfully Loaded");
createAndShow();
setToolTipText("Select Storm");
setBackground(Color.WHITE);
}
private void createAndShow(){
JPanel mainFrame = new JPanel("Storm Tracker V1.0");
}
private void addComponents(Container pane){
pane.setLayout(null);
JLabel lblStormTrackerV = DefaultComponentFactory.getInstance().createTitle("
Storm Tracker V1.0");
lblStormTrackerV.setBackground(SystemColor.activeCaption);
lblStormTrackerV.setFont(new Font("Calibri", Font.PLAIN, 22));
lblStormTrackerV.setBounds(162, 11, 430, 41);
add(lblStormTrackerV);
JRadioButton rdbtnMap = new JRadioButton("MAP");
rdbtnMap.setBounds(283, 59, 72, 23);
add(rdbtnMap);
JRadioButton rdbtnBulletin = new JRadioButton("Bulletin");
rdbtnBulletin.setBounds(406, 60, 72, 23);
add(rdbtnBulletin);
textField = new JTextField();
textField.setBounds(61, 103, 72, 20);
add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(61, 134, 72, 20);
add(textField_1);
textField_1.setColumns(10);
JTextPane txtpnX = new JTextPane();
txtpnX.setText("X:");
txtpnX.setBounds(23, 103, 28, 20);
add(txtpnX);
JTextPane txtpnY = new JTextPane();
txtpnY.setText("Y:");
txtpnY.setBounds(23, 134, 12, 20);
add(txtpnY);
JTextPane txtpnSpeed = new JTextPane();
txtpnSpeed.setText("Speed:");
txtpnSpeed.setBounds(10, 165, 41, 20);
add(txtpnSpeed);
textField_2 = new JTextField();
textField_2.setBounds(61, 165, 72, 20);
add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(61, 196, 72, 20);
add(textField_3);
textField_3.setColumns(10);
JTextPane txtpnSpeed_1 = new JTextPane();
txtpnSpeed_1.setText("Speed:");
txtpnSpeed_1.setBounds(10, 196, 41, 20);
add(txtpnSpeed_1);
JTextPane txtpnBearing = new JTextPane();
txtpnBearing.setText("Bearing:");
txtpnBearing.setBounds(10, 227, 41, 20);
add(txtpnBearing);
textField_4 = new JTextField();
textField_4.setBounds(61, 227, 72, 20);
add(textField_4);
textField_4.setColumns(10);
File input = new File(new
FileReader("C:\\Users\\620086793\\Documents\\test\\src\\test"));
List<String> strings = new ArrayList<String>();
try {
String line = null;
while (( line = input.readLine()) != null){
strings.add(line);
}
}
catch (FileNotFoundException e) {
System.err.println("Error, file " + "C:\\Users\\620086793\\Documents\\test\\src\\test" + " didn't exist.");
}
finally {
input.close();
}
String[] lineArray = strings.toArray(new String[]{});
JComboBox comboBox = new JComboBox(lineArray);
JComboBox comboBox = new JComboBox();
comboBox.setToolTipText("Select Storm");
comboBox.setEditable(true);
comboBox.setBounds(20, 60, 113, 20);
add(comboBox);
JButton btnUpdate = new JButton("UPDATE");
btnUpdate.setFont(new Font("Calibri", Font.BOLD, 11));
btnUpdate.setBounds(10, 273, 110, 28);
add(btnUpdate);
JButton btnNewButton = new JButton("NEW STORM\r\n");
btnNewButton.setFont(new Font("Calibri", Font.BOLD, 11));
btnNewButton.setBounds(10, 312, 110, 28);
add(btnNewButton);
addActionListeners();
pane.add(rdbtnMap);
pane.add(rdbtnBulletin);
pane.add(textField);
pane.add(textField_1);
pane.add(textField_2);
pane.add(textField_3);
pane.add(textField_4);
pane.add(comboBox);
pane.add(btnUpdate);
pane.add(btnNewButton);
}
//Im not sure how to properly add the action listeners i first learned about them
today.
private void addActionListeners(){
rdbtnMap.addActionListener(new ActionListener){
public void actionPerformed(ActionEvent ){
}
}
rdbtnBulletin.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_2.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_3.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
textField_4.addActionListener(){
public void actionPerformed(ActionEvent ){
stormTracker.tick = textField_4;
}
}
comboBox.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
btnUpdate.addActionListener(){
public void actionPerformed(ActionEvent ){
obj.upgrade() = btnUpdate;
return btnUpdate;
}
}
btnNewButton.addActionListener(){
public void actionPerformed(ActionEvent ){
}
}
}

Related

how to print text from a JtextField by clicking a JButton

I'm creating a little program for my self to learn Java now I did make a GUI with 12 JTextFields and now I want to use the text that's in the text fields in another class where I print them out.
When I press the button class print is successful, summand I get the last System.out.println("Test if class is triggerd") in my console but I don't get the JTextFields output in the console
Class Frame1
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Frame1 {
public JFrame frmCratingRecipieGenerator;
public JTextField txtSlot;
public JTextField textField_1;
public JTextField textField_2;
public JTextField textField_3;
public JTextField textField_4;
public JTextField textField_5;
public JTextField textField_6;
public JTextField textField_7;
public JTextField textField_8;
public JTextField textField_9;
public JTextField textField_10;
public JTextField textField_11;
public JLabel lblNewLabel;
public String slot1;
public String slot2;
public String slot3;
public String slot4;
public String slot5;
public String slot6;
public String slot7;
public String slot8;
public String slot9;
public String output;
public String output_count;
public String file_num;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frmCratingRecipieGenerator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frmCratingRecipieGenerator = new JFrame();
frmCratingRecipieGenerator.setResizable(false);
frmCratingRecipieGenerator.setTitle("Crating Recipie Generator Alpha 1");
frmCratingRecipieGenerator.getContentPane().setBackground(new Color(192, 192, 192));
frmCratingRecipieGenerator.setBounds(100, 100, 452, 248);
frmCratingRecipieGenerator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCratingRecipieGenerator.getContentPane().setLayout(null);
txtSlot = new JTextField();
txtSlot.setBounds(10, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(txtSlot);
txtSlot.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(106, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(202, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(10, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(106, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(202, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_5);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(10, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_6);
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(106, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_7);
textField_7.setColumns(10);
textField_8 = new JTextField();
textField_8.setBounds(202, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setBounds(351, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_9);
textField_9.setColumns(10);
textField_10 = new JTextField();
textField_10.setBounds(351, 81, 46, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_10);
textField_10.setColumns(10);
textField_11 = new JTextField();
textField_11.setText("1");
textField_11.setBounds(351, 50, 46, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_11);
textField_11.setColumns(10);
JLabel lblFileId = new JLabel("File ID");
lblFileId.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblFileId.setHorizontalAlignment(SwingConstants.LEFT);
lblFileId.setBounds(298, 82, 86, 14);
frmCratingRecipieGenerator.getContentPane().add(lblFileId);
JLabel lblOutput = new JLabel("Output");
lblOutput.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblOutput.setBounds(298, 20, 58, 14);
frmCratingRecipieGenerator.getContentPane().add(lblOutput);
JLabel lblCount = new JLabel("Count");
lblCount.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCount.setBounds(298, 51, 52, 14);
frmCratingRecipieGenerator.getContentPane().add(lblCount);
JLabel lblThisJavaProgram = new JLabel("This Java program is coded by Jason");
lblThisJavaProgram.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblThisJavaProgram.setBounds(10, 179, 270, 20);
frmCratingRecipieGenerator.getContentPane().add(lblThisJavaProgram);
JLabel TestLabel = new JLabel("");
TestLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
TestLabel.setBounds(391, 179, 46, 19);
frmCratingRecipieGenerator.getContentPane().add(TestLabel);
JButton btnNewButton = new JButton("Generate the McFunction File's");
btnNewButton.setBounds(10, 108, 427, 60);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestLabel.setText(textField_10.getText());
print name = new print();
name.Print();
}
});
frmCratingRecipieGenerator.getContentPane().add(btnNewButton);
lblNewLabel = new JLabel("Last File num");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(287, 182, 110, 14);
frmCratingRecipieGenerator.getContentPane().add(lblNewLabel);
this.slot1 = txtSlot.getText();
this.slot2 = textField_1.getText();
this.slot3 = textField_2.getText();;
this.slot4 = textField_3.getText();;
this.slot5 = textField_4.getText();;
this.slot6 = textField_5.getText();;
this.slot7 = textField_6.getText();;
this.slot8 = textField_7.getText();;
this.slot9 = textField_8.getText();;
this.output = textField_9.getText();
this.output_count = textField_11.getText();
this.file_num = textField_10.getText();
}
}
Class print
package example;
public class print {
public void Print() {
Frame1 slots = new Frame1();
String slot1 = slots.slot1;
String slot2 = slots.slot2;
String slot3 = slots.slot3;
String slot4 = slots.slot4;
String slot5 = slots.slot5;
String slot6 = slots.slot6;
String slot7 = slots.slot7;
String slot8 = slots.slot8;
String slot9 = slots.slot9;
String output = slots.output;
String output_count = slots.output_count;
String file_num = slots.file_num;
System.out.println(slot1);
System.out.println(slot2);
System.out.println(slot3);
System.out.println(slot4);
System.out.println(slot5);
System.out.println(slot6);
System.out.println(slot7);
System.out.println(slot8);
System.out.println(slot9);
System.out.println(output);
System.out.println(output_count);
System.out.println(file_num);
System.out.println("Test if class is triggerd");
}
}
This is happening because you create on the print class a new Object of frame1 so the variables by default are null. You can add on the Print method a list parameter that stores all the Strings in that list.
public void Print(List<String> slots) {
System.out.println(slots.get(0));
System.out.println(slots.get(1));
System.out.println(slots.get(2));
System.out.println(slots.get(3));
System.out.println(slots.get(4));
System.out.println(slots.get(5));
System.out.println(slots.get(6));
System.out.println(slots.get(7));
System.out.println(slots.get(8));
System.out.println(slots.get(9));
System.out.println(slots.get(10));
System.out.println(slots.get(11));
}
and redefine before the method call all the variables and store them in the list.
You can learn some things about Java's lists here
If you are struggling finding the code I made it for you
slot1 = txtSlot.getText();
slot2 = textField_1.getText();
slot3 = textField_2.getText();;
slot4 = textField_3.getText();;
slot5 = textField_4.getText();;
slot6 = textField_5.getText();;
slot7 = textField_6.getText();;
slot8 = textField_7.getText();;
slot9 = textField_8.getText();;
output = textField_9.getText();
List<String> slots = new ArrayList<String>();
slots.add(slot1);
slots.add(slot2);
slots.add(slot3);
slots.add(slot4);
slots.add(slot5);
slots.add(slot6);
slots.add(slot7);
slots.add(slot8);
slots.add(slot9);
slots.add(output);
slots.add(output_count);
slots.add(file_num);
output_count = textField_11.getText();
file_num = textField_10.getText();
TestLabel.setText(textField_10.getText());
print name = new print();
name.Print(slots);
and Print class:
public void Print(List<String> slots) {
System.out.println(slots.get(0));
System.out.println(slots.get(1));
System.out.println(slots.get(2));
System.out.println(slots.get(3));
System.out.println(slots.get(4));
System.out.println(slots.get(5));
System.out.println(slots.get(6));
System.out.println(slots.get(7));
System.out.println(slots.get(8));
System.out.println(slots.get(9));
System.out.println(slots.get(10));
System.out.println(slots.get(11));
System.out.println("Test if class is triggerd");
}

Syntax Error insert "}" to complete class block

I am making a program for my friend. I ran into an error while trying to create a write and save text into a .txt file. I should note this is the first time I have ever created something in Java Swing.
The error is marked with an asterisk.
package com.laganstoop.me;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
// Created by: Laganstoop (David L. Perez)
// Do Not Distribute!
public class Main implements ActionListener {
private JFrame frame;
public static String infoBox(String infoMessage, String titleBar)
{
JOptionPane.showMessageDialog(null, infoMessage, "" + titleBar, JOptionPane.INFORMATION_MESSAGE);
return "";
}
public static void errorMessage(String errorMessage, String titleBar)
{
JOptionPane.showMessageDialog(null, errorMessage, "" + titleBar, JOptionPane.INFORMATION_MESSAGE);
}
public static final int HEIGHT = 800;
public static final int WIDTH = 600;
public static final int nHEIGHT = 400;
public static final int nWIDTH = 300;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(HEIGHT, WIDTH);
JButton btnNewButton = new JButton("Click To Add New");
btnNewButton.setBounds(10, 11, 132, 23);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
frame.getContentPane().setLayout(null);
frame.getContentPane().add(btnNewButton);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 764, 181);
panel.setBackground(Color.WHITE);
panel.setForeground(Color.WHITE);
frame.getContentPane().add(panel);
panel.setLayout(null);
panel.hide();
JLabel lblName = new JLabel("Name:");
lblName.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblName.setBounds(10, 11, 46, 14);
panel.add(lblName);
JLabel lblDate = new JLabel("Date:");
lblDate.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblDate.setBounds(10, 36, 46, 14);
panel.add(lblDate);
JLabel lblLocation = new JLabel("Time:\r\n");
lblLocation.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblLocation.setBounds(10, 61, 60, 14);
panel.add(lblLocation);
JLabel lblLocation_1 = new JLabel("Location:");
lblLocation_1.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblLocation_1.setBounds(10, 86, 60, 14);
panel.add(lblLocation_1);
JLabel lblRe = new JLabel("Referee");
lblRe.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblRe.setBounds(10, 111, 60, 14);
panel.add(lblRe);
textField = new JTextField();
textField.setBounds(76, 8, 127, 20);
panel.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(76, 33, 127, 20);
panel.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(76, 58, 127, 20);
panel.add(textField_2);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(76, 86, 127, 20);
panel.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(76, 108, 127, 20);
panel.add(textField_4);
JCheckBox checkBox = new JCheckBox("");
checkBox.setBounds(209, 7, 21, 23);
panel.add(checkBox);
JCheckBox checkBox_1 = new JCheckBox("");
checkBox_1.setBounds(209, 32, 21, 23);
panel.add(checkBox_1);
JCheckBox checkBox_2 = new JCheckBox("");
checkBox_2.setBounds(209, 57, 21, 23);
panel.add(checkBox_2);
JCheckBox checkBox_3 = new JCheckBox("");
checkBox_3.setBounds(209, 82, 21, 23);
panel.add(checkBox_3);
JCheckBox checkBox_4 = new JCheckBox("");
checkBox_4.setBounds(209, 107, 21, 23);
panel.add(checkBox_4);
JPanel panel_2 = new JPanel();
panel_2.setBounds(20, 290, 358, 210);
panel_2.setLayout(null);
JButton btnNewNote = new JButton("New Note");
btnNewNote.setFont(new Font("Yu Gothic", Font.BOLD, 16));
btnNewNote.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame noteFrame = new JFrame();
noteFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
noteFrame.setLocationRelativeTo(panel);
noteFrame.pack();
noteFrame.setSize(nHEIGHT, nWIDTH);
noteFrame.getContentPane().add(panel_2);
noteFrame.setVisible(true);
}
});
btnNewNote.setBounds(293, 16, 461, 151);
panel.add(btnNewNote);
JLabel lblNotes = new JLabel("Notes:");
lblNotes.setBounds(10, 0, 46, 23);
panel_2.add(lblNotes);
lblNotes.setFont(new Font("Yu Gothic", Font.BOLD, 13));
JButton btnSaveCreate = new JButton("Create & Save");
btnSaveCreate.setBounds(237, 177, 131, 23);
panel_2.add(btnSaveCreate);
JCheckBox checkBox_5 = new JCheckBox("");
checkBox_5.setBounds(210, 177, 21, 23);
panel_2.add(checkBox_5);
btnSaveCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String NAME = textField.getText();
String DATE = textField_1.getText();
String TIME = textField_2.getText();
String LOCATION = textField_3.getText();
String Referee = textField_4.getText();
if(checkBox.isSelected() && checkBox_1.isSelected() && checkBox_2.isSelected() && checkBox_2.isSelected() && checkBox_3.isSelected() && checkBox_4.isSelected() && checkBox_5.isSelected())
{
infoBox("Met with " + NAME + " on " + DATE + " #" + TIME + ", and went to " + LOCATION + ", then I was introduced by " + Referee, "Paragraph");
}
else {
errorMessage("Error: Please validate entries!", "Error!");
}
}
});
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.hide();
panel.setVisible(true);
}
});
try {
String content = textField_5.getText();
File file = new File("/users/David/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content); //step2: write it
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace(); **** *****
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Any Help is greatly appreciated!
You just forgot to add the } in this statement:
catch (IOException e) {
e.printStackTrace();
}
Just as Andrew Thompson said: simple indentation would have made this error a lot easier to spot if done properly, and should be used as common practice.

Java – How do I use counter data for analytics?

I have a JFrame with three tabs: Home, Statistics, and About.
On the Home tab, I have a counter that tracks how many times you click your left or right arrow, and it updates a counter (I use the KeyListener method):
On the Statistics tab, I want to somehow import these Left and Right values that I generate from the counter.
But, I don't know how to. Can someone help me out here?
Edit:
Here is the code for the Home tab (Left/Right counter)
import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class HomePanel extends JPanel implements KeyListener {
// Variables
private int counter = 0;
private int counter2 = 0;
private JLabel counterLabel;
private JLabel counterLabel2;
private JButton countUpButton;
private JButton countDownButton;
// Panel Elements
public HomePanel() {
countUpButton = new JButton ("Left");
countUpButton.setBounds(195, 121, 75, 29);
countUpButton.addKeyListener(this);
countDownButton = new JButton ("Right");
countDownButton.setBounds(195, 162, 77, 29);
countDownButton.addKeyListener(this);
counterLabel = new JLabel("" + counter);
counterLabel.setBounds(282, 126, 34, 16);
counterLabel2 = new JLabel("" + counter2);
counterLabel2.setBounds(282, 167, 34, 16);
setLayout(null);
add (countUpButton);
add (counterLabel);
add (countDownButton);
add (counterLabel2);
}
// Key Listener
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
// Ensures counter updates once per stroke
#Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
counter++;
counterLabel.setText("" + counter);
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
counter2++;
counterLabel2.setText("" + counter2);
}
}
#Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
And here is the code for the Statistics tab:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class StatsPanel extends JPanel {
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
public StatsPanel() {
setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(41, 43, 371, 234);
add(panel);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("LEFT:");
lblNewLabel.setBounds(115, 39, 33, 16);
panel.add(lblNewLabel);
JLabel lblRight = new JLabel("RIGHT:");
lblRight.setBounds(105, 67, 43, 16);
panel.add(lblRight);
JLabel lblNewLabel_2 = new JLabel("TOTAL:");
lblNewLabel_2.setBounds(102, 95, 46, 16);
panel.add(lblNewLabel_2);
JLabel lblNewLabel_3 = new JLabel("% LEFT:");
lblNewLabel_3.setBounds(102, 123, 46, 16);
panel.add(lblNewLabel_3);
JLabel lblNewLabel_4 = new JLabel("% RIGHT: ");
lblNewLabel_4.setBounds(91, 151, 60, 16);
panel.add(lblNewLabel_4);
textField_2 = new JTextField();
textField_2.setBounds(160, 33, 134, 28);
panel.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(160, 61, 134, 28);
panel.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(160, 89, 134, 28);
panel.add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(160, 117, 134, 28);
panel.add(textField_5);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(160, 145, 134, 28);
panel.add(textField_6);
textField_6.setColumns(10);
JButton btnCalculate = new JButton("Calculate");
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Variables
double num1, num2, ans, percEq1, percEq2, percLeft, percRight;
try {
num1 = Double.parseDouble(textField_2.getText());
num2 = Double.parseDouble(textField_3.getText());
ans = num1 + num2;
textField_4.setText(Double.toString(ans));
percEq1 = (num1/(num1+num2))*100;
percLeft = Math.round(percEq1);
textField_5.setText(Double.toString(percLeft));
percEq2 = (num2/(num1+num2))*100;
percRight = Math.round(percEq2);
textField_6.setText(Double.toString(percRight));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Please enter a valid number!");
}
}
});
btnCalculate.setBounds(79, 185, 117, 29);
panel.add(btnCalculate);
JButton btnRest = new JButton("Reset");
btnRest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textField_2.setText("");
textField_3.setText("");
textField_4.setText("");
textField_5.setText("");
textField_6.setText("");
}
});
btnRest.setBounds(197, 185, 117, 29);
panel.add(btnRest);
}
}
I suggest using variables in the parent container so every tab can access them or, from the statistics tab, ask the values to the parent frame and the parent asks to the home tab.
It's not a short code and you haven't attached yours, so I hope your get the idea.
In the main container you have something like this:
KeyPressStats statistics = new KeyPressStats();
HomePanel home = new HomePanel(statistics);
StatsPanel stats = new StatsPanel(statistics);
JTabbedPane tabbed = new JTabbedPane();
tabbed.addTab("Home", home);
tabbed.addTab("Stats", stats);
tabbed.setVisible(true);
KeyPressStats is as follows:
public class KeyPressStats {
private int leftCount = 0;
private int rightCount = 0;
public int getLeftCount() {
return leftCount;
}
public void setLeftCount(int leftCount) {
this.leftCount = leftCount;
}
public int getRightCount() {
return rightCount;
}
public void setRightCount(int rightCount) {
this.rightCount = rightCount;
}
public void incrementRight() {
rightCount++;
}
public void incrementLeft() {
leftCount++;
}
}
And the code in the Home and stats tabs will have these changes:
public class HomePanel extends JPanel implements KeyListener {
...
private KeyPressStats stats;
public HomePanel(KeyPressStats stats) {
this.stats = stats;
countUpButton = new JButton("Left");
countUpButton.setBounds(195, 121, 75, 29);
...
}
#Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
stats.incrementLeft();
counterLabel.setText("" + stats.getLeftCount());
} else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
stats.incrementRight();
counterLabel2.setText("" + stats.getRightCount());
}
}
public class StatsPanel extends JPanel {
...
public StatsPanel(KeyPressStats stats) {
...
textField_2 = new JTextField(""+stats.getLeftCount());
textField_2.setBounds(160, 33, 134, 28);
...
textField_3 = new JTextField(""+stats.getRightCount());
textField_3.setBounds(160, 61, 134, 28);
...
}
}

Toggle Java JPanels - each panel deifferent class

I'm a beginer in Java GUI, and i have an issue with trying to toggle between panel in other class.
My main Class "MyBoxGUI" has 2 Panels, "Login" and "UserMenu"
The default is "Login" panel, when pressing "OK" the window moved to "UserMenu" Panel.
So far so good while in the same class.
The issue is, when i set a new panel in new class and setvisibe that panel, i can't go back to "UserMenu" panel - (The actionlistener is working fine).
Can someone help me please?
MyBoxGUI Class
package main;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.CardLayout;
import java.awt.Component;
//import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Toolkit;
import GUIs.*;
//import ocsf.client.*;
public class MyBoxGUI extends JPanel
{
private Mtds mtd;
public JFrame frmMybox;
private static JTextField UserName;
private static JTextField port;
private static JTextField IP;
private static JPasswordField passwordField;
public final JPanel Login = new JPanel();
public final JPanel UserMenu = new JPanel();
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
MyBoxGUI window = new MyBoxGUI();
window.frmMybox.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MyBoxGUI()
{
mtd = new Mtds();
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frmMybox = new JFrame();
frmMybox.setFont(new Font("Tempus Sans ITC", Font.BOLD, 15));
// frmMybox.setIconImage(Toolkit.getDefaultToolkit().getImage(MyBoxGUI.class.getResource("/images/gift-ideas-gift-card-bridal-gift-baby-shower-gift-gift-box-groom-gift-christmas-gift-party-gift-gift-for-wedding-friend-gift-birthday-gift-baby-gift-good-gift-box-ideas-for-friend-necklace-gift-box.jpg")));
frmMybox.setBounds(100, 100, 800, 500);
frmMybox.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmMybox.getContentPane().setLayout(new CardLayout(0, 0));
frmMybox.setTitle("MyBox");
frmMybox.getContentPane().add(Login);
Login.setLayout(null);
JButton btnNewButton = new JButton("OK");
btnNewButton.addActionListener(new ActionListener()
{
#SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent arg0)
{
Boolean ok = false;
String UName, Pass, Prt, SIP;
UName = UserName.getText();
Pass = passwordField.getText();
Prt = port.getText();
SIP = IP.getText();
if( ( mtd.isUserValid(UName) && mtd.isPasswordValid(Pass) && mtd.isPortValid(Prt) && mtd.isIPValid(SIP))
&& ( !UName.equals("") && !Pass.equals("") && !Prt.equals("") && !SIP.equals("") ) )
ok = true;
else
{
if(!mtd.isUserValid(UName) || UName.equals(""))
JOptionPane.showMessageDialog(frmMybox,"User Name Can Contain Only The Following Characters: ( 0-9 , a-z , A-Z , _ )"
+ " ","Invalid characters",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isPasswordValid(Pass) || Pass.equals(""))
JOptionPane.showMessageDialog(frmMybox,"Invalid characters","Error Message",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isPortValid(Prt) || Prt.equals(""))
JOptionPane.showMessageDialog(frmMybox,"Port Can Contain Only Numbers","Invalid characters",JOptionPane.WARNING_MESSAGE);
else if(!mtd.isIPValid(SIP) || SIP.equals(""))
JOptionPane.showMessageDialog(frmMybox,"IP Address Can Contain Only Numbers seperated By Dots '.' ","Invalid characters",JOptionPane.WARNING_MESSAGE);
}
if(ok)
{
//LoginController user = new LoginController();
//chat= new ClientGUI(IP.getText(),port.getText());
//client = ClientGUI.getClient();
//msg=new Msg("login",user);
//client.handleMessageFromClientUI(msg);
setScreen(Login,UserMenu);
// frmMybox.getContentPane().add(UserMenu, "UserMenu");
// UserMenu.setVisible(true);
// Login.setVisible(false);
}
}
});
btnNewButton.setBounds(344, 313, 82, 48);
Login.add(btnNewButton);
JLabel lblWellcomeToMybox = new JLabel("Wellcome to MyBox");
lblWellcomeToMybox.setForeground(new Color(51, 204, 255));
lblWellcomeToMybox.setFont(new Font("Arial", Font.PLAIN, 36));
lblWellcomeToMybox.setBounds(224, 23, 327, 42);
Login.add(lblWellcomeToMybox);
JLabel lblUserName = new JLabel("User Name:");
lblUserName.setBounds(268, 109, 89, 14);
lblUserName.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblUserName);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(268, 139, 69, 14);
lblPassword.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblPassword);
JLabel lblServerPort = new JLabel("Server port:");
lblServerPort.setBounds(268, 197, 82, 14);
lblServerPort.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblServerPort);
JLabel lblServerIp = new JLabel("Server IP:");
lblServerIp.setBounds(266, 222, 71, 14);
lblServerIp.setFont(new Font("Arial", Font.PLAIN, 14));
Login.add(lblServerIp);
UserName = new JTextField();
UserName.setBounds(406, 107, 96, 20);
Login.add(UserName);
UserName.setColumns(10);
port = new JTextField();
port.setBounds(406, 195, 96, 20);
Login.add(port);
port.setColumns(10);
IP = new JTextField();
IP.setBounds(406, 220, 96, 20);
Login.add(IP);
IP.setColumns(10);
passwordField = new JPasswordField();
passwordField.setBounds(406, 137, 96, 20);
Login.add(passwordField);
frmMybox.getContentPane().add(UserMenu, "User Menu");
UserMenu.setLayout(null);
JButton LeaveGOI = new JButton("Back");
LeaveGOI.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
setScreen(UserMenu,Login);
}
});
LeaveGOI.setBounds(10, 66, 153, 23);
UserMenu.add(LeaveGOI);
JButton btnJoinAGoi = new JButton("Join a GOI");
btnJoinAGoi.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
}
});
btnJoinAGoi.setBounds(10, 100, 153, 23);
UserMenu.add(btnJoinAGoi);
JButton btnSearchForA = new JButton("Search for a GOI");
btnSearchForA.setBounds(10, 134, 153, 23);
UserMenu.add(btnSearchForA);
JButton btnCreateAGoi = new JButton("Create a GOI");
btnCreateAGoi.setBounds(10, 32, 153, 23);
UserMenu.add(btnCreateAGoi);
JButton btnFilesSharedWith = new JButton("Files shared with you");
btnFilesSharedWith.setBounds(271, 66, 153, 23);
UserMenu.add(btnFilesSharedWith);
JButton btnUploadAFile = new JButton("Upload a file");
btnUploadAFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
JPanel UpLoad = new UploadFile();
setScreen(UserMenu,UpLoad);
// frmMybox.getContentPane().add(UpLoad, "UpLoad");
// UserMenu.setVisible(false);
// UpLoad.setVisible(true);
}
});
btnUploadAFile.setBounds(271, 32, 153, 23);
UserMenu.add(btnUploadAFile);
JButton btnSignout = new JButton("Sign-Out");
btnSignout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int dialog=JOptionPane.showConfirmDialog(Login, getUserName()+", are you sure you wants to leave?", "Do you want to leave?", JOptionPane.YES_NO_OPTION);
if(dialog==0){
//client.quit();
}
}
});
btnSignout.setBounds(293, 227, 131, 23);
UserMenu.add(btnSignout);
JButton btnHelpme = new JButton("Help-Me");
btnHelpme.addActionListener(new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
JOptionPane.showMessageDialog(Login,"Hello "+ getUserName()+", wellcom to Mybox!\n"
+ "Inside GOIs buttons you can ask to join, leave and even create a GOI.\n"
+ "Inside Files buttons you can check and edit files people shared with you.\n"
+ "You can even become an uploader and share files you own.\n"
+ "We wish you good luck and have fun using MyBox!");
}
});
btnHelpme.setBounds(10, 227, 131, 23);
UserMenu.add(btnHelpme);
}
public static String getUserName(){
return UserName.getText();
}
#SuppressWarnings("deprecation")
public static String getPassword(){
return passwordField.getText();
}
public static String getIP(){
return IP.getText();
}
public static String getPort(){
return port.getText();
}
public void setScreen(JPanel fls, JPanel tru)
{
frmMybox.getContentPane().add(tru);
tru.setVisible(true);
fls.setVisible(false);
}
}
UploadFile Class
package GUIs;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import main.MyBoxGUI;
public class UploadFile extends MyBoxGUI {
private JTextField textField;
/**
* Create the panel.
*/
public static final JPanel UpLoad = new JPanel();
public UploadFile()
{
setBounds(100, 100, 800, 500);
setLayout(null);
JButton btnNewButton = new JButton("Back");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
setScreen(UpLoad,UserMenu);
System.out.print("A+++");
}
});
btnNewButton.setBounds(126, 145, 205, 30);
add(btnNewButton);
textField = new JTextField();
textField.setBounds(409, 150, 300, 20);
add(textField);
textField.setColumns(10);
JButton btnDone = new JButton("Done");
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnDone.setBounds(365, 223, 89, 30);
add(btnDone);
JButton btnHelpMe = new JButton("Help Me");
btnHelpMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
btnHelpMe.setBounds(188, 390, 122, 52);
add(btnHelpMe);
JButton SignOut = new JButton("Sign-Out");
SignOut.setBounds(501, 392, 122, 48);
add(SignOut);
}
}
public void setScreen(JPanel fls, JPanel tru)
{
frmMybox.getContentPane().add(tru);
tru.setVisible(true);
fls.setVisible(false);
frmMybox.getContentPane().revalidate();
frmMybox.getContentPane().repaint();
}
update your setScreen method as above.

JApplet issue stating class not found and class not found exception

I am having an issue that is giving me an error message and a blank applet is appearing. The error message says load:class Myapp.class not found and java.lang.ClassNotFoundException and then a list of lines saying # and then some other jargon.
If I run try to run the program as a Jcreator applet like I expected I get the prior stated error and if I run it in an application it just gives me the main method error.
Here is my program:
package MyPack;
import javax.swing.*;
import java.awt.event.*;
public class Myapp extends JApplet implements ItemListener{
JButton jbtnOne;
JButton jbtnTwo;
JLabel jlab;
JComboBox comboBox;
JComboBox comboBox_1;
JCheckBox chckbxPizza;
JCheckBox chckbxBurger;
JCheckBox chckbxSalad;
JLabel label_3;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
JTextArea txtrCommentsForSpecial;
JTextArea txtrCommentsForSpecial_2;
JTextArea txtrCommentsForSpecial_1;
JInternalFrame internalFrame;
public Myapp() {
}
public void init() {
setSize(700,640);
// setVisible(true);
try {
SwingUtilities.invokeAndWait(new Runnable () {
public void run() {
guiInit(); // initialize the GUI
}
});
} catch(Exception exc) {
System.out.println("Can't create because of "+ exc);
}
}
// Called second, after init(). Also called
// whenever the applet is restarted.
public void start() {
// Not used by this applet.
}
// Called when the applet is stopped.
public void stop() {
// Not used by this applet.
}
// Called when applet is terminated. This is
// the last method executed.
public void destroy() {
// Not used by this applet.
}
// Setup and initialize the GUI.
private void guiInit() {
// Create two buttons and a label.
// setLayout(null);
String a[] = { "Chiken Tikka", "Fajita", "Vegetable"};
comboBox = new JComboBox(a);
comboBox.setBounds(115, 103, 100, 20);
comboBox.addItemListener(this);
getContentPane().add(comboBox);
String b[] = { "small", "medium","large","extra large"};
comboBox_1 = new JComboBox(b);
// comboBox_1.addItemListener(this);
// comboBox_1.setEnabled(false);
getContentPane().add(comboBox_1);
comboBox_1.setBounds(290, 103, 140, 20);
// setSize(300, 300);
getContentPane().setLayout(null);
JLabel lblSelectMenu = new JLabel("Select Menu");
lblSelectMenu.setForeground(new Color(72, 61, 139));
lblSelectMenu.setFont(new Font("Segoe Script", Font.BOLD, 16));
lblSelectMenu.setBounds(58, 10, 107, 34);
getContentPane().add(lblSelectMenu);
chckbxPizza = new JCheckBox("Pizza");
chckbxPizza.setBounds(37, 64, 97, 23);
getContentPane().add(chckbxPizza);
JLabel lblFlavour = new JLabel("flavour");
lblFlavour.setBounds(60, 103, 45, 20);
getContentPane().add(lblFlavour);
JLabel lblSize = new JLabel("size");
lblSize.setBounds(253, 103, 45, 20);
getContentPane().add(lblSize);
JLabel lblQty = new JLabel("Qty");
lblQty.setBounds(451, 103, 35, 20);
getContentPane().add(lblQty);
textField = new JTextField();
textField.setBounds(485, 103, 86, 20);
getContentPane().add(textField);
textField.setColumns(10);
JLabel lblComments = new JLabel("comments");
lblComments.setBounds(139, 154, 76, 20);
getContentPane().add(lblComments);
txtrCommentsForSpecial = new JTextArea();
txtrCommentsForSpecial.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial.setText("comments for special features");
txtrCommentsForSpecial.setBounds(210, 154, 268, 61);
getContentPane().add(txtrCommentsForSpecial);
chckbxBurger = new JCheckBox("Burger");
chckbxBurger.setBounds(37, 236, 97, 23);
getContentPane().add(chckbxBurger);
JLabel label = new JLabel("flavour");
label.setBounds(60, 286, 45, 20);
getContentPane().add(label);
String c[] = { "Fish", "Vegetable", "Chicken"};
JComboBox comboBox_2 = new JComboBox(c);
comboBox_2.setBounds(115, 286, 100, 20);
getContentPane().add(comboBox_2);
JLabel label_1 = new JLabel("size");
label_1.setBounds(253, 286, 45, 20);
getContentPane().add(label_1);
JComboBox comboBox_3 = new JComboBox(b);
comboBox_3.setBounds(290, 286, 110, 20);
getContentPane().add(comboBox_3);
JLabel label_2 = new JLabel("Qty");
label_2.setBounds(451, 286, 35, 20);
getContentPane().add(label_2);
textField_1 = new JTextField();
textField_1.setBounds(485, 286, 86, 20);
getContentPane().add(textField_1);
textField_1.setColumns(10);
label_3 = new JLabel("comments");
label_3.setBounds(139, 340, 76, 20);
getContentPane().add(label_3);
txtrCommentsForSpecial_1 = new JTextArea();
txtrCommentsForSpecial_1.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_1.setText("comments for special features");
txtrCommentsForSpecial_1.setBounds(210, 340, 268, 61);
getContentPane().add(txtrCommentsForSpecial_1);
chckbxSalad = new JCheckBox("Soft Drink");
chckbxSalad.setBounds(37, 424, 97, 23);
getContentPane().add(chckbxSalad);
JLabel label_4 = new JLabel("flavour");
label_4.setBounds(60, 475, 45, 20);
getContentPane().add(label_4);
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
JComboBox comboBox_4 = new JComboBox(f);
comboBox_4.setBounds(115, 475, 86, 20);
getContentPane().add(comboBox_4);
JLabel label_5 = new JLabel("size");
label_5.setBounds(253, 475, 45, 20);
getContentPane().add(label_5);
JComboBox comboBox_5 = new JComboBox(b);
comboBox_5.setBounds(290, 475, 110, 20);
getContentPane().add(comboBox_5);
JLabel label_6 = new JLabel("Qty");
label_6.setBounds(451, 475, 35, 20);
getContentPane().add(label_6);
textField_2 = new JTextField();
textField_2.setBounds(485, 475, 86, 20);
getContentPane().add(textField_2);
textField_2.setColumns(10);
JLabel label_7 = new JLabel("comments");
label_7.setBounds(139, 546, 76, 20);
getContentPane().add(label_7);
txtrCommentsForSpecial_2 = new JTextArea();
txtrCommentsForSpecial_2.setForeground(new Color(220, 220, 220));
txtrCommentsForSpecial_2.setText("comments for special features");
txtrCommentsForSpecial_2.setBounds(210, 546, 268, 61);
getContentPane().add(txtrCommentsForSpecial_2);
JButton btnNewButton = new JButton("Ok");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame = new JInternalFrame("Order Details");
internalFrame.setBounds(100, 80, 450, 300);
internalFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
internalFrame.getContentPane().setLayout(null);
internalFrame.setVisible(true);
JTextArea textArea = new JTextArea();
textArea.setBounds(67, 52, 270, 149);
internalFrame.getContentPane().add(textArea);
JButton btnOk = new JButton("Ok");
btnOk.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
internalFrame.setVisible(false);
}
});
btnOk.setBounds(164, 212, 89, 23);
internalFrame.getContentPane().add(btnOk);
JLabel lblOrderYouPlaced = new JLabel("Order You Placed");
lblOrderYouPlaced.setBounds(67, 26, 98, 14);
internalFrame.getContentPane().add(lblOrderYouPlaced);
getContentPane().add(internalFrame);
}
});
btnNewButton.setBounds(162, 616, 89, 23);
getContentPane().add(btnNewButton);
JButton btnNewButton_1 = new JButton("Reset");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
textField.setText("");
textField_1.setText("");
textField_2.setText("");
chckbxPizza.setText("");
chckbxBurger.setText("");
chckbxSalad.setText("");
txtrCommentsForSpecial.setText("");
txtrCommentsForSpecial_2.setText("");
txtrCommentsForSpecial_1.setText("");
}
});
btnNewButton_1.setBounds(279, 616, 89, 23);
getContentPane().add(btnNewButton_1);
JButton btnNewButton_2 = new JButton("Cancel");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btnNewButton_2.setBounds(389, 616, 89, 23);
getContentPane().add(btnNewButton_2);
}
public void itemStateChanged(ItemEvent e) {
String b[] = { "Chikken Tikka", "Fajita" };
String c[] = { "Fish", "Vegetable", "Chicken"};
String d[] = { "Russian Salad", "Fruit Salad", "Beans Salad"};
String f[] = { "Pepsi", "Coca Cola", "Sprite","7up"};
if (e.getSource() == comboBox) {
if (comboBox.getSelectedItem().equals("Select")) {
comboBox_1.setEnabled(false);
} else if (comboBox.getSelectedItem().equals("Pizza")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < b.length; i++) {
comboBox_1.addItem(b[i]);
}
} else if (comboBox.getSelectedItem().equals("Burger")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < c.length; i++) {
comboBox_1.removeItem(c[i]);
comboBox_1.addItem(c[i]);
}
} else if (comboBox.getSelectedItem().equals("Salad")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < d.length; i++) {
comboBox_1.addItem(d[i]);
}
}else if (comboBox.getSelectedItem().equals("Soft Drink")) {
comboBox_1.setEnabled(true);
comboBox_1.removeAllItems();
for (int i = 0; i < f.length; i++) {
comboBox_1.addItem(f[i]);
}
}
}
}
}
Write the itemListener with a captial letter like this
public class Myapp extends JApplet implements ItemListener{
and import all this packages:
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

Categories

Resources