Looking to validate both textField and comboBox in Java Swing GUI - java

Please be gentle, this is my first time working with Swing and GUI's.
My problem/question:
I have written some code using Java Swing GUI. The validation portion of my code will only recognize if the textField is incorrectly filled out and ignores if the comboBoxes are left empty. Can anyone tell me what I am doing wrong here? My goal is to make sure that all fields are filled out when the user hits submit. Here is my code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.layout.FormSpecs;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FeliciasStore_GUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FeliciasStore_GUI frame = new FeliciasStore_GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FeliciasStore_GUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 558, 364);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Size:");
lblNewLabel.setBounds(67, 39, 23, 14);
contentPane.add(lblNewLabel);
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.setBounds(96, 36, 161, 20);
contentPane.add(comboBox);
comboBox.addItem("Small");
comboBox.addItem("Medium");
comboBox.addItem("Large");
comboBox.addItem("XLarge");
comboBox.setSelectedItem(null);
JLabel lblColor = new JLabel("Color:");
lblColor.setBounds(61, 90, 29, 14);
contentPane.add(lblColor);
JComboBox<String> comboBox_1 = new JComboBox<String>();
comboBox_1.setBounds(96, 87, 161, 20);
contentPane.add(comboBox_1);
comboBox_1.addItem("Blue");
comboBox_1.addItem("Red");
comboBox_1.addItem("Silver");
comboBox_1.addItem("Gold");
comboBox_1.addItem("Aqua");
comboBox_1.addItem("Green");
comboBox_1.addItem("Pink");
comboBox_1.addItem("Purple");
comboBox_1.addItem("White");
comboBox_1.addItem("Black");
comboBox_1.addItem("Orange");
comboBox_1.addItem("Maroon");
comboBox_1.setSelectedItem(null);
JLabel label = new JLabel("");
label.setBounds(518, 122, 19, 0);
contentPane.add(label);
JLabel lblStyle = new JLabel("Style:");
lblStyle.setBounds(62, 141, 28, 14);
contentPane.add(lblStyle);
JComboBox<String> comboBox_2 = new JComboBox<String>();
comboBox_2.setBounds(96, 138, 161, 20);
contentPane.add(comboBox_2);
comboBox_2.addItem("Queen");
comboBox_2.addItem("King");
comboBox_2.addItem("Pawn");
comboBox_2.addItem("Knight");
comboBox_2.addItem("Joker");
comboBox_2.addItem("Babydoll");
comboBox_2.addItem("Superstar");
comboBox_2.setSelectedItem(null);
JLabel lblInscription = new JLabel("Inscription:");
lblInscription.setBounds(36, 192, 54, 14);
contentPane.add(lblInscription);
textField = new JTextField();
textField.setBounds(96, 189, 161, 20);
contentPane.add(textField);
textField.setColumns(10);
JButton btnSubmit_1 = new JButton("SUBMIT");
btnSubmit_1.setBounds(96, 240, 161, 23);
btnSubmit_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(textField.getText().length()>15 || comboBox == null || comboBox_1 == null || comboBox_2 == null) {
JOptionPane.showMessageDialog(null, "Insufficient input.", "Input Error", JOptionPane.ERROR_MESSAGE);
return;
}
String enscription = textField.getText();
}
});
contentPane.add(btnSubmit_1);
}
}

Related

Setting a JLabel visible on JRadioButton click

I tried setting JLabel to being visible only after I select a certain JRadioButton. The program is getting an error if I set lblNewLabel_1.setVisible(true) in the action performed of a radio button. It worked with the text field, but with this not. What can I do? Is it different with the label? Is there any advice someone can offer me?
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JRadioButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.ButtonGroup;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Fereastra extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private final ButtonGroup buttonGroup = new ButtonGroup();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Fereastra frame = new Fereastra();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Fereastra() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 751, 565);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(null);
setContentPane(contentPane);
JTextArea textArea = new JTextArea();
textArea.setBounds(12, 13, 447, 251);
contentPane.add(textArea);
JRadioButton rdbtnNewRadioButton = new JRadioButton("Cautarea pe nivel");
rdbtnNewRadioButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton);
rdbtnNewRadioButton.setBounds(488, 55, 185, 25);
contentPane.add(rdbtnNewRadioButton);
JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("Cautarea in adancime");
rdbtnNewRadioButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(rdbtnNewRadioButton_1.isSelected())
{
textField.setVisible(true);
textField_1.setVisible(true);
}
else
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_1);
rdbtnNewRadioButton_1.setBounds(488, 96, 237, 25);
contentPane.add(rdbtnNewRadioButton_1);
JRadioButton rdbtnNewRadioButton_2 = new JRadioButton("Costul uniform");
rdbtnNewRadioButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_2.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_2);
rdbtnNewRadioButton_2.setBounds(488, 138, 127, 25);
contentPane.add(rdbtnNewRadioButton_2);
JRadioButton rdbtnNewRadioButton_3 = new JRadioButton("Adancime iterativa");
rdbtnNewRadioButton_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_3.isSelected())
{
textField_1.setVisible(true);
textField.setVisible(true);
}
else
{
textField_1.setVisible(false);
textField.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_3);
rdbtnNewRadioButton_3.setBounds(488, 179, 237, 25);
contentPane.add(rdbtnNewRadioButton_3);
JRadioButton rdbtnNewRadioButton_4 = new JRadioButton("Greedy");
rdbtnNewRadioButton_4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(rdbtnNewRadioButton_4.isSelected())
{
textField.setVisible(false);
textField_1.setVisible(false);
}
}
});
buttonGroup.add(rdbtnNewRadioButton_4);
rdbtnNewRadioButton_4.setBounds(488, 221, 127, 25);
contentPane.add(rdbtnNewRadioButton_4);
JLabel lblNewLabel = new JLabel("Alegeti:");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(488, 13, 84, 33);
contentPane.add(lblNewLabel);
textField = new JTextField();
textField.setBounds(402, 277, 116, 22);
contentPane.add(textField);
textField.setColumns(10);
textField.setVisible(false);
textField_1 = new JTextField();
textField_1.setBounds(246, 277, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_1.setVisible(false);
JLabel lblNewLabel_1 = new JLabel("Introduceti valorile dorite:");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel_1.setBounds(22, 277, 212, 21);
contentPane.add(lblNewLabel_1);
lblNewLabel_1.setVisible(false);
}
}
It's because your label is declared in function context, put the declaration next the textbox declaration and it should work fine

random number generator jframe

Working on a side project for a professor that wants a random number generator.
The program in its current state leaves a lot to be desired but I think I'm on the right track. However, I am stuck when it comes to using the text field, passing that data to my random number equation, and displaying it. Basically, the program isn't waiting for the user input to the text field.
We never made it past OOP in school so I need to pull out the big guns (you guys).
Here is what I am working with currently. The last bit at the end is the part that I am having issue with.
package RandomButton;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JLabel;
public class GUI extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField ClassSize;
private JLabel label;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GUI frame = new GUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public GUI() {
int x;
int max;
String strMax;
String strX;
Scanner kb = new Scanner(System.in);
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);
//Text field code
textField = new JTextField();
textField.setText("1");
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setBounds(10, 143, 414, 35);
contentPane.add(textField);
textField.setColumns(10);
//Randomize button code
JButton btnNewButton = new JButton("Randomize!");
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 40));
btnNewButton.setBounds(10, 196, 414, 54);
contentPane.add(btnNewButton);
//Label code
label = new JLabel();
label.setFont(new Font("Tahoma", Font.PLAIN, 70));
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setBounds(10, 11, 414, 103);
contentPane.add(label);
//math code
Random num = new Random();
strMax = textField.getText();
max = Integer.parseInt(strMax);
x = num.nextInt(max) + 1;
strX = String.valueOf(x);
label.setText(strX);
}
}
What you need is to add an ActionListener, see javadoc here
Simple modification of the code, without bigger discussion, is to move up the code that simulate random numbers...
//Randomize button code
JButton btnNewButton = new JButton("Randomize!");
btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 40));
btnNewButton.setBounds(10, 196, 414, 54);
contentPane.add(btnNewButton);
// the listener
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Random num = new Random();
String strMax = textField.getText();
int max = Integer.parseInt(strMax);
int x = num.nextInt(max) + 1;
String strX = String.valueOf(x);
label.setText(strX);
}
} );

How to store string or integer in java.swing JTextField?

Hello I am working on this project to store student names and record their grades. I have the entire program mapped out and all I need to do now is record the integers and strings from the user input and call it from different classes. I am having trouble with this. Do I use an array? How do I call from another class? Thank you.
package gradebook;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class Student extends JFrame {
private JFrame studentFrame;
private JPanel contentPane;
private JTextField studentNameTextField;
protected Component frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Student frame = new Student();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Student() {
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 gradebookLabel = new JLabel("Gradebook");
gradebookLabel.setBounds(189, 6, 68, 16);
contentPane.add(gradebookLabel);
JLabel lblPleaseEnterThe = new JLabel("Please enter the student name");
lblPleaseEnterThe.setBounds(130, 105, 194, 16);
contentPane.add(lblPleaseEnterThe);
studentNameTextField = new JTextField("Enter here");
String stdname = studentNameTextField.getText();
studentNameTextField.setBounds(130, 133, 194, 26);
contentPane.add(studentNameTextField);
studentNameTextField.setColumns(10);
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student.this.dispose();
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades();
addGradesScreen.setVisible(true);
}
}
});
continueButton.setBounds(327, 243, 117, 29);
contentPane.add(continueButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a) {
if(cancelButton.isEnabled()){
MainScreen mainScreenScreen = new MainScreen();
mainScreenScreen.setVisible(true);
contentPane.setVisible(false);
contentPane.disable();
}
}
});
cancelButton.setBounds(207, 243, 117, 29);
contentPane.add(cancelButton);
}
private void initizalize() {
// TODO Auto-generated method stub
}
}
Is it something along the lines of this?
studentNameTextField = new JTextField("Enter here");
String stdname = studentNameTextField.getText();
I know this would be storing it but how do I call that in a different class so I can make it appear on a different Frame?
Update: Okay so I've done this
Student frame = new Student();
String stdname = frame.studentNameTextField.getText();
JLabel addgradesLabel = new JLabel("Add grades for" + frame.studentNameTextField);
addgradesLabel.setBounds(139, 34, 167, 29);
contentPane.add(addgradesLabel);
And it's still not working. I believe I'm not implementing this correctly. I'm trying to title the label with what the user inputs for the name. So it would but "Add grades for" + stdname But it's not calling it correctly. How can I fix this?
Here is my code from the AddStudentName class
studentNameTextField = new JTextField();
studentNameTextField.setBounds(130, 133, 194, 26);
contentPane.add(studentNameTextField);
studentNameTextField.setColumns(10);
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student.this.dispose();
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades();
addGradesScreen.setVisible(true);
}
}
});
What should I add here to save the input that the user is inputting into the JTextField? Thanks for the help
Here's the full code for the classes:
package gradebook;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;
public class Student extends JFrame {
private JFrame studentFrame;
private JPanel contentPane;
public JTextField studentNameTextField;
protected Component frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Student frame = new Student();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Student() {
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 gradebookLabel = new JLabel("Gradebook");
gradebookLabel.setBounds(189, 6, 68, 16);
contentPane.add(gradebookLabel);
JLabel lblPleaseEnterThe = new JLabel("Please enter the student name");
lblPleaseEnterThe.setBounds(130, 105, 194, 16);
contentPane.add(lblPleaseEnterThe);
JTextField studentNameTextField = new JTextField();
studentNameTextField.setBounds(130, 133, 194, 26);
contentPane.add(studentNameTextField);
studentNameTextField.setColumns(10);
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades();
addGradesScreen.setVisible(true);
}
}
});
continueButton.setBounds(327, 243, 117, 29);
contentPane.add(continueButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a) {
if(cancelButton.isEnabled()){
MainScreen mainScreenScreen = new MainScreen();
mainScreenScreen.setVisible(true);
contentPane.setVisible(false);
contentPane.disable();
}
}
});
cancelButton.setBounds(207, 243, 117, 29);
contentPane.add(cancelButton);
}
private void initizalize() {
// TODO Auto-generated method stub
}
}
and:
package gradebook;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class AddGrades extends JFrame {
private JPanel contentPane;
private JTextField Assignment1;
private JTextField Assignment2;
private JTextField Assignment3;
private JTextField Assignment4;
private JLabel testsLabel;
private JTextField Test1;
private JTextField Test2;
public JTextField Test3;
public JTextField Test4;
/**
* Launch the application.
*/
public JFrame frame;
public JButton continueButton;
public JButton exitButton;
public static void main(String[] args) {
AddGrades frame = new AddGrades();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddGrades frame = new AddGrades();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public AddGrades() {
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 gradebookLabel = new JLabel("Gradebook");
gradebookLabel.setBounds(189, 6, 68, 16);
contentPane.add(gradebookLabel);
JLabel addgradesLabel = new JLabel("Add grades for" + stdname);
addgradesLabel.setBounds(139, 34, 167, 29);
contentPane.add(addgradesLabel);
JLabel assignmentsLabel = new JLabel("Assignments:");
assignmentsLabel.setBounds(19, 75, 86, 16);
contentPane.add(assignmentsLabel);
Assignment1 = new JTextField();
Assignment1.setBounds(54, 91, 130, 26);
contentPane.add(Assignment1);
Assignment1.setColumns(10);
Assignment2 = new JTextField();
Assignment2.setBounds(54, 129, 130, 26);
contentPane.add(Assignment2);
Assignment2.setColumns(10);
Assignment3 = new JTextField();
Assignment3.setBounds(54, 167, 130, 26);
contentPane.add(Assignment3);
Assignment3.setColumns(10);
Assignment4 = new JTextField();
Assignment4.setBounds(54, 205, 130, 26);
contentPane.add(Assignment4);
Assignment4.setColumns(10);
testsLabel = new JLabel("Tests:");
testsLabel.setBounds(243, 75, 38, 16);
contentPane.add(testsLabel);
Test1 = new JTextField();
Test1.setBounds(262, 91, 130, 26);
contentPane.add(Test1);
Test1.setColumns(10);
Test2 = new JTextField();
Test2.setBounds(262, 129, 130, 26);
contentPane.add(Test2);
Test2.setColumns(10);
Test3 = new JTextField();
Test3.setBounds(262, 167, 130, 26);
contentPane.add(Test3);
Test3.setColumns(10);
Test4 = new JTextField();
Test4.setBounds(262, 205, 130, 26);
contentPane.add(Test4);
Test4.setColumns(10);
continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent a){
if (continueButton.isEnabled()){
MainScreen mainScreenScreen = new MainScreen();
mainScreenScreen.setVisible(true);
}
}
});
continueButton.setBounds(327, 243, 117, 29);
contentPane.add(continueButton);
exitButton = new JButton("Exit");
exitButton.setBounds(208, 243, 117, 29);
contentPane.add(exitButton);
}
}
I'm trying to take the input from the JTextField
JTextField studentNameTextField = new JTextField();
and set it on the label
JLabel addgradesLabel = new JLabel("Add grades for" + stdname);
Where stdname would be the user input from the JTextField.
Now I'm getting errors from
public static void main(String[] args) {
AddGrades frame = new AddGrades();
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
AddGrades frame = new AddGrades();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
AddGrades frame = new AddGrades(); gives me error Add argument to match AddGrades(String)
The constructor AddGrades() is undefined
Edit: The best way to do this is to pass an argument to your newly created AddGrades class. See below:
public AddGrades(String stdname) {
JLabel addgradesLabel = new JLabel("Add grades for " + stdname);
}
You will need to modify your code to pass a string to your AddGrades class wherever you create it:
JButton continueButton = new JButton("Continue");
continueButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
if(continueButton.isEnabled()){
Student studentScreen = new Student();
studentScreen.dispose();
AddGrades addGradesScreen = new AddGrades(studentNameTextField.getText());
addGradesScreen.setVisible(true);
}
}
});
That worked for me - I downloaded your code and tested it.
JSpinner or JFormattedTextField would be my first thought, but if you can't do that then you could use Integer.parseInt to parse a String to int and a NumberFormat to format the numbers to Strings
Maybe have a look at How to Use Spinners and How to Use Formatted Text Fields for starters
I know this would be storing it but how do I call that in a different class so I can make it appear on a different JFrame?
That's a open question with little context. You might use some kind of "model" which represented one or more Student values; you might use a Observer Pattern to allow the editor to generate events to interested parties which would tell them that something has changed; you might use a Model-View-Controller; you might use a modal dialog and when it's closed, ask the editor for the values via getters.
Have a look at How to Make Dialogs for more details
One of things you want to keep in mind is "responsibility" and "encapsulation". You don't want outside parties to have uncontrolled access into your editor or model, this could allow them to modify the state in an uncontrolled way leading to inconsistencies in your data or UI.
You need to decide who is actually responsible for modifying the state of the model. There's no "right" answer, for example, you could have the editor either be capable of editing existing student objects or creating new ones based on how it's configured, equally, you could also have the editor be "dumb" and simply use setters to setup the editor and getters to get the values.
There are lots of options available to you, which you would use would be based on the context in which you want to use it. My gut feeling is start with an observer pattern and a modal dialog.

AWT-EventQueue-0" StackOverflowError

Afternoon all, i have a problem with some JFrame code, this JFrame is started when the user presses "New User", and whenever they do so, i get:
Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at frontend.Registration.<init>(Registration.java:36)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)
With this code:
package frontend;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class Registration extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static boolean ranOnce = false;
private JPanel contentPane;
private Registration reg = new Registration();
private JTextField userTF;
private JTextField passTF;
private JTextField emailTF;
private LoginProcess lp = new LoginProcess();
private JLabel error;
/**
* Create the frame.
*/
public Registration() {
if (!ranOnce) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ranOnce = true;
reg = new Registration();
reg.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 245, 195);
setLocationRelativeTo(null);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblUsername = new JLabel("Username:");
lblUsername.setBounds(10, 11, 75, 14);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password:");
lblPassword.setBounds(10, 36, 75, 14);
contentPane.add(lblPassword);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(10, 61, 75, 14);
contentPane.add(lblEmail);
userTF = new JTextField();
userTF.setBounds(95, 8, 130, 20);
contentPane.add(userTF);
userTF.setColumns(10);
passTF = new JTextField();
passTF.setColumns(10);
passTF.setBounds(95, 33, 130, 20);
contentPane.add(passTF);
emailTF = new JTextField();
emailTF.setColumns(10);
emailTF.setBounds(95, 58, 130, 20);
contentPane.add(emailTF);
error = new JLabel("Error: Username already in use");
error.setBounds(10, 120, 215, 14);
contentPane.add(error);
JButton regBtn = new JButton("Register");
regBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
if (lp.newUser(userTF.getText(), passTF.getText(), emailTF.getText())) {
reg.setVisible(false);
Main.mainFrame.setVisible(true);
} else {
if (lp.duplicateAccount) {
error.setText("lol");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
regBtn.setBounds(10, 86, 215, 23);
contentPane.add(regBtn);
}
}
I know this error is caused by an infinite loop, or something. but i have a boolean in place to stop it from infinitely running. This code was working about 20mins ago, and i haven't changed the first part of the constructor since i created the code.
Any ideas? Thanks..
You create a new Object in the line private Registration reg = new Registration();.
You should create this object in the consturctor.
The problem at that line:
private Registration reg = new Registration();
You get in a infinite loop and then stack overflow.

JTextField doesn't respond

I work with Swing and WindowsBuilder, the eclipse environment is running on the unix server.
Every time I run the program and try to copy paste some data from another window to JTextField in the program the JTextField stops responding.
I mean I press play in the Eclipse, the form is up and the JTextField is responding but then I press alt+tab to move to another window, press ctrl+c and then alt+tab again to go back to the running program but the JTextField doesn't respond to anything.
help someone?
Thanks
well here is the code:
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.DefaultFormatter;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.text.DecimalFormat;
import javax.swing.JTextField;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.DropMode;
public class MainPage extends JFrame {
private JPanel contentPane;
private String sSpe1;
private String sSpe2;
private JTextField txtTomB1;
private JTextField txtTomB2;
private JTextField txtTomB3;
private JTextField txtTomB4;
private JTextField txtTomB5;
private JTextField txtPotB1;
private JTextField txtPotB2;
private JTextField txtPotB3;
private JTextField txtPotB4;
private JTextField txtPotB5;
private JTextField txtAraB1;
private JTextField txtAraB2;
private JTextField txtAraB3;
private JTextField txtAraB4;
private JTextField txtAraB5;
private JTextField[] arrTomBaits = new JTextField[5];
private JTextField[] arrPotBaits = new JTextField[5];
private JTextField[] arrAraBaits = new JTextField[5];
private JLabel lblNote;
private JLabel lblChooseLocation;
private JTextField txtLocation;
private JButton btnBrowse;
private boolean bCanContinue;
private JSpinner spinner;
private JSpinner spnRValueBelow;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainPage frame = new MainPage();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public MainPage()
{
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 551, 609);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setSize(600, 1000);
setContentPane(contentPane);
contentPane.setLayout(null);
txtTomB1 = new JTextField();
txtTomB1.setDropMode(DropMode.INSERT);
txtTomB1.setBounds(20, 308, 86, 20);
contentPane.add(txtTomB1);
txtTomB1.setColumns(10);
arrTomBaits[0] = txtTomB1;
txtTomB2 = new JTextField();
txtTomB2.setDropMode(DropMode.INSERT);
txtTomB2.setColumns(10);
txtTomB2.setBounds(20, 339, 86, 20);
contentPane.add(txtTomB2);
arrTomBaits[1] = txtTomB2;
txtTomB3 = new JTextField();
txtTomB3.setDropMode(DropMode.INSERT);
txtTomB3.setColumns(10);
txtTomB3.setBounds(20, 370, 86, 20);
contentPane.add(txtTomB3);
arrTomBaits[2] = txtTomB3;
txtTomB4 = new JTextField();
txtTomB4.setDropMode(DropMode.INSERT);
txtTomB4.setColumns(10);
txtTomB4.setBounds(20, 401, 86, 20);
contentPane.add(txtTomB4);
arrTomBaits[3] = txtTomB4;
txtTomB5 = new JTextField();
txtTomB5.setDropMode(DropMode.INSERT);
txtTomB5.setColumns(10);
txtTomB5.setBounds(20, 432, 86, 20);
contentPane.add(txtTomB5);
arrTomBaits[4] = txtTomB5;
txtPotB1 = new JTextField();
txtPotB1.setDropMode(DropMode.INSERT);
txtPotB1.setColumns(10);
txtPotB1.setBounds(122, 308, 86, 20);
contentPane.add(txtPotB1);
arrPotBaits[0] = txtPotB1;
txtPotB2 = new JTextField();
txtPotB2.setDropMode(DropMode.INSERT);
txtPotB2.setColumns(10);
txtPotB2.setBounds(122, 339, 86, 20);
contentPane.add(txtPotB2);
arrPotBaits[1] = txtPotB2;
txtPotB3 = new JTextField();
txtPotB3.setDropMode(DropMode.INSERT);
txtPotB3.setColumns(10);
txtPotB3.setBounds(122, 370, 86, 20);
contentPane.add(txtPotB3);
arrPotBaits[2] = txtPotB3;
txtPotB4 = new JTextField();
txtPotB4.setDropMode(DropMode.INSERT);
txtPotB4.setColumns(10);
txtPotB4.setBounds(122, 401, 86, 20);
contentPane.add(txtPotB4);
arrPotBaits[3] = txtPotB4;
txtPotB5 = new JTextField();
txtPotB5.setDropMode(DropMode.INSERT);
txtPotB5.setColumns(10);
txtPotB5.setBounds(122, 432, 86, 20);
contentPane.add(txtPotB5);
arrPotBaits[4] = txtPotB5;
txtAraB1 = new JTextField();
txtAraB1.setDropMode(DropMode.INSERT);
txtAraB1.setColumns(10);
txtAraB1.setBounds(234, 308, 86, 20);
contentPane.add(txtAraB1);
arrAraBaits[0] = txtAraB1;
txtAraB2 = new JTextField();
txtAraB2.setDropMode(DropMode.INSERT);
txtAraB2.setColumns(10);
txtAraB2.setBounds(234, 339, 86, 20);
contentPane.add(txtAraB2);
arrAraBaits[1] = txtAraB2;
txtAraB3 = new JTextField();
txtAraB3.setDropMode(DropMode.INSERT);
txtAraB3.setColumns(10);
txtAraB3.setBounds(234, 370, 86, 20);
contentPane.add(txtAraB3);
arrAraBaits[2] = txtAraB3;
txtAraB4 = new JTextField();
txtAraB4.setDropMode(DropMode.INSERT);
txtAraB4.setColumns(10);
txtAraB4.setBounds(234, 401, 86, 20);
contentPane.add(txtAraB4);
arrAraBaits[3] = txtAraB4;
txtAraB5 = new JTextField();
txtAraB5.setDropMode(DropMode.INSERT);
txtAraB5.setColumns(10);
txtAraB5.setBounds(234, 432, 86, 20);
contentPane.add(txtAraB5);
arrAraBaits[4] = txtAraB5;
txtLocation = new JTextField();
txtLocation.setBounds(20, 492, 248, 20);
contentPane.add(txtLocation);
txtLocation.setColumns(10);
}
}
the problem is with all the txtARAB#, txtTomB#, txtPotB# and txtLocation
I haven't worked out the code yet. But my simple guess is when you "ALT + TAB" back to the swing form your preferred textfield may have lost focus.. so what you can do is add a WindowFocusListener to your JFrame and whenever the focus is gained, transfer the focus to the preferred textbox.. like this..
public class TestFrame extends JFrame {
private JTextField txtBox1;
private JTextField txtBox2;
public TestFrame() {
// init the components and add to frame
this.addWindowFocusListener(new WindowFocusListener() {
#Override
public void windowLostFocus(WindowEvent arg0) {
System.out.println("Window Focus Lost");
}
#Override
public void windowGainedFocus(WindowEvent arg0) {
System.out.println("Window Focus Gained");
txtBox2.requestFocus();
}
});
}
}
So now everytime you get back to your Jframe your textbox would have got focus..

Categories