Sign in to follow this  
The C modest god

[java] JButton disabling

Recommended Posts

I have a JButton in my program, but I want to use it only to present text and that the user wont be able to click it. What I have now is that when the user press the button it changes it colors for a second, I want to disable this. How would I do that? Thanks in advance.

Share this post


Link to post
Share on other sites
Hi,

Your description isn't very clear, but if I understand what you mean it's quite easy, really.


JButton button = new JButton("foo");
button.setEnabled(false);






Regards,

GCS584

[EDIT:] Otherwise, you can just not handle the event.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave
If you just want to present text they why dont you just use a text control or a label?

Dave


I concur

Share this post


Link to post
Share on other sites
Ok, I tried to use JLabel instead.
However, I have two problems.
First, I want that the text will have a frame around it like the JButton has.
Second, its a minor problem, when I do SetHorizentalAlignment, I can only use the number 0 and not the constant CENTER.
If I use CENTER I get the following error:

lbl.setHorizontalAlignment (CENTER);

"UserLoginView.java": cannot find symbol; symbol : variable CENTER, location: class view.UserLoginView at line 47, column 41

I included javax.swing.swingConstants in my file.
So why do I get this error?

Another question, why my program doesnt exit?
I run the program, when the exit button is pressed I simply do window.show(false)
In the window process the application dissappeares, but in the JBuilder it shows that it is still running.
How do I terminate the program?

Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
Ok, I tried to use JLabel instead.
However, I have two problems.
First, I want that the text will have a frame around it like the JButton has.


JLabel lbl = new JLabel("foo");
lbl.setBorder(BorderFactory.createLoweredBevelBorder());


Quote:

Second, its a minor problem, when I do SetHorizentalAlignment, I can only use the number 0 and not the constant CENTER.
If I use CENTER I get the following error:

lbl.setHorizontalAlignment (CENTER);

"UserLoginView.java": cannot find symbol; symbol : variable CENTER, location: class view.UserLoginView at line 47, column 41

I included javax.swing.swingConstants in my file.
So why do I get this error?

Refer to it as SwingConstants.CENTER. Including a class only lets you refer to it w/o fully qualifying it. It doesn't import the sybols within that class into the current namespace.
Quote:

Another question, why my program doesnt exit?
I run the program, when the exit button is pressed I simply do window.show(false)
In the window process the application dissappeares, but in the JBuilder it shows that it is still running.
How do I terminate the program?

Hard to answer w/o seeing the code, but normally you exit by calling System.exit().

Sounds to me like you need to work through a basic Java tutorial --either the one at sun.com or "Thinking in Java" are good choices, Both are freely available on the web.

Secondly, you need to keep the javadoc for the standard library open in your browser and *use* it. Relying upon auto-complete in JBuilder to educate you as to the contents of the library will leave you mired in ignorance. It's like trying to view a huge mural through a peephole.

I hope this was helpful.

Share this post


Link to post
Share on other sites
Quote:
Original post by The C modest god
Another question, why my program doesnt exit?
I run the program, when the exit button is pressed I simply do window.show(false)
In the window process the application dissappeares, but in the JBuilder it shows that it is still running.
How do I terminate the program?


The term "application" doesn't exist in java, which leads to certain issues with shutting down applications. The common way is, to listen on the main window, and when that one gets closed, you terminate the VM yourself. This may be a bit unusual, but due to design of AWT (even if using swing), is the only way.


public class ApplicationExitListener extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}

// put this as your window listener on your main window, for example
mainFrame.addWindowListener(new ApplicationExitListener());
// You might be able to use NetBeans IDE to attach the handler for this





Alternatively, you could just use:

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



Note, that this will kill the JVM itself, so if you have anything you want to take care off before closing the app, do it before calling System.exit

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this