JTextField doesn't respond - java

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..

Related

Looking to validate both textField and comboBox in Java Swing GUI

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);
}
}

set JTextField Size according to text in JTextArea

In my program i have to make JTextField Dynamically when JButton pressed. And i have to set JTextField size according to text inside in JTextArea.
The process is whenever i typed any message in JTextArea and pressed JButton it show that message inside JTextField. And JTextField size is according to the text that i want to display in JTextArea.
this is my code
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.GroupLayout.Alignment;
import com.alee.laf.WebLookAndFeel;
import javax.swing.JTextField;
import net.miginfocom.swing.MigLayout;
public class ThirdApplication {
private JFrame frame;
private JPanel panel_2;
private JTextArea SendText;
private int y=20;
private JTextField DisplayText;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WebLookAndFeel.install();
ThirdApplication window = new ThirdApplication();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ThirdApplication() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 614, 649);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 621, Short.MAX_VALUE)
);
panel.setLayout(null);
JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 534, 608, 87);
panel.add(panel_1);
panel_1.setLayout(null);
JButton btnNewButton = new JButton("Send");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel_2.revalidate();
panel_2.repaint();
DisplayText = new JTextField();
DisplayText.setFont(new Font("Georgia", Font.PLAIN, 15));
DisplayText.setEditable(false);
DisplayText.setBounds(10,y,0,0);
DisplayText.setText(SendText.getText());
panel_2.add(DisplayText,"width 179, height 40,wrap");
DisplayText.setColumns(10);
y=y+38;
}
});
btnNewButton.setFont(new Font("Georgia", Font.BOLD, 15));
btnNewButton.setBounds(509, 23, 89, 40);
panel_1.add(btnNewButton);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 486, 65);
panel_1.add(scrollPane);
SendText = new JTextArea();
SendText.setFont(new Font("Georgia", Font.PLAIN, 15));
scrollPane.setViewportView(SendText);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(0, 0, 608, 534);
panel.add(scrollPane_1);
panel_2 = new JPanel();
scrollPane_1.setViewportView(panel_2);
panel_2.setLayout(new MigLayout("wrap"));
frame.getContentPane().setLayout(groupLayout);
}
}

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);
}
} );

Write my ComboBox value to file

I've got a problem with this, I've got text from comboBox saved into text variable but now I can't make it to be saved to the file like 'num1' and 'num2' after I click a buttom. I know I am missing something simple - or everything is wrong anyways please help! Thank!
package windowbuilded.views;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
public class WiewWindow {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WiewWindow window = new WiewWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public WiewWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int num1, num2, ans2, combo;
num1=Integer.parseInt(textField.getText());
num2=Integer.parseInt(textField_1.getText());
ans2 = num1 + num2;
textField_2.setText(Integer.toString(ans2));
try{
File dir = new File("C:\\test");
File[] directoryListing = dir.listFiles();
if (directoryListing != null) {
for (File child : directoryListing) {
FileWriter writer = new FileWriter(child, true);
PrintWriter out = new PrintWriter(writer);
out.println(num1);
out.println(num2);
out.close();
}
}
} catch (IOException e) {
// do something
}
}
});
btnNewButton.setBounds(124, 206, 89, 23);
frame.getContentPane().add(btnNewButton);
textField = new JTextField();
textField.setBounds(10, 34, 86, 20);
frame.getContentPane().add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(124, 34, 86, 20);
frame.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(124, 101, 86, 20);
frame.getContentPane().add(textField_2);
textField_2.setColumns(10);
JComboBox<String> comboBox = new JComboBox<String>();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"Yes", "No", "Blah!"}));
String text = (String)comboBox.getSelectedItem();
System.out.println(text);
comboBox.setBounds(265, 101, 121, 20);
frame.getContentPane().add(comboBox);
}
}

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.

Categories