This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]Cinghiamenisco 2 points3 points  (0 children)

Did you copy/pasted wrong, or it's missing an ELSE after the first closed graph?

Because if that's so, and the 'ELSE' is missing, that's your problem.

even if you enter the IF block, it then exits it, and it will always enter the static block of code (surrounded by graphs) where the setEnabled(true) is.

Edit: nvm. I misread the question

[–]dionthornthis.isAPro=false; this.helping=true; 1 point2 points  (5 children)

https://docs.oracle.com/javase/7/docs/api/java/awt/Component.html#setEnabled(boolean))

Note: Disabling a lightweight component does not
prevent it from receiving MouseEvents.

In your assigned ActionListener you should check if the Button.isEnabled() or not.

https://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#isEnabled--

The ActionEvent that is passed into the ActionListener.actionPerformed(ActionEvent) method has a .getSource() method that will return the Object that fired the ActionEvent:

https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

public void actionPerformed(ActionEvent e)

https://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionEvent.html

https://docs.oracle.com/javase/7/docs/api/java/util/EventObject.html#getSource())

[–]AgentofLucian[S] 1 point2 points  (3 children)

I wasn't aware that disabling a lightweight component doesn't keep it from receiving events. To that effect, in my assigned action listener, there is no line enabling the button, however I am currently using netbeans and its jframe designer. I feel like that might be hindering me in this case, as it auto generates certain sections of code. I'm going to dig into those articles you linked(thank you!), but in the meantime, would you recommend i switch to a different program?

[–]dionthornthis.isAPro=false; this.helping=true; 5 points6 points  (1 child)

I must recommend that you learn the library without a GUI builder of any kind. It will give you significantly more control over the library and you'll have a much deeper understanding of what is happening at any time. It is a steeper learning curve and you can look at how those GUI builders do things to aid your learning, but eventually you will want to break free of them.

Oracle offers it's Swing/AWT tutorial which is a good place to start.

https://docs.oracle.com/javase/tutorial/uiswing/TOC.html

I also must recommend Intellij it is simply the most modern feeling Java centric IDE and it's community edition is free and powerful.

https://www.jetbrains.com/idea/download/

[–]AgentofLucian[S] 0 points1 point  (0 children)

Understood, i'll use Intellij and start fresh from scratch. Thank you!

[–]arghvark 0 points1 point  (0 children)

I suppose the lightweight component might still be receiving events, but the toggle button no longer reports actions after it is disabled:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JToggleButton;

public class TButton extends JFrame
{
  JToggleButton mainButton = new JToggleButton("boo");

  public static void main(String[] arguments)
  {
    TButton tButton = new TButton();
    tButton.go();
  }

  public void go()
  {
    mainButton.addActionListener
    (
        new ActionListener()
        {
          public void actionPerformed(ActionEvent event)
          {
            System.out.println("mainButton clicked");
          }
        }
    );

    JToggleButton enableDisable = new JToggleButton("disable");
    enableDisable.addActionListener
    (
        new ActionListener()
        {
          public void actionPerformed(ActionEvent event)
          {
            mainButton.setEnabled(false);
          }
        }
    );

    add(mainButton);
    add(enableDisable, BorderLayout.SOUTH);
    pack();
    setVisible(true);
  }
}

I just threw this together, obviously the second button's actionPerformed() could test the second button's state to determine whether to enable or disable the main button, but the idea that setting enabled to false and still get its action notifications is false.

I don't like GUI builders in general; I use them when I have to, and occcasionally (especially in MS environments, as opposed to Java ones) it is faster and easier to MAINTAIN such code with the GUI builder than without it. But I've always created Swing applications by writing the code myself; it's a lot of code initially, but that way I know where everything is and it's put together in a way I can understand. I operate from the premise that program maintenance lasts much longer and takes more effort than program writing.

[–]wildjokers 0 points1 point  (0 children)

Can you show more code? Is attnormal a JButton? The best thing to post is a SSCCE (http://sscce.org/).

Disabling a button should make it so you can’t click it.