Results 1 to 4 of 4
  1. #1
    shern91 is offline Member
    Join Date
    May 2015
    Posts
    1
    Rep Power
    0

    Default Know JButton is clicked in another class

    I have two classes one is called main.java another is called Gui.java

    in gui.java, will create a JFrame and some component.here is some code in main.java.

    Java Code:
     public static void main(String[] args){
    while(true)//will always loop once user close the window
    
    Gui gui = new Gui();
    gui.setVisible();
    
    if()//at here need to determine button in Gui.java is press or not to process the user input
    {
    String fromUser = gui.Jusertxt.getText();
    } 
    
    }

    what is the code need to use to know Button in Gui.java is clicked or not? i try use method to return true value, but in not working. Thank in advance

  2. #2
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: Know JButton is clicked in another class

    looking at your little snippet I have the feeling that there are some fundamental issues that have to be straightened out before hand. If you are just interested to "make a button work" I would read the chapter about events and event handlers.

  3. #3
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: Know JButton is clicked in another class

    I'm not exactly sure what you mean, because the sentences are hard to understand

    but going off the question in the title, if you want to know if a button is pressed or not why not just make a boolean variable that is initialized 'False' and will be set to 'True' when the button is pressed.
    then you can just check whether that variable is true or false from the other class.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Know JButton is clicked in another class

    The OP is probably asking how to run code when clicking the button. As Benji says: using event handlers, more specifically an ActionListener.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Call action of JButton from another class
    By geckolux in forum AWT / Swing
    Replies: 5
    Last Post: 08-04-2014, 06:17 PM
  2. Replies: 2
    Last Post: 07-16-2012, 04:08 AM
  3. One JBUTTON DISPLAY DIFFERENT TEXTS WHEN CLICKED.
    By Nakinsige in forum New To Java
    Replies: 9
    Last Post: 01-08-2012, 05:36 PM
  4. Replies: 4
    Last Post: 02-16-2009, 03:20 PM
  5. Help, events in Jbutton class
    By Heather in forum Java Applets
    Replies: 2
    Last Post: 06-30-2007, 04:32 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •