2
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

public class Loginpanel extends JFrame {

    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField usr;
    private JPasswordField pass;
    private JButton submit;
    private JRadioButton admin;
    private JRadioButton student;
    private ButtonGroup type;

    public Loginpanel() 
    {
        super("User Login");
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setSize( 350, 200 ); // set frame size

        FieldHandler handler = new FieldHandler();


        setLayout(new GridBagLayout());
        setResizable(false);

        GridBagConstraints c = new GridBagConstraints();

        label3 = new JLabel();
        label3.setText("Login");
        c.weightx =0;
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.VERTICAL;
        add(label3,c);

        label1 = new JLabel("Username:");
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(label1,c);

        usr = new JTextField(10);
        c.weightx = 0;
        c.gridx = 2;
        c.gridy = 2;
        c.ipadx = 2;

        add(usr,c);

        label2 = new JLabel("Password:");
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.fill = GridBagConstraints.HORIZONTAL;
        add(label2,c);

        pass = new JPasswordField(10);
        c.weightx = 0;
        c.gridx = 2;
        c.gridy = 3;
        c.ipadx= 2;
        add(pass,c);

        admin = new JRadioButton( "Admin", true );
        c.weightx = 0;
        c.gridx = 0;
        c.gridy = 4;
        add(admin,c);

        student = new JRadioButton( "Student", false );
        c.weightx = 0;
        c.gridx = 1;
        c.gridy = 4;
        add(student,c);

        submit = new JButton("Submit");
        c.weightx = 0;
        c.gridx = 1;
        c.gridy = 5;
        add(submit,c);

        type = new ButtonGroup();
        type.add(admin);
        type.add(student);

        usr.addActionListener( handler );
        pass.addActionListener( handler );
        submit.addActionListener(handler);

    }

    class FieldHandler implements ActionListener{

        public void actionPerformed(ActionEvent event)  {
            String string ="";
            if(event.getSource() == usr){
                string = String.format("%s",event.getActionCommand());

            }
            else if(event.getSource() == pass){
                string = String.format("%s",event.getActionCommand());

            }
            else if(event.getSource() == submit){
                string = "Submit Button";

            }
            JOptionPanel.showMessageDialog( null, string );
        }

    }

}

I am trying to make a Online examination system in Java, and i am new to the language. So i have made the neat form, with text fields, radio buttons, and a submit button. I checked out the event handler functions, and i am pretty much stuck right now. The event handler invokes the element which has an operation on it.Thats pretty much fine, but i cannot find a way to get the data on every element. event.getActionCommand() gets the information of just the element which invoked the handler. Another thing is I am not sure how to change the frame after the results from the database is fetched successfully, and if the password mismatches or no such user is found then it just shows a error message(I know it would be done by Joptionpanel) and returns.

The class which has the main function is

import java.awt.GridBagLayout;
import javax.swing.JFrame;

public class Start {

    public static void main(String args[]) {
        Loginpanel login = new Loginpanel();
        login.setVisible( true ); // display frame
    }

}
XuryaX
  • 41
  • 1
  • 7
  • What data do you want to get from the Element? If you want to get the user input data, you can do like "if(event.getSource() instanceof JTextField) {JTextField tf = (JTextField)event.getSource(); String content = tf.getText();}",but I suggest you don't use FieldHandler class, just use usr.addActionListener(new ActionListener(){@override ActionPerformed(){}}) – Sstx Jul 18 '13 at 07:38
  • Before, proceeding, do `setActionCommand(...)` on the `JPasswordField`, since password in it can be accessed in terms of `String` as cited in this [thread](http://stackoverflow.com/q/17693730/1057230), which is not a good thingy for security reasons :-) Moreover, do not access password the way you doing, visit that thread for more info :-) – nIcE cOw Jul 18 '13 at 07:45

2 Answers2

2

The FieldHandler is an inner class of the LoginPanel. It thus has access to all the fields of the LoginPanel:

class FieldHandler implements ActionListener{

    public void actionPerformed(ActionEvent event)  {
        if (event.getSource() == submit){
            String user = usr.getText(); 
            // or String user = LoginPanel.this.usr.getText(); 
            char[] password = pass.getPassword();
            // do whatever you want with the user and password
        }
        ...
    }

}

Learn about nested classes in the Java tutorial (which also covers Swing in depth, by the way).

It's a good practice to use a separate class for every listener rather than a unique class with a long chain of if blocks testing where the event comes from.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

an actioncommand has to be set with setActionCommand() before you can ask for it

mspringsits
  • 43
  • 1
  • 9