My too simple Swing code is causing the whole system to freeze. I'm learning java still :)
this is the actionEvent that is causing the problem
#Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
System.out.println("hej");
}
I haven't wrote anything in there yet(still testing) because obviously it is in the code here in the class xD
public class NewAccount implements ActionListener {
static JFrame frame = new JFrame();
static JButton createAccountButton = new JButton();
static JButton haveAnAccount = new JButton();
static JLabel usernameLabel = new JLabel();
static JLabel passwordLabel = new JLabel();
static JTextField usernameField = new JTextField();
static JPasswordField passwordField = new JPasswordField();
NewAccount() {
frame = new JFrame();
createAccountButton = new JButton("Create account");
haveAnAccount = new JButton("Already have an account?");
usernameLabel = new JLabel("New username");
passwordLabel = new JLabel("New Password");
usernameField = new JTextField(20);
passwordField = new JPasswordField(20);
//Frame
frame.setLayout(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(400, 250);
frame.setTitle("Create new account");
//Buttons
frame.add(createAccountButton);
createAccountButton.setLocation(20, 130);
createAccountButton.setSize(230, 30);
createAccountButton.setFocusable(false);
createAccountButton.addActionListener(new NewAccount());
frame.add(haveAnAccount);
haveAnAccount.setLocation(20, 170);
haveAnAccount.setSize(230, 30);
haveAnAccount.setFocusable(false);
//Labels
frame.add(usernameLabel);
usernameLabel.setLocation(20, 20);
usernameLabel.setSize(130, 30);
frame.add(passwordLabel);
passwordLabel.setLocation(20, 50);
passwordLabel.setSize(130, 30);
//Fields
frame.add(usernameField);
usernameField.setLocation(150, 20);
usernameField.setSize(165, 25);
frame.add(passwordField);
passwordField.setLocation(150, 50);
passwordField.setSize(165, 25);
}
#Override
public void actionPerformed(ActionEvent e) {
String username = usernameField.getText();
String password = passwordField.getText();
System.out.println("hej");
}
}
I have no idea what is wrong with it but when I delete the actionlistener it works without freezing my whole system.
createAccountButton.addActionListener(new NewAccount());
Congratulations, you created an infinite loop. That eats your memory.
Maybe you mean to pass this?
Change this line
createAccountButton.addActionListener(new NewAccount());
to
createAccountButton.addActionListener(this);
Solved:
it was a simple mistake but took me so many hours that I had to make a question here. the only problem was to put this
createAccountButton.addActionListener(new NewAccount());
before setting it's bounds and location. hope if anyone had the same problem sees this.
Related
So I want to store a string value in a variable, the value is given through a JTextField and after a confirm button is clicked, I want it to store what's written in the text field in a string variable.
This is the relevant part of the code:
public class Window {
private JButton confirm;
private JTextField textfield;
private JLabel label;
public void drawWindow() {
JFrame window = new JFrame("CountryQuiz");
ClickChecker click = new ClickChecker();
JPanel panel = new JPanel();
panel.setBounds(40, 80, 200, 200);
panel.setBackground(Color.green);
JTextField t1 = new JTextField("Enter country...");
t1.setBounds(50, 100, 200, 30);
window.add(t1);
JButton confirm = new JButton("Confirm");
confirm.setBounds(50, 50, 95, 30);
confirm.addActionListener(click);
window.add(confirm);
window.setSize(400, 400);
window.setLayout(null);
window.setVisible(true);
window.add(panel);
}
private class ClickChecker implements ActionListener {
public void actionPerformed(ActionEvent e) {
String answer = textfield.getText();
System.out.println(answer);
}
}
}
Results in the following error:
Cannot invoke "javax.swing.JTextField.getText()" because "this.this$0.textfield" is null
Your textfield variable is only declared but never used.
Replace the below
JTextField t1 = new JTextField("Enter country...");
t1.setBounds(50, 100, 200, 30);
window.add(t1);
With
textfield = new JTextField("Enter country...");
textfield.setBounds(50, 100, 200, 30);
window.add(textfield);
this button does nothing what would be the mistake?
its a login, I dont know why it doesnt work seems pretty well, could somebody help me to indentify the issue?.....................................................................................................................
package Operaciones_Logicas;
import Mainargs.FirstClass;
import java.awt.event.*;
import javax.swing.*;
public class Main1 extends JFrame implements ActionListener {
private JLabel usuario;
private JLabel contraseña;
public JButton blogin;
public JTextField jtusuario, jtcontra;
public static String susuario = "", scontra = "";
public Main1() {
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Cajero Automatico");
JLabel usuario = new JLabel();
usuario.setVisible(true);
usuario.setBounds(20, 100, 100, 50);
add(usuario);
JLabel contraseña = new JLabel();
contraseña.setBounds(20, 300, 100, 50);
add(contraseña);
JButton blogin = new JButton("Login");
blogin.setBounds(50, 400, 100, 30);
blogin.addActionListener(this);
add(blogin);
JTextField jtusuario = new JTextField();
jtusuario.setBounds(20, 120, 150, 30);
add(jtusuario);
JTextField jtcontra = new JTextField();
jtcontra.setBounds(20, 150, 150, 30);
add(jtcontra);
}
//control para el login
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == blogin) {
susuario = jtusuario.getText();
scontra = jtcontra.getText();
if (susuario.equals("josmart96") && (scontra.equals("rojo2000"))) {
FirstClass secondwindow = new FirstClass();
secondwindow.setBounds(0, 0, 600, 360);
secondwindow.setVisible(true);
secondwindow.setResizable(false);
secondwindow.setLocationRelativeTo(null);
this.setVisible(false);
} else {
JOptionPane.showMessageDialog(null, "Usuario y/o Contraseña
incorrectas");
}
}
}
public static void main(String[] args) {
Main1 firstwindow = new Main1();
firstwindow.setBounds(0, 0, 360, 600);
firstwindow.setVisible(true);
firstwindow.setResizable(false);
firstwindow.setLocationRelativeTo(null);
}
}
no erros just doesnt work, when hit button does nothing I can compile it and everything is good I think it could be the IDE itself
You are using local variable blogin instead of using instance variable blogin
Inside the contructor, change
JButton blogin = new JButton("Login");
to
blogin = new JButton("Login");
you also need to use other declared instance variables rather than declaring local variables inside the constructor
I have a JTextField in a JFrame. The caret blinks between a normal looking caret and a half drawn caret.
The caret only shows at the top and bottom. I have tried using custom carets but they all only show part of the graphic. The bug is the same for the password field too. I don't know if it's relevant but I'm on a mac and I don't own a windows machine to test if the bug occurs on windows as well.
Here is an MCVE of my window (as short as I could make it):
public class Login1 extends JFrame implements Serializable {
private static final long serialVersionUID = 1L;
private JPanel pnl_mainPanel;
private JButton btn_login_main, btn_newAccount, btn_seeAccounts, btn_login_login, btn_cancel_login, btn_next, btn_cancel_new;
private JTextField tf_username_login, tf_username_new;
private JPasswordField pf_password, pf_password_new, pf_password_confirm;
private JLabel lbl_username_login, lbl_password_login, lbl_username_new, lbl_password_new, lbl_password_confirm;
private static final String TITLE = "Login";
private final int WINDOW_WIDTH = 400;
private final int WINDOW_HEIGHT = 300;
private final Dimension WINDOW_DIM = new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT);
public Login1() {
super(TITLE);
setSize(WINDOW_DIM);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout());
buildPanel();
add(pnl_mainPanel);
setVisible(true);
setResizable(false);
}
private void buildPanel() {
pnl_mainPanel = new JPanel();
btn_login_main = new JButton("Login");
btn_newAccount = new JButton("New Account");
btn_seeAccounts = new JButton("ℹ");
// login page
btn_login_login = new JButton("Login");
btn_cancel_login = new JButton("Cancel");
tf_username_login = new JTextField();
pf_password = new JPasswordField();
lbl_username_login = new JLabel("Username:");
lbl_password_login = new JLabel("Password:");
// new account page
btn_next = new JButton("Next");
btn_cancel_new = new JButton("Cancel");
tf_username_new = new JTextField();
pf_password_new = new JPasswordField();
pf_password_confirm = new JPasswordField();
lbl_username_new = new JLabel("Username:");
lbl_password_new = new JLabel("Password:");
lbl_password_confirm = new JLabel("<html>Confirm<br>Password:</html>");
pnl_mainPanel.setLayout(null);
pnl_mainPanel.add(btn_login_main);
pnl_mainPanel.add(btn_newAccount);
pnl_mainPanel.add(btn_seeAccounts);
btn_login_login.setSize(75, 30);
btn_login_login.setLocation(new Point(310, 175));
btn_cancel_login.setSize(75, 30);
btn_cancel_login.setLocation(new Point(310, 215));
tf_username_login.setSize(200, 25);
tf_username_login.setLocation(new Point(100, 178));
pf_password.setSize(200, 25);
pf_password.setLocation(new Point(100, 218));
lbl_username_login.setSize(100, 25);
lbl_username_login.setLocation(new Point(15, 178));
lbl_password_login.setSize(100, 25);
lbl_password_login.setLocation(new Point(15, 218));
btn_next.setSize(75, 30);
btn_next.setLocation(new Point(310, 160));
btn_cancel_new.setSize(75, 30);
btn_cancel_new.setLocation(new Point(310, 200));
tf_username_new.setSize(200, 25);
tf_username_new.setLocation(new Point(100, 163));
pf_password_new.setSize(200, 25);
pf_password_new.setLocation(new Point(100, 203));
pf_password_confirm.setSize(200, 25);
pf_password_confirm.setLocation(new Point(100, 243));
lbl_username_new.setSize(100, 25);
lbl_username_new.setLocation(new Point(15, 163));
lbl_password_new.setSize(100, 25);
lbl_password_new.setLocation(new Point(15, 203));
lbl_password_confirm.setSize(100, 30);
lbl_password_confirm.setLocation(new Point(15, 238));
btn_login_main.setSize(163, 100);
btn_login_main.setLocation(new Point(25, 150));
btn_newAccount.setSize(162, 100);
btn_newAccount.setLocation(new Point(213, 150));
btn_seeAccounts.setSize(20, 20);
btn_seeAccounts.setLocation(new Point(375, 255));
btn_login_main.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pnl_mainPanel.remove(btn_login_main);
pnl_mainPanel.remove(btn_newAccount);
pnl_mainPanel.remove(btn_next);
pnl_mainPanel.remove(btn_cancel_new);
pnl_mainPanel.remove(tf_username_new);
pnl_mainPanel.remove(pf_password_new);
pnl_mainPanel.remove(pf_password_confirm);
pnl_mainPanel.remove(lbl_username_new);
pnl_mainPanel.remove(lbl_password_new);
pnl_mainPanel.remove(lbl_password_confirm);
pnl_mainPanel.add(btn_login_login);
pnl_mainPanel.add(btn_cancel_login);
pnl_mainPanel.add(tf_username_login);
pnl_mainPanel.add(pf_password);
pnl_mainPanel.add(lbl_username_login);
pnl_mainPanel.add(lbl_password_login);
repaint();
tf_username_login.requestFocus();
}
});
btn_cancel_login.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pnl_mainPanel.remove(btn_login_login);
pnl_mainPanel.remove(tf_username_login);
pnl_mainPanel.remove(pf_password);
pnl_mainPanel.remove(btn_cancel_login);
pnl_mainPanel.remove(lbl_username_login);
pnl_mainPanel.remove(lbl_password_login);
pnl_mainPanel.add(btn_login_main);
pnl_mainPanel.add(btn_newAccount);
repaint();
}
});
btn_newAccount.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pnl_mainPanel.remove(btn_login_main);
pnl_mainPanel.remove(btn_newAccount);
pnl_mainPanel.add(btn_next);
pnl_mainPanel.add(btn_cancel_new);
pnl_mainPanel.add(tf_username_new);
pnl_mainPanel.add(pf_password_new);
pnl_mainPanel.add(pf_password_confirm);
pnl_mainPanel.add(lbl_username_new);
pnl_mainPanel.add(lbl_password_new);
pnl_mainPanel.add(lbl_password_confirm);
repaint();
tf_username_new.requestFocus();
}
});
btn_cancel_new.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
pnl_mainPanel.remove(btn_next);
pnl_mainPanel.remove(btn_cancel_new);
pnl_mainPanel.remove(tf_username_new);
pnl_mainPanel.remove(pf_password_new);
pnl_mainPanel.remove(pf_password_confirm);
pnl_mainPanel.remove(lbl_username_new);
pnl_mainPanel.remove(lbl_password_new);
pnl_mainPanel.remove(lbl_password_confirm);
pnl_mainPanel.add(btn_login_main);
pnl_mainPanel.add(btn_newAccount);
repaint();
}
});
}
public static void main(String[] args){
Login1 l = new Login1();
}
}
Thanks in advance!
I can confirm that this happens on my Mac, and does not happen on my Linux.
It can be easily solved by changing the height of the field to at least 28.
tf_username_login.setSize(200, 28);
My guess is that Mac OS X cannot create a field whose height is less than 28 pixels (though this may be a question of font choice etc., I haven't tested it that far). For some reason, it draws the full caret at full height (28 pixels), but erases it at the user indicated size (25) which causes the phenomenon you have noticed.
I am trying to make the text field for quiz_7 set up, however, it is not showning up like the remaning six, i tried putting an parameter as 360, no text field. Changed it 340, but i only see half a textfield.
import java.awt.Container;
import javax.swing.*;
public class QuizEntry {
JPanel textPanel,panelForTextFields;
JLabel Entry_for_Quizes, quiz1, quiz2, quiz3, quiz4, quiz5, quiz6;
JTextField quiz_1, quiz_2, quiz_3, quiz_4, quiz_5, quiz_6;
JTextField quiz_7;
private JLabel quiz7;
public JPanel createContentPane() {
// We create a bottom JPanel to place everything on.
JPanel totalGUI1 = new JPanel();
totalGUI1.setLayout(null);
Entry_for_Quizes = new JLabel("Quiz Entry ");
Entry_for_Quizes.setLocation(0, 0);
Entry_for_Quizes.setSize(400, 400);
Entry_for_Quizes.setHorizontalAlignment(4);
totalGUI1.add(Entry_for_Quizes);
// Creation of a Panel to contain the JLabels
textPanel = new JPanel();
textPanel.setLayout(null);
textPanel.setLocation(10, 35);
textPanel.setSize(100, 600);
totalGUI1.add(textPanel);
// Username Label
quiz1 = new JLabel("Quiz 1");
quiz1.setLocation(-20, 0);
quiz1.setSize(70, 40);
quiz1.setHorizontalAlignment(4);
textPanel.add(quiz1);
// Login Label
quiz2 = new JLabel("Quiz 2");
quiz2.setLocation(-20, 60);
quiz2.setSize(70, 40);
quiz2.setHorizontalAlignment(4);
textPanel.add(quiz2);
// Username Label
quiz3 = new JLabel("Quiz 3");
quiz3.setLocation(-20, 120);
quiz3.setSize(70, 40);
quiz3.setHorizontalAlignment(4);
textPanel.add(quiz3);
// Login Label
quiz4 = new JLabel("Quiz 4");
quiz4.setLocation(-20, 180);
quiz4.setSize(70, 40);
quiz4.setHorizontalAlignment(4);
textPanel.add(quiz4);
// Username Label
quiz5 = new JLabel("Quiz 5");
quiz5.setLocation(-20, 240);
quiz5.setSize(70, 40);
quiz5.setHorizontalAlignment(4);
textPanel.add(quiz5);
// L
quiz6 = new JLabel("Quiz 6");
quiz6.setLocation(-20, 300);
quiz6.setSize(70, 40);
quiz6.setHorizontalAlignment(4);
textPanel.add(quiz6);
quiz7 = new JLabel("Quiz 7");
quiz7.setLocation(-20, 350);
quiz7.setSize(70, 40);
quiz7.setHorizontalAlignment(4);
textPanel.add(quiz7);
//////////////////////////////////////////////////////////////
panelForTextFields = new JPanel();
panelForTextFields.setLayout(null);
panelForTextFields.setLocation(110, 40);
panelForTextFields.setSize(100, 350);
totalGUI1.add(panelForTextFields);
// quiz Textfield
quiz_1 = new JTextField(8);
quiz_1.setLocation(0, 0);
quiz_1.setSize(100, 30);
panelForTextFields.add(quiz_1);
quiz_2 = new JTextField(8);
quiz_2.setLocation(0, 60);
quiz_2.setSize(100, 30);
panelForTextFields.add(quiz_2);
quiz_3 = new JTextField(8);
quiz_3.setLocation(0, 120);
quiz_3.setSize(100, 30);
panelForTextFields.add(quiz_3);
quiz_4 = new JTextField(8);
quiz_4.setLocation(0, 180);
quiz_4.setSize(100, 30);
panelForTextFields.add(quiz_4);
quiz_5 = new JTextField(8);
quiz_5.setLocation(0, 240);
quiz_5.setSize(100, 30);
panelForTextFields.add(quiz_5);
quiz_6 = new JTextField(8);
quiz_6.setLocation(0, 300);
quiz_6.setSize(100, 30);
panelForTextFields.add(quiz_6);
quiz_7 = new JTextField(8);
quiz_7.setLocation(0, 340);
quiz_7.setSize(100, 30);
panelForTextFields.add(quiz_7);
return totalGUI1;
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("Quiz Entry");
QuizEntry demo = new QuizEntry();
frame.setContentPane(demo.createContentPane());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 700);
frame.setVisible(true);
}
public static void main(String[] args) {
// Schedule a job for the event-dispatching thread:
// creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
Basically change size: for panelForTextFields. From 350 to 500 for example.
To make code a bit generic I would write something like:
panelForTextFields.setSize(100, 6*80);
where 6 is count of JTextFields.
If we will go further, I would create list of JTextField like:
List<JTextField> list = new ArrayList<JTextField>();
list.add(quiz_1);
list.add(quiz_2);
list.add(quiz_3);
list.add(quiz_4);
list.add(quiz_5);
list.add(quiz_6);
and after would write:
final int GAP = 40;
panelForTextFields.setSize(100, 6*( quiz_1.getHeight() + GAP));
Since you used the same logic to configure JTextfields you can use now list like:
quiz_1 = new JTextField(8);
panelForTextFields.add(quiz_1);
quiz_2 = new JTextField(8);
panelForTextFields.add(quiz_2);
quiz_3 = new JTextField(8);
panelForTextFields.add(quiz_3);
quiz_4 = new JTextField(8);
panelForTextFields.add(quiz_4);
quiz_5 = new JTextField(8);
panelForTextFields.add(quiz_5);
quiz_6 = new JTextField(8);
panelForTextFields.add(quiz_6);
quiz_7 = new JTextField(8);
panelForTextFields.add(quiz_7);
list.add(quiz_1);
list.add(quiz_2);
list.add(quiz_3);
list.add(quiz_4);
list.add(quiz_5);
list.add(quiz_6);
list.add(quiz_7);
for(int k = 0; k<list.size(); k++){
JTextField f = list.get(k);
f.setLocation(0, k*60);
f.setSize(100, 30);
}
final int GAP = 40;
panelForTextFields.setSize(100, 6*( quiz_1.getHeight() + GAP));
I have a form , That when i click to save button, "Yes" String should display on my console!
(I use "Yes" String for test!)
But does not work when clicked.
My code:
public final class NewUserFrame1 extends JFrame implements ActionListener {
UserInformation userinfo;
JLabel fnamelbl;
JLabel lnamelbl;
JTextField fntf;
JTextField lntf;
JLabel gndlnl;
JRadioButton malerb;
JRadioButton femalerb;
ButtonGroup bgroup;
JLabel registnm;
JButton savebt;
JButton cancelbt;
JLabel showreglbl;
public NewUserFrame1() {
add(rowComponent(), BorderLayout.CENTER);
setLocation(200, 40);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
}
public JPanel rowComponent() {
JPanel panel = new JPanel();
fnamelbl = new JLabel("First name");
lnamelbl = new JLabel("Last Name");
JLabel fntemp = new JLabel();
JLabel lntemp = new JLabel();
fntf = new JTextField(10);
lntf = new JTextField(10);
gndlnl = new JLabel("Gender");
malerb = new JRadioButton("Male");
femalerb = new JRadioButton("Female");
bgroup = new ButtonGroup();
bgroup.add(malerb);
bgroup.add(femalerb);
registnm = new JLabel("Registration ID is:");
showreglbl = new JLabel("");
JLabel regtemp = new JLabel();
savebt = new JButton("Save");
cancelbt = new JButton("Cancell");
JLabel buttontemp = new JLabel();
panel.add(fnamelbl);
panel.add(fntf);
panel.add(fntemp);
panel.add(lnamelbl);
panel.add(lntf);
panel.add(lntemp);
panel.add(gndlnl);
JPanel radiopanel = new JPanel();
radiopanel.setLayout(new FlowLayout(FlowLayout.LEFT));
radiopanel.add(malerb);
radiopanel.add(femalerb);
panel.add(radiopanel);
panel.add(new JLabel());
panel.add(registnm);
panel.add(showreglbl);
panel.add(regtemp);
panel.add(savebt);
panel.add(cancelbt);
panel.add(buttontemp);
panel.setLayout(new SpringLayout());
SpringUtilities.makeCompactGrid(panel, 5, 3, 50, 10, 80, 60);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NewUserFrame1 newUserFrame1 = new NewUserFrame1();
}
});
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == savebt) {
System.out.print("Yes");
}
}
}
You need to add an ActionListener to your button like so:
savebt.addActionListener(this);
or with an anonymous class, like so:
savebt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// your code.
}
});
Using anonymous classes (or inner classes) is better because you can't have more than one actionPerformed() method in a given class.
You need to tell the button to invoke the ActionListener:
savebt = new JButton("Save");
savebt.addActionListener(this);
Note if you intend to use the same method for the save and cancel buttons, you'll need to differentiate, perhaps by comparing the source of the ActionEvent against the two buttons.