I've recently had a question answered about how to open a login panel in my main method in another class. Because i have not yet had any lessons in Swing yet (only basic Java Programming) i have already stumbled upon another question.
How can i detect if a user has pressed a button in the JPanel and make it do something.
For example: User pressed login -> if (textfield1.getText() == "user"){ open another JFrame } -> etc.
Here is my main code:
import java.awt.*;
import javax.swing.*;
public class Corendon {
public static void main(String[] args) {
showLogin();
}
private static void showLogin(){
Login login = new Login();
JFrame loginFrame = new JFrame();
loginFrame.add(login);
loginFrame.pack();
loginFrame.setLocationRelativeTo(null);
loginFrame.setVisible(true);
loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Here is the Login class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Daan
*/
public class Login extends javax.swing.JPanel {
/**
* Creates new form Login
*/
public Login() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNINGds: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jPasswordField1 = new javax.swing.JPasswordField();
jTextField1 = new javax.swing.JTextField();
jComboBox1 = new javax.swing.JComboBox();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\Daan\\Dropbox\\HvA\\Programming\\Corendon\\corendon.png")); // NOI18N
jLabel2.setText("Username");
jLabel3.setText("Password");
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dutch", "English" }));
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Cancel");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(31, 31, 31)
.addComponent(jLabel1))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(14, 14, 14)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(29, 29, 29)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1))
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addContainerGap(22, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(16, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2)
.addComponent(jButton1))
.addGap(35, 35, 35))
);
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JPasswordField jPasswordField1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
So i tried working inside the Login.java and use the private method which is an event-handler. I made it something like:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String user = jTextField1.getText();
if(user == "user") {
jTextField1.setText("LOL");
}
}
Just to test if it actually even did what i wanted, but when i pressed the button it didn't do anything. After that i tried working it out in my main.java where i actually created the frame with the JPanel in it. After reading alot of tutorials, which always created the JPanel and JButton etc. all in the same java file (which is in my case the main.java).
So how do i detect if the login button was pressed, and then make it dispose of the JFrame and move on to another Method in which i create another JFrame with information in it.
I hope i am clear enough, if not please let me know.
EDIT: When i changed the test code to: user.equals("user") it did work. But now i need it to dispose the Login frame and access another method in my main.java. How can i achieve this from inside a private method in the Login.java?
Thanks in advance,