9 Replies - 279 Views - Last Post: 31 January 2018 - 04:51 AM Rate Topic: -----

#1 JavaRez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-January 18

Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 02:10 PM

How do you get a toggle button to work with a button. Num1 is the button, Candidate is the toggle. I want the button to run some code when the toggle is set to candidate or number.


@Override
   public void actionPerformed(ActionEvent e) {
          String name = e.getActionCommand();


JToggleButton tglBtn = (JToggleButton)e.getSource();
   if(tglBtn.isSelected()){
      tglBtn.setText("Candidates");        
      
   } else {
      tglBtn.setText("Numbers");
   } 

}

if(name.equals("Num1") && tglBtn.getName().equals("Candidate")) {
//some code here.
{

if(name.equals("Num1") && tglBtn.getName().equals("Numbere")) {
//do some different code here.
{




Is This A Good Question/Topic? 0
  • +

Replies To: Need help with using a JToggleButton with a JButton

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 02:22 PM

The Strings on line 11 and line 20 are different. Should they be the same?
It is better to define the value of the String in one place as a final variable and then use that variable in the code like on lines 11 and 20. That way the compiler will catch spelling errors.
With String literals as used in the code, the compiler won't check that they are the same.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • blow up my boots
  • member icon

Reputation: 6549
  • View blog
  • Posts: 26,553
  • Joined: 12-December 12

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 02:30 PM

With a toggle button I would think you'd only need to check whether it is selected or not, isSelected(), you wouldn't need to take the additional step of checking its text: it only has two states.

[Well, it does have more available states, isArmed, isPressed. Example.]

It also looks to me that you are trying to determine the association of a single event with two controls, the button and toggle button. The event will only happen with one control at a time.
Was This Post Helpful? 0
  • +
  • -

#4 JavaRez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-January 18

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 03:00 PM

View PostNormR, on 30 January 2018 - 02:22 PM, said:

The Strings on line 11 and line 20 are different. Should they be the same?
It is better to define the value of the String in one place as a final variable and then use that variable in the code like on lines 11 and 20. That way the compiler will catch spelling errors.
With String literals as used in the code, the compiler won't check that they are the same.



Yes they should be the sam , that was a typo.
Was This Post Helpful? 0
  • +
  • -

#5 JavaRez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-January 18

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 04:30 PM

I thought this would work but I get this error.
java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JToggleButton


   @Override
   public void actionPerformed(ActionEvent e) {
           String name = e.getActionCommand();
         
   JToggleButton tglBtn = (JToggleButton)e.getSource();
   if(tglBtn.isSelected()){
      
      if(name.equals("Num1")) {
         //do something here
      }

   } else {
     
   } 


Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 04:43 PM

Quote

javax.swing.JButton cannot be cast to javax.swing.JToggleButton

The source returned by getSource is a JButton not a JToggleButton.
The cast on line 5 is wrong. Change it to cast to a JButton.

Make sure the action listeners are coded for the object they are added to.
Have separate listeners for each object type so the cast is correct.
Was This Post Helpful? 0
  • +
  • -

#7 JavaRez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-January 18

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 05:55 PM

I have no idea how to do this, please give me an idea.
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 06:14 PM

Quote

how to do this

Please explain what the "this" is you want to do.

Are you asking about how to create unique listeners for each component?
One way is to use anonymous class:
   yourComponent.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          //  code here

      }
   });

This post has been edited by NormR: 30 January 2018 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#9 JavaRez   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 26-January 18

Re: Need help with using a JToggleButton with a JButton

Posted 30 January 2018 - 07:07 PM

View PostJavaRez, on 30 January 2018 - 04:30 PM, said:

I thought this would work but I get this error.
java.lang.ClassCastException: javax.swing.JButton cannot be cast to javax.swing.JToggleButton


   @Override
   public void actionPerformed(ActionEvent e) {
           String name = e.getActionCommand();
         
   JToggleButton tglBtn = (JToggleButton)e.getSource();
   if(tglBtn.isSelected()){
      
      if(name.equals("Num1")) {
         //do something here
      }

   } else {
     
   } 



JToggleButton tglButton = new JToggleButton("Numbers");

I got rid of line five and changed this tglBtn.isSelected() to this tglButton.isSelected().
Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 693
  • View blog
  • Posts: 5,303
  • Joined: 25-December 13

Re: Need help with using a JToggleButton with a JButton

Posted 31 January 2018 - 04:51 AM

Does the program work now?
If not, please explain.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1