ensorj
ensorj

Reputation: 21

Working with ActionListeners and cannot set text field to not visible

I'm just trying to make a text field not visible until I click one of the numbered buttons. Ignore my poor spacing and lack of commenting as this is an early, rough draft. The text field is called inputField1 and I want it to be set Visible when I click the button labeled 1. Thanks

import java.lang.String.*;
import java.lang.Exception.*;
import java.util.EventObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Airplanes extends JFrame implements ActionListener {
    private static final int FRAME_WIDTH = 330;
    private static final int FRAME_HEIGHT = 300;

    private static final int FRAME_X_ORIGIN = 150;
    private static final int FRAME_Y_ORIGIN = 300;

    private static final int BUTTON_WIDTH = 50;
    private static final int BUTTON_HEIGHT = 30;

    private JTextField inputLine1;

    private JButton oneButton;
    private JButton twoButton;
    private JButton threeButton;
    private JButton fourButton;
    private JButton fiveButton;
    private JButton sixButton;
    private JButton sevenButton;
    private JButton eightButton;
    private JButton nineButton;
    private JButton tenButton;
    private JButton elevenButton;
    private JButton twelveButton;

    public static void main(String[] args) {
        Airplanes airplanes = new Airplanes();
        airplanes.setVisible(true);

    }

    @SuppressWarnings("null")
    public Airplanes() {
        Container contentPane;
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setResizable(false);
        setTitle("Island Airways");
        setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
        contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.setBackground(Color.white);

        oneButton = new JButton("1");
        oneButton.setBounds(10, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(oneButton);

        twoButton = new JButton("2");
        twoButton.setBounds(10, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(twoButton);

        threeButton = new JButton("3");
        threeButton.setBounds(60, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(threeButton);

        fourButton = new JButton("4");
        fourButton.setBounds(60, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(fourButton);

        fiveButton = new JButton("5");
        fiveButton.setBounds(110, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(fiveButton);

        sixButton = new JButton("6");
        sixButton.setBounds(110, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(sixButton);

        sevenButton = new JButton("7");
        sevenButton.setBounds(160, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(sevenButton);

        eightButton = new JButton("8");
        eightButton.setBounds(160, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(eightButton);

        nineButton = new JButton("9");
        nineButton.setBounds(210, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(nineButton);

        tenButton = new JButton("10");
        tenButton.setBounds(210, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(tenButton);

        elevenButton = new JButton("11");
        elevenButton.setBounds(260, 100, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(elevenButton);

        JButton twelveButton = new JButton("12");
        twelveButton.setBounds(260, 140, BUTTON_WIDTH, BUTTON_HEIGHT);
        contentPane.add(twelveButton);

        JButton firstClass = new JButton("First Class");
        firstClass.setBounds(50, 30, 100, 30);
        contentPane.add(firstClass);

        JButton coach = new JButton("Coach");
        coach.setBounds(175, 30, 100, 30);
        contentPane.add(coach);
        inputLine1 = new JTextField();
        inputLine1.setBounds(70, 200, 135, 30);
        contentPane.add(inputLine1);
        inputLine1.setVisible(false);

    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() instanceof JButton) {
            JButton clickedButton = (JButton) event.getSource();
            String buttonText = clickedButton.getText();
            if (buttonText.equals("First Class")) {
                inputLine1.setVisible(true);
                if (buttonText.equals("1")) {

                }
            }
        }
    }
}

Upvotes: 2

Views: 1876

Answers (2)

mprabhat
mprabhat

Reputation: 20323

You have implemented ActionListener but your buttons are not attached to any Listener, hence its not invoking your actionPerformed method.

You need to do add listners to your buttons like this:

oneButton.addActionListener(this);

Then change your actionPerfomed like:

public void actionPerformed(ActionEvent event) {
        if (event.getSource() instanceof JButton) {
            JButton clickedButton = (JButton) event.getSource();
            String buttonText = clickedButton.getText();
            if (buttonText.equals("First Class")) {
                //do something
            } else if (buttonText.equals("1")) {
                inputLine1.setVisible(true);
            }
        }
    }

Read this tutorial

Upvotes: 5

Balaswamy Vaddeman
Balaswamy Vaddeman

Reputation: 8530

When you are implementing ActionListener

  1. you have to implement it's methods

  2. you have to add that listener to a particular component you want.

You forgot to do second one.

oneButton.addActionListener(this);

you add the following code to your actionPerformed method.

if (buttonText.equals("1")) {
                inputLine1.setVisible(true);
            }

Upvotes: 2

Related Questions