Textfield and JPasswordField ActionListener not working
Code :
package theatre;
import java.awt.event.KeyEvent;
import java.awt.*;
import javax.swing.*;
import javax.swing.JTabbedPane;
import javax.swing.JFrame;
import java.awt.event.*;
public class Theatre extends JFrame implements ActionListener {
static JFrame Frame = new JFrame("Theatre");
static NewJPanel pane = new NewJPanel();
static NewJPanel1 pane2 = new NewJPanel1();
static JTabbedPane Tabbed_Pane = new JTabbedPane();
static final NewJPanel Admin_Login = new NewJPanel();
static NewJPanel1 Set_Prices = new NewJPanel1();
static String F;
static String P;
static String Username = "Team1";
static String Password = "Team1";
static boolean Login;
static boolean Pass;
public Theatre () {
//set up
setLocation(400,300);
setSize(300,300);
//setting up tabs
Tabbed_Pane.addTab("Admin", null, null,"Administrative Priveleges");
Tabbed_Pane.setMnemonicAt(0, KeyEvent.VK_1);
Tabbed_Pane.setComponentAt(0, pane);
//admin login
Admin_Login.jTextField1.addActionListener(this);
// Admin_Login.jTextField1.addActionListener(
// new ActionListener() {
// @Override
// public void actionPerformed(ActionEvent e) {
// F = e.getActionCommand();
// Admin_Login.jPasswordField1.setText(F);
// if(F.equals(Username)){
// System.out.print("yes");
// Login = true;
// } else {
// Login = false;
// }
// }
// }
// );
//admin password
Admin_Login.jPasswordField1.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
char[] p = Admin_Login.jPasswordField1.getPassword();
P = e.getActionCommand();
if(P.equals(Password)){
System.out.print("yes");
Pass = true;
} else {
Pass = false;
}
}
}
);
//start button
Admin_Login.jButton1.setActionCommand("Start");
Admin_Login.jButton1.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Start"));
char[] p = Admin_Login.jPasswordField1.getPassword();
F = Admin_Login.jTextField1.getText();
}
}
);
}
public static void main(String[] args) {
Theatre GUI = new Theatre();
GUI.setVisible(true);
GUI.setAlwaysOnTop(true);
GUI.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
GUI.getContentPane().add(Tabbed_Pane);
}
@Override
public void actionPerformed(ActionEvent e) {
F = e.getActionCommand();
}
}
Code :
package theatre;
public class NewJPanel extends javax.swing.JPanel {
public NewJPanel() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jPasswordField1 = new javax.swing.JPasswordField();
jButton1 = new javax.swing.JButton();
jTextField1.setText("");
jTextField1.setEditable(true);
jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Login ");
jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel2.setText("Username:");
jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
jLabel3.setText("Password: ");
jButton1.setText("Start");
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.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPasswordField1))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jButton1)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.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.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addComponent(jButton1)
.addContainerGap(60, Short.MAX_VALUE))
);
}
public javax.swing.JButton jButton1;
public javax.swing.JLabel jLabel1;
public javax.swing.JLabel jLabel2;
public javax.swing.JLabel jLabel3;
public javax.swing.JPasswordField jPasswordField1;
public javax.swing.JTextField jTextField1;
}
Re: Textfield and JPasswordField ActionListener not working
Please read the link in my signature on asking questions the smart way. What exactly do you mean by not working? Where is your SSCCE?
Re: Textfield and JPasswordField ActionListener not working
Quote:
Originally Posted by
KevinWorkman
Please read the link in my signature on asking questions the smart way. What exactly do you mean by not working? Where is your
SSCCE?
Sorry. My actionlistener is not working for any of my components.
Re: Textfield and JPasswordField ActionListener not working
Define 'not working'. Have you added println debug statements to see if they are getting called? Best to read post #2 again, in particular the link to the page describing what an SSCCE is.