Learning Event Handling

So I've been doing a bit of java in college and looking ahead as we are not on this yet I have tried to teach myself event handling and gui's by looking at future exam questions etc. and so far i have got gui's fairly handy but event handling not so much... I have been going at this for a while now and I just cant seem to get my head around them at all, I am trying to get that when the button is pressed it returns the length() of the JTextField into the JLabel, any help at all will be greatly appreciated. Thanks

   import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;

public class guiWithCatchBlock extends JFrame implements ActionListener, MouseListener {


guiWithCatchBlock() {

    super("Attempting Event Handling");

    Container c = getContentPane();
    JButton stringLengthButton = new JButton("Get String Length");
    JTextField inputField = new JTextField();
    JLabel outputLabel = new JLabel("String Length = ");
    stringLengthButton.addActionListener(this);
    inputField.addActionListener(this);
    outputLabel.addMouseListener(this);
    c.add(stringLengthButton,BorderLayout.NORTH);
    c.add(inputField,BorderLayout.CENTER);
    c.add(outputLabel,BorderLayout.SOUTH);
    setSize(400, 300);
    show();

}

public static void main(String args[]) {

    guiWithCatchBlock testAction = new guiWithCatchBlock();

}

public void actionPerformed(ActionEvent e) {

    System.out.print(paramString());

}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

Answers


You need to pass the ActionListener to the button

stringLengthButton.addActionListener(this);

You can use this as you are implementing the ActionListener Class. Now the event that will fire is:

public void actionPerformed(ActionEvent e) {
    // Do GUI manipulations
    System.out.print(paramString());
}

If you tried to declare your inputField and outputLabel inside your constructor, it will be hard to refer later, better to declare it as class member. In your actionPerform, tried to get the text length and re-label like this

@Override
public void actionPerformed(ActionEvent e) {
    outputLabel.setText("String Length = " + inputField.getText().length());
}

Completed code as

public class guiWithCatchBlock extends JFrame implements ActionListener, MouseListener {
    JTextField inputField;
    JLabel outputLabel;
    guiWithCatchBlock () {
        super("Attempting Event Handling");
        Container c = getContentPane();
        JButton stringLengthButton = new JButton("Get String Length");
        inputField = new JTextField();
        outputLabel = new JLabel("String Length = ");
        stringLengthButton.addActionListener(this);
        inputField.addActionListener(this);
        outputLabel.addMouseListener(this);
        c.add(stringLengthButton, BorderLayout.NORTH);
        c.add(inputField, BorderLayout.CENTER);
        c.add(outputLabel, BorderLayout.SOUTH);
        setSize(400, 300);
        show();

    }

    public static void main(String args[]) {
        guiWithCatchBlock testAction = new guiWithCatchBlock ();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        outputLabel.setText("String Length = " + inputField.getText().length());
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}

Need Your Help

Using multiple LayoutTransforms in one Control (Grid View Header) in WPF

c# wpf layouttransform

I am trying to build a grid of items, all dynamically (rows and columns) generated. I have a listview and gridview. I get all of the columns, and add them to the gridview. I then add all my rows to a

Can't read property of undefined javascript

javascript arrays json ejs

[ { dateTime: '2016-03-30 05:55:53', value: '4.0' },