Java Swing button actionEvent

Chaves MainMan

sorry to bother everyone.

Overall problem: I'm trying to open a dialogue box let the user enter something then close it

Issue: - A function is not being called (i think) - The main problem is when i use debug it works fine so Its difficult for me to track down the problem

I'm having trouble with JButtons, it works in debug but not in normal run. this was probably because i was using an infinite loop. someone online suggested i used SwingUtilities but that didn't work (at least i don't think.

/**
 *
 * @author Deep_Net_Backup
 */
public class butonTest extends JFrame  {
String name;
boolean hasValue;

//name things
private JLabel m_nameLabel;
private JTextField m_name;

//panel
private JPanel pane;

//button
private JButton m_submit;

//action listener for the button submit
class submitListen implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        submit();
        System.out.println("Test");
    }
}

//constructor
public butonTest(){
    //normal values
    name = null;
    hasValue = false;
    //create the defauts
    m_nameLabel = new JLabel("Name:");
    m_name = new JTextField(25);
    pane = new JPanel();
    m_submit = new JButton("Submit");
    m_submit.addActionListener(new submitListen());
    //

    setTitle("Create Cat");
    setSize(300,200);
    setResizable(false);

    //add components
    pane.add(m_nameLabel);
    pane.add(m_name);

    pane.add(m_submit);

    add(pane);
    //last things
    setVisible(true);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

//submit
private void submit()
{
    System.out.println("submit");
    name = m_name.getText();
    hasValue = true;
}

//hasValue
public boolean hasValue()
{
    return(hasValue);

}

//get the text name
public String getName()
{
    return(name);
}

public void close()
{
    setVisible(false);
    dispose();
}

public static void main(String[] args)
{

    /* Test 1
    boolean run = true;
    String ret = new String();
    butonTest lol = new butonTest();

    while(run)
    {
        if(lol.hasValue())
        {
            System.out.println("Done");
            run = false;
            ret = new String(lol.getName());
            lol.close();
        }
    }



    System.out.println(ret);*/

    //Tset 2
    /*
    SwingUtilities.invokeLater(new Runnable(){
        @Override
        public void run() {
            butonTest lol = new butonTest();
            if(lol.hasValue())
            {
                System.out.println(lol.getName());
            }
        }
    });*/

}

}

Edit: How its not working: When i run Test the program will print test and submit then it should change the hasValue to true. this will (hopefully) allow the if statement to run to print done. This does not happen.

Edit 2: I have just added a few more lines for further testing 2 prints and this seems to have solved the issue (but this is bad) System.out.println("hasValue " + hasValue); -> to the hasValue() function System.out.println("set to true"); -> submit() function

Oliver Watkins

You are doing something far too complicated than is necessary. Instead of having the listener as a seperate class, you could have it as an anonymous class. That way you can get a handle on the outer class (butonTest.this), and call any method you want on it.

m_submit.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
        submit();
        System.out.println("Test");
        butonTest.this.close();
    }
});

I'm not sure what you are trying to do with the infinite loop. It would have run to completion before you show the dialog anyway.

It would help to read up a bit on how Event-Handling works in Swing :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Java swing ActionEvent for button click

From Java

How to implement multiple buttons using Java's Swing library and ActionEvent

From Java

Java Swing button colors

From Dev

ActionEvent button - TextField JavaFX

From Java

Java Swing: adding button to TitledBorder

From Dev

Java Swing Making On/Off Button

From Dev

Java swing bug with custom button

From Java

Java Convert MouseEvent to ActionEvent

From Dev

ActionEvent get source of button JavaFX

From Dev

ActionEvent from a dynamic SWT button

From Dev

how to disable one button but not all in Java Swing?

From Java

Java - is there a way to hide the close button in a swing application?

From Java

Not able to enable a button after it is disabled in java swing

From Java

Programmatically clicking a GUI button in Java Swing

From Dev

Java - swing timer in WHILE, with enable / disable button

From Dev

Waiting for a button to be pressed in Java Swing before moving on?

From Dev

Open a new panel via a button Java Swing

From Java

Java Swing: How JToolbar changes appearance of a button?

From Dev

How to update button colors continually [JAVA SWING]

From Dev

Can't make button visible in Java Swing

From Dev

Java Swing open form on button click

From Dev

Java Swing JComboBox/ Button edit parameters

From Dev

Calling methods in a Java Fx ActionEvent

From Dev

Clicking on one button calls the actionListener of another button in JAVA Swing

From Java

Javafx button sending arguments to ActionEvent function

From Dev

JAVA - Can't hide button and text field in Java Swing

From Dev

JAVA - Press button when user hits enter java swing

From Dev

Java swing - when cancel button clicked don't loop

From Java

How do I use the Button Group Swing control in Java?

Related Related

  1. 1

    Java swing ActionEvent for button click

  2. 2

    How to implement multiple buttons using Java's Swing library and ActionEvent

  3. 3

    Java Swing button colors

  4. 4

    ActionEvent button - TextField JavaFX

  5. 5

    Java Swing: adding button to TitledBorder

  6. 6

    Java Swing Making On/Off Button

  7. 7

    Java swing bug with custom button

  8. 8

    Java Convert MouseEvent to ActionEvent

  9. 9

    ActionEvent get source of button JavaFX

  10. 10

    ActionEvent from a dynamic SWT button

  11. 11

    how to disable one button but not all in Java Swing?

  12. 12

    Java - is there a way to hide the close button in a swing application?

  13. 13

    Not able to enable a button after it is disabled in java swing

  14. 14

    Programmatically clicking a GUI button in Java Swing

  15. 15

    Java - swing timer in WHILE, with enable / disable button

  16. 16

    Waiting for a button to be pressed in Java Swing before moving on?

  17. 17

    Open a new panel via a button Java Swing

  18. 18

    Java Swing: How JToolbar changes appearance of a button?

  19. 19

    How to update button colors continually [JAVA SWING]

  20. 20

    Can't make button visible in Java Swing

  21. 21

    Java Swing open form on button click

  22. 22

    Java Swing JComboBox/ Button edit parameters

  23. 23

    Calling methods in a Java Fx ActionEvent

  24. 24

    Clicking on one button calls the actionListener of another button in JAVA Swing

  25. 25

    Javafx button sending arguments to ActionEvent function

  26. 26

    JAVA - Can't hide button and text field in Java Swing

  27. 27

    JAVA - Press button when user hits enter java swing

  28. 28

    Java swing - when cancel button clicked don't loop

  29. 29

    How do I use the Button Group Swing control in Java?

HotTag

Archive