Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can execute some operations only if a specific JButton is clicked? Some doubts trying to use getSource()
    primarykey
    data
    text
    <p>I am pretty new in Java <strong>Swing</strong> development and I have some doubts about how handle the following situation.</p> <p>I have a custo <strong>LoginFrame</strong> class that extends a classic Swing <strong>JFrame</strong> class.</p> <p>In my class I have a <strong>JButton</strong> and a method that will be perform when my button is clicked, something like this:</p> <pre><code>JButton loginButton = new JButton("Login"); @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.out.println("Button LogIn cliccked"); System.out.println("SOURCE: " + e.getSource()); if(e.getSource() == "loginButton"){ System.out.println("BLABLABLA"); String name = userNameTextField.getText(); char[] pass = pswdTextField.getPassword(); // your remaining operation... } </code></pre> <p>The method work and, in the Eclipse console, print the message <strong>"Button LogIn cliccked"</strong> but I think that if I had more JButton objects this message will always print...so I am trying to use the <strong>getSource</strong> method, but in the used way seems not work beause can't recognize "loginButton" ass the source of the clicked button.</p> <p>I have also try to print my souce by this line:</p> <pre><code>System.out.println("SOURCE: " + e.getSource()); </code></pre> <p>and the output is:</p> <pre><code>SOURCE: javax.swing.JButton[,6,152,95x30,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder@1167e3a5,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Login,defaultCapable=true] </code></pre> <p>What can I do to execute something only if the specific <strong>loginButton</strong> is clicked?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload