Well, i have this code.
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;
import java.awt.Font;
import javax.swing.JSeparator;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Menu {
private JFrame frmFelps;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Menu window = new Menu();
window.frmFelps.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Menu() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
Funcoes acoes = new Funcoes();
frmFelps = new JFrame();
frmFelps.setResizable(false);
frmFelps.setTitle("Felps");
frmFelps.setBounds(100, 100, 824, 522);
frmFelps.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmFelps.getContentPane().setLayout(null);
JProgressBar progressoSede = new JProgressBar();
progressoSede.setStringPainted(true);
progressoSede.setValue(acoes.getSede());
progressoSede.setBounds(71, 25, 100, 25);
frmFelps.getContentPane().add(progressoSede);
JProgressBar progressoAmor = new JProgressBar();
progressoAmor.setValue(acoes.getAmor());
progressoAmor.setStringPainted(true);
progressoAmor.setBounds(71, 56, 100, 25);
frmFelps.getContentPane().add(progressoAmor);
JProgressBar progressoFeliz = new JProgressBar();
progressoFeliz.setValue(acoes.getFeliz());
progressoFeliz.setStringPainted(true);
progressoFeliz.setBounds(71, 85, 100, 25);
frmFelps.getContentPane().add(progressoFeliz);
JLabel lblSede = new JLabel("Sede:");
lblSede.setToolTipText("Porcentagem de AQUA que voc\u00EA bebeu e gravou nos ultimos tempos.");
lblSede.setFont(new Font("Arial", Font.PLAIN, 16));
lblSede.setBounds(24, 25, 46, 23);
frmFelps.getContentPane().add(lblSede);
JLabel lblAmor = new JLabel("Amor:");
lblAmor.setToolTipText("O Amor n\u00E3o precisa ser descrito com palavras.");
lblAmor.setFont(new Font("Arial", Font.PLAIN, 16));
lblAmor.setBounds(24, 56, 46, 23);
frmFelps.getContentPane().add(lblAmor);
JLabel lblFeliz = new JLabel("Feliz:");
lblFeliz.setToolTipText("Um Felps Feliz \u00E9 um Felps que n\u00E3o esta Triste.");
lblFeliz.setFont(new Font("Arial", Font.PLAIN, 16));
lblFeliz.setBounds(24, 85, 46, 23);
frmFelps.getContentPane().add(lblFeliz);
JLabel lblCamera = new JLabel("Camera:");
lblCamera.setToolTipText("Quanto melhor a Camera mais Video com FaceCam o Felps Faz");
lblCamera.setBounds(24, 168, 74, 14);
frmFelps.getContentPane().add(lblCamera);
JLabel lblPc = new JLabel("PC:");
lblPc.setToolTipText("Quanto melhor seu PC mais o Cellbits vai sentir inveja de voc\u00EA.");
lblPc.setBounds(24, 199, 74, 14);
frmFelps.getContentPane().add(lblPc);
JLabel lblFone = new JLabel("Fone:");
lblFone.setToolTipText("Hey Galera Felps falando e bem vindos ao meu canal.");
lblFone.setBounds(24, 230, 74, 14);
frmFelps.getContentPane().add(lblFone);
JLabel lblInscritos = new JLabel("Inscritos:");
lblInscritos.setToolTipText("Quanto mais melhor. (P.S.: Eles s\u00F3 v\u00E3o te amar se voc\u00EA amar eles)");
lblInscritos.setBounds(50, 290, 83, 14);
frmFelps.getContentPane().add(lblInscritos);
JLabel lblDinheiros = new JLabel("Dinheiros:");
lblDinheiros.setToolTipText("Quantos dinheiros existem dentro do seu bolso Felpastico.");
lblDinheiros.setBounds(10, 432, 60, 14);
frmFelps.getContentPane().add(lblDinheiros);
JTextPane txtpnCamera = new JTextPane();
txtpnCamera.setEditable(false);
txtpnCamera.setText(acoes.getCamera());
txtpnCamera.setBounds(108, 162, 104, 20);
frmFelps.getContentPane().add(txtpnCamera);
JTextPane txtpnPc = new JTextPane();
txtpnPc.setText(acoes.getPc());
txtpnPc.setEditable(false);
txtpnPc.setBounds(108, 193, 104, 20);
frmFelps.getContentPane().add(txtpnPc);
JTextPane txtpnFone = new JTextPane();
txtpnFone.setText(acoes.getFone());
txtpnFone.setEditable(false);
txtpnFone.setBounds(108, 224, 104, 20);
frmFelps.getContentPane().add(txtpnFone);
JTextPane txtpnInscrito = new JTextPane();
txtpnInscrito.setEditable(false);
txtpnInscrito.setText(acoes.getInscritos());
txtpnInscrito.setBounds(24, 315, 130, 20);
frmFelps.getContentPane().add(txtpnInscrito);
JTextPane txtpnDinheiro = new JTextPane();
txtpnDinheiro.setText(acoes.getDinheiro());
txtpnDinheiro.setEditable(false);
txtpnDinheiro.setBounds(80, 426, 91, 20);
frmFelps.getContentPane().add(txtpnDinheiro);
JButton btnYoutube = new JButton("YouTube");
btnYoutube.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//TODO Fazer Algo
}
});
JSeparator separator = new JSeparator();
separator.setOrientation(SwingConstants.VERTICAL);
separator.setBounds(228, 0, 13, 493);
frmFelps.getContentPane().add(separator);
btnYoutube.setBounds(534, 172, 146, 25);
frmFelps.getContentPane().add(btnYoutube);
JButton btnCasa = new JButton("Casa");
btnCasa.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//TODO Fazer Algo
acoes.setInscritos(10);
//txtpnInscrito.setText(acoes.getInscritos());
atualizador();
//txtpnInscrito.setText(acoes.getInscritos());
}
});
btnCasa.setBounds(268, 171, 146, 25);
frmFelps.getContentPane().add(btnCasa);
JButton btnCidade = new JButton("Cidade");
btnCidade.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//TODO Fazer Algo
}
});
btnCidade.setBounds(268, 264, 146, 25);
frmFelps.getContentPane().add(btnCidade);
JButton btnBica = new JButton("Bica");
btnBica.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
//TODO Fazer Algo
}
});
btnBica.setBounds(534, 265, 146, 25);
frmFelps.getContentPane().add(btnBica);
JLabel lblFundo = new JLabel("New label");
lblFundo.setIcon(new ImageIcon("img\\mapa.png"));
lblFundo.setBounds(251, 20, 528, 435);
frmFelps.getContentPane().add(lblFundo);
}
private void atualizador(){
//TODO
}
}
I want to change the text on txtpnInscrito every time that atualizador() runs
But i don't know how to do this.
If i change the text directly in the button btnCasa it changes, but i want to make a refresher by time and utilize the same function to refresh when doing actions on buttons.
Thanks in advance.
And sorry for the horrible english.
You need to make txtpnInscrito field of the class so that it's accessible in all your methods within your class
Have a look at Understanding Class Members for more details
Related
When I try to add some function to get data from jtext field under public void actionPerformed(ActionEvent arg0) My GUI goes blank I have attached the images below I can't figure out what's wrong with it.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Register extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblName = new JLabel("Name");
lblName.setBounds(56, 56, 56, 16);
contentPane.add(lblName);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(56, 85, 77, 16);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(56, 114, 56, 16);
contentPane.add(lblPassword);
JLabel lblAge = new JLabel("Age");
lblAge.setBounds(56, 143, 56, 16);
contentPane.add(lblAge);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(56, 172, 56, 16);
contentPane.add(lblGender);
textField = new JTextField();
textField.setBounds(130, 53, 116, 19);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(130, 82, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(130, 111, 116, 22);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(130, 140, 116, 22);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(130, 169, 116, 22);
contentPane.add(textField_4);
textField_4.setColumns(10);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnSubmit.setBounds(169, 215, 97, 25);
contentPane.add(btnSubmit);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(330, 56, 56, 16);
contentPane.add(lblNewLabel);
}
}
EDIT 1: Added String name to action just to get name.
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String Name;
}
});
and This happened
GUI went blank:
You're using an absolute layout. Since you can't pack() the window, you need to at least validate() its contents.
Register frame = new Register();
frame.validate();
frame.setVisible(true);
Now you can see the real problem: you picked bounds for lblPassword and lblNewLabel that cut off the text on my computer. You really need to use a layout, maybe like this.
I am curious to ask that is it possible to transfer a data from one JPanel to another. I'm currently doing a swing java project which has a profile page and a profile edit page. I wanted to try out creating a method to get the text in a textfield in the profile edit page and then return the textfield.getText();. Then I will follow by calling the exact method from the profile page and setting it as a JLabel. However, I wasn't sure how to do this...
I updated the codes below.
Here is the Profile Edit Page:
package Project;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ProfileEdit extends MasterPanel {
private JTextField txtbxFullName;
private JTextField txtAdmissionNo;
private JTextField txtbxContactNo;
private JTextField txtFieldDiploma;
/**
* Create the panel.
*/
public ProfileEdit(JFrame mf) {
super(mf);
JLabel lblUserProfile = new JLabel("");
lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE));
lblUserProfile.setForeground(Color.WHITE);
lblUserProfile.setFont(new Font("Tahoma", Font.PLAIN, 15));
lblUserProfile.setBounds(60, 103, 514, 297);
add(lblUserProfile);
JLabel lblDisplayPic = new JLabel("");
lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
lblDisplayPic.setIcon(new ImageIcon(ProfileEdit.class.getResource("/javaProject/images/default-avatar.png")));
lblDisplayPic.setBounds(100, 132, 90, 100);
add(lblDisplayPic);
JLabel lblFullName = new JLabel("Full Name:");
lblFullName.setForeground(Color.WHITE);
lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblFullName.setBounds(235, 132, 76, 14);
add(lblFullName);
JLabel lblGender = new JLabel("Gender: ");
lblGender.setForeground(Color.WHITE);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblGender.setBounds(235, 168, 60, 14);
add(lblGender);
JRadioButton rdbtnMale = new JRadioButton("Male");
rdbtnMale.setBackground(Color.DARK_GRAY);
rdbtnMale.setFont(new Font("Tahoma", Font.PLAIN, 13));
rdbtnMale.setForeground(Color.WHITE);
rdbtnMale.setBounds(326, 164, 68, 23);
add(rdbtnMale);
JRadioButton rdbtnFemale = new JRadioButton("Female");
rdbtnFemale.setBackground(Color.DARK_GRAY);
rdbtnFemale.setFont(new Font("Tahoma", Font.PLAIN, 13));
rdbtnFemale.setForeground(Color.WHITE);
rdbtnFemale.setBounds(396, 164, 77, 23);
add(rdbtnFemale);
ButtonGroup genderRdBtn = new ButtonGroup();
genderRdBtn.add(rdbtnFemale);
genderRdBtn.add(rdbtnMale);
JLabel lblAdmissionNo = new JLabel("Admission No:");
lblAdmissionNo.setForeground(Color.WHITE);
lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblAdmissionNo.setBounds(235, 203, 97, 14);
add(lblAdmissionNo);
JLabel lblContactNo = new JLabel("Contact No:");
lblContactNo.setForeground(Color.WHITE);
lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblContactNo.setBounds(235, 240, 77, 14);
add(lblContactNo);
JLabel lblDiploma = new JLabel("Diploma In:");
lblDiploma.setForeground(Color.WHITE);
lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblDiploma.setBounds(235, 275, 76, 14);
add(lblDiploma);
txtbxFullName = new JTextField();
txtbxFullName.setBounds(326, 130, 147, 20);
add(txtbxFullName);
txtbxFullName.setColumns(10);
txtAdmissionNo = new JTextField();
txtAdmissionNo.setBounds(326, 201, 147, 20);
add(txtAdmissionNo);
txtAdmissionNo.setColumns(10);
txtbxContactNo = new JTextField();
txtbxContactNo.setBounds(326, 238, 147, 20);
add(txtbxContactNo);
txtbxContactNo.setColumns(10);
txtFieldDiploma = new JTextField();
txtFieldDiploma.setBounds(326, 273, 147, 20);
add(txtFieldDiploma);
txtFieldDiploma.setColumns(10);
JButton btnSubmit = new JButton("SUBMIT");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home home = new Home(mf);
mf.setContentPane(home);
mf.setVisible(true);
}
});
btnSubmit.setBounds(235, 336, 114, 23);
add(btnSubmit);
JButton btnCancel = new JButton("CANCEL");
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home home = new Home(mf);
mf.setContentPane(home);
mf.setVisible(true);
}
});
btnCancel.setBounds(359, 336, 114, 23);
add(btnCancel);
JButton btnBrowse = new JButton("Browse");
btnBrowse.setBounds(101, 247, 89, 23);
add(btnBrowse);
}
}
Here is the Profile Page :
package Project;
import java.awt.Color;
import Project.ProfileEdit;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.border.LineBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import Project.ProfileEdit;
public class Home extends MasterPanel {
/**
* Create the panel.
*/
public Home(JFrame mf) {
super(mf);
JLabel lblUserProfile = new JLabel("");
//createTitledBorder(border, string, int, int, font, color)
lblUserProfile.setBorder(BorderFactory.createTitledBorder(null,"User Profile",0,0, new Font("Tahoma", Font.PLAIN, 15), Color.WHITE));
//setBounds(left, top, right, bottom)
lblUserProfile.setBounds(60, 103, 510, 267);
add(lblUserProfile);
JLabel lblDisplayPic = new JLabel("");
lblDisplayPic.setBorder(new LineBorder(Color.WHITE));
lblDisplayPic.setIcon(new ImageIcon(Home.class.getResource("/javaProject/images/default-avatar.png")));
lblDisplayPic.setBounds(108, 142, 90, 100);
add(lblDisplayPic);
JLabel lblFullName = new JLabel("Full Name:");
lblFullName.setForeground(Color.WHITE);
lblFullName.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblFullName.setBounds(251, 131, 76, 14);
add(lblFullName);
JLabel lblGender = new JLabel("Gender: ");
lblGender.setForeground(Color.WHITE);
lblGender.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblGender.setBounds(251, 151, 60, 14);
add(lblGender);
JLabel lblAdmissionNo = new JLabel("Admission No:");
lblAdmissionNo.setForeground(Color.WHITE);
lblAdmissionNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblAdmissionNo.setBounds(251, 176, 97, 14);
add(lblAdmissionNo);
JLabel lblContactNo = new JLabel("Contact No:");
lblContactNo.setForeground(Color.WHITE);
lblContactNo.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblContactNo.setBounds(251, 195, 77, 14);
add(lblContactNo);
JLabel lblDiploma = new JLabel("Diploma In:");
lblDiploma.setForeground(Color.WHITE);
lblDiploma.setFont(new Font("Tahoma", Font.PLAIN, 13));
lblDiploma.setBounds(251, 218, 76, 14);
add(lblDiploma);
JButton btnEdit = new JButton("Edit ");
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ProfileEdit ProfileEdit = new ProfileEdit(mf);
mf.setContentPane(ProfileEdit);
mf.setVisible(true);
}
});
btnEdit.setBounds(251, 309, 89, 23);
add(btnEdit);
}
}
and Lastly, here is the Master Panel if you guys need it :
package Project;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import java.awt.Color;
import javax.swing.JSeparator;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class MasterPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
protected JFrame myFrame = null;
/**
* Create the panel.
*/
public MasterPanel(JFrame mf) {
myFrame = mf;
setBackground(Color.DARK_GRAY);
setLayout(null);
//set a new menubar for home
JMenuBar menuBarHome = new JMenuBar();
menuBarHome.setForeground(Color.BLACK);
menuBarHome.setBackground(Color.WHITE);
// x,y,width,height
// >x = right, <x = left
// >y = bottom, <y = up
menuBarHome.setBounds(80, 40, 97, 21);
//length and height
menuBarHome.setSize(48, 20);
add(menuBarHome);
JMenu home = new JMenu("HOME");
home.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
Home home = new Home(mf);
mf.setContentPane(home);
mf.setVisible(true);
}
});
menuBarHome.add(home);
//set a new menubar for calendar
JMenuBar menuBarCalendar = new JMenuBar();
menuBarCalendar.setForeground(Color.BLACK);
menuBarCalendar.setBackground(Color.WHITE);
menuBarCalendar.setBounds(145, 40, 97, 21);
menuBarCalendar.setSize(75, 20);
add(menuBarCalendar);
JMenu calendar = new JMenu("CALENDAR");
calendar.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
Calendar calendar = new Calendar(mf);
mf.setContentPane(calendar);
mf.setVisible(true);
}
});
menuBarCalendar.add(calendar);
JMenuBar menuBarAcademic = new JMenuBar();
menuBarAcademic.setForeground(Color.BLACK);
menuBarAcademic.setBackground(Color.WHITE);
menuBarAcademic.setBounds(235, 40, 220, 21);
menuBarAcademic.setSize(72, 20);
add(menuBarAcademic);
JMenu academic = new JMenu("ACADEMIC");
menuBarAcademic.add(academic);
//add sub menu into the academic
JMenuItem lectureNotes = new JMenuItem("Lecture Notes");
lectureNotes.setForeground(Color.BLACK);
lectureNotes.setBackground(Color.WHITE);
lectureNotes.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
LectureNotes LectureNotes = new LectureNotes(mf);
mf.setContentPane(LectureNotes);
mf.setVisible(true);
}
});
academic.add(lectureNotes);
JMenuItem resulttracker = new JMenuItem("Result Tracker");
resulttracker.setForeground(Color.BLACK);
resulttracker.setBackground(Color.WHITE);
resulttracker.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ResultTracker resulttracker = new ResultTracker(mf);
mf.setContentPane(resulttracker);
mf.setVisible(true);
}
});
academic.add(resulttracker);
JMenuItem studyplan = new JMenuItem("Study Planner");
studyplan.setForeground(Color.BLACK);
studyplan.setBackground(Color.WHITE);
studyplan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StudyPlanner studyplanner = new StudyPlanner(mf);
mf.setContentPane(studyplanner);
mf.setVisible(true);
}
});
academic.add(studyplan);
JMenuItem timetable = new JMenuItem("Timetable");
timetable.setForeground(Color.BLACK);
timetable.setBackground(Color.WHITE);
timetable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TimeTable timetable = new TimeTable(mf);
mf.setContentPane(timetable);
mf.setVisible(true);
}
});
academic.add(timetable);
JMenuBar menuBarEmail = new JMenuBar();
menuBarEmail.setForeground(Color.BLACK);
menuBarEmail.setBackground(Color.WHITE);
menuBarEmail.setBounds(320, 40, 97, 21);
menuBarEmail.setSize(47, 20);
add(menuBarEmail);
JMenu email = new JMenu("EMAIL");
menuBarEmail.add(email);
JMenuItem composeemail = new JMenuItem("Compose");
composeemail.setForeground(Color.BLACK);
composeemail.setBackground(Color.WHITE);
composeemail.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Email email = new Email(mf);
mf.setContentPane(email);
mf.setVisible(true);
}
});
email.add(composeemail);
JMenuItem inboxemail = new JMenuItem("Inbox");
inboxemail.setForeground(Color.BLACK);
inboxemail.setBackground(Color.WHITE);
email.add(inboxemail);
JMenuBar menuBarContactus = new JMenuBar();
menuBarContactus.setForeground(Color.BLACK);
menuBarContactus.setBackground(Color.WHITE);
menuBarContactus.setBounds(380, 40, 97, 21);
menuBarContactus.setSize(85, 20);
add(menuBarContactus);
JMenu contactus= new JMenu("CONTACT US");
contactus.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
ContactUs contactus = new ContactUs(mf);
mf.setContentPane(contactus);
mf.setVisible(true);
}
});
menuBarContactus.add(contactus);
JMenuBar menuBarLogout = new JMenuBar();
menuBarLogout.setForeground(Color.BLACK);
menuBarLogout.setBackground(Color.WHITE);
menuBarLogout.setBounds(480, 40, 97, 21);
menuBarLogout.setSize(60, 20);
add(menuBarLogout);
JMenu logout = new JMenu("LOGOUT");
logout.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
Login login = new Login(mf);
mf.setContentPane(login);
mf.setVisible(true);
}
});
menuBarLogout.add(logout);
JSeparator separatorTop = new JSeparator();
separatorTop.setBounds(50, 23, 524, 2);
add(separatorTop);
JSeparator separatorBottom = new JSeparator();
separatorBottom.setBounds(50, 79, 524, 2);
add(separatorBottom);
}
}
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.
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.
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;