[java] JButton disabling

Started by
6 comments, last by Antheus 18 years ago
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.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
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.
If you just want to present text they why dont you just use a text control or a label?

Dave
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
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?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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.

Thanks, that was helpful.
I have thinking in java and it partly helped.
I also have the standard API documents, so it also helped me a bit.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
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 examplemainFrame.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

This topic is closed to new replies.

Advertisement