Java provides a simple method to disable its buttons with button.setEnabled(false);
However doing so will grey out the entire button thus affecting the visibility of the text and images on the disabled button.
Question: Are there any available methods/ways in Java to allow buttons to be disabled yet does not grey out?
Certainly, I am not expecting manual tweaking on the ActionListener of the buttons to achieve this.
Companies spend millions of dollars to develop a UI can is common and can be used by all users.
How is the user suppose to know that the button is disabled if there is no visual indication?
I am not expecting manual tweaking on the ActionListener of the buttons to achieve this.
Why? What is wrong with this approach? It is better than trying to create a custom LAF for all platforms your code might run on.
Anyway (rant finished) you could use a custom ButtonModel:
button.setModel( new DefaultButtonModel()
{
#Override
public boolean isArmed()
{
return false;
}
#Override
public boolean isPressed()
{
return false;
}
});
Should work for all LAF's and the button won't be painted as a pressed (which I suppose is better than just removing the ActionListener).
This is a little bit of work, and may not be the best coding style or solution, but it might satisfy your requirements. I'm going to assume you have an icon for when the button is enabled and another for when it is not.
Basically, create and manage your own, virtual "disabled" state for the button (but don't touch the built-in enabled or disabledIcon methods).
In the code that manages the enabling/disabling, when you disable "virtually", set the regular icon ( setIcon() ) to be your disabled Icon graphic, and set a boolean to reflect the virtual "disabled" state of the button.
When you "enable", use setIcon() to return the default Icon, and set the boolean to reflect "enabled".
Then, when the button is clicked, in the ActionListener, you can inspect the boolean and do nothing if the boolean says the virtual state is "disabled".
Related
I'm doing an app with lots of buttons and menus and I want enable and disable the buttons and menu items when the actions attached to them can or can not be performed. ie the save button and the save menu item only will be active when there are unsaved changes.
Question: How can I do this efficiently/Which is the correct way to do this?
One solution can be have a private variable for each button and menu entry and enable/disable it as needed.
Other solution can be get all the components of the JToolBar and the JMenu in an array and iterate over all them and enable/disable as needed.
But I think there are better solutions. Any help or guideline will be apreciated.
edit: The question is not how to enable/disable a single button or menu item, I will know how can I manage the state of all the buttons and menu items of the app. Which is the best way to achive this?. I have explained some solutions in which I have been thinkin, but none of them convince me.
The Action class already supports this. See the Action#isEnabled and Action#setEnabled methods. Calling the setter will fire an event, on which the UI on which this Action is set will enable/disable itself.
The Action can be seen as the model for your UI buttons/menu items/... . All state is stored and updated in the model, and the view needs to reflect this (MVC pattern).
1) to disable from clicking use:
JButton#setEnabled(boolean)
and
JMenuItem#setEnabled(boolean)
2) to disable ActionListener for some time use a private boolean variable inside
public void actionPerformed (ActionEvent ae) {
if (!enabled) return;
// rest of code
}
I had just a few class of java on college, but I want to do a thing that I don't know how.
Is basically a window with a SplitPane, on Left side I have a menu made with toggle buttons, and on the Right side I need to change the content based on each button.
Theres any way to design the ViewA and ViewB on separated JFrame Form and load then inside my Right Side when I click on menu items?
Another idea is, put the ViewA and ViewB put a JTabbedPane on the Right Side, and hide the Tabs. So there's any way to hide the tabs?
I have none experience developing in java, any problem about this concept (difficult, loading time, memory, maintenance), If you guy know a better way to to this, I just don't want a lot of windows popping up.
A really simply way would be to simply have a set of jPanels in the right side, with only one ever set to Visible.
Basically, for each toggle on the left side, you will have an Event Listener that does this:
private void toggle1ActionPerformed(java.awt.event.ActionEvent evt) {
jPanel1.setVisible(false);
jPanel2.setVisible(false);
jPanel3.setVisible(true);
}
Simply changing the true value depending on the individual toggle.
In Netbeans, if using the GUI editor, you can simply double click the toggle button to generate the listener and appropriate method, then fill in the code for it.
I'd like to create a set of buttons in a Java Swing application like you get in a typical tool palette in a paint program. That is, a set of small square buttons, each containing an icon, only one of which is pressed down, and when you press another button, the first is deselected. I've thought of a number of solutions and none of them seem very easy/elegant.
This sounds like a job for JRadioButton, but if you add an Icon to that, you still get the small circle, which is fairly space inefficient. I guess an option would be finding an alternative Look and Feel or painting code for JRadioButton.
Another alternative might be to add JButton to a ButtonGroup, maybe setting a JToggleButton.ToggleButtonModel as the model, but that doesn't have the desired effect, as the painting code for a standard JButton does not keep it depressed when selected. Possibly the JButton code could be modified to do this. Like making it painting "selected" the same way as "pressed".
A third alternative would be to use normal JButton's, and add a common mouse listener that keeps them pressed or not, and communicates the changes between the buttons.
Can anyone advise on the best way to achieve the aim please? A simple method I've missed would be best, but advice on which of these three alternatives would be best and pointers on how to get started would be useful too.
What about a plain JToggleButton in a ButtonGroup? It is not abstract, you can instantiate one with an Icon, and it stays depressed while selected.
See the SwingSet2 demo:
http://java.sun.com/products/plugin/1.4/demos/jfc/SwingSet2/SwingSet2.html
Click the second icon on the toolbar (the one twith the check box and radio button) then tab "Radio buttons". Then click on "Paint Border" on the right panel, under "Display Options".
Source code of the demo is under your JDK install dir, so for example on my PC it's under \jdk1.6.0_01\demo\jfc\SwingSet2\src
Sorry the title is fuzzy, but I really coudln't come up with a fitting title.
I'm developing my first application with Swing, and I'm having a hard time figuring out how to keep track of the current view of the application. With I mean with current view is for example if a button has already been pushed. For example, you shouldn't be able to press "Execute" before a file has even been loaded. I've come up with an architechtural solution to this that is really crappy, and I'd like tips on how to improve it.
I have a label called infoText, and it's updated pretty much every time I press a button. Through this, I'm keeping track of the applications state in this fugly way:
if (infoText == LOADING_NARROW){
printSelected(narrow_list);
}else{
printSelected(list);
}
Rather than keeping track of your state with GUI components, use normal Java objects and variables.
Just keep a boolean loadingNarrow in this case that you reference and update when needed.
Also if you are running a large load as the result of a button press and don't want the user to press it again you can disable the button once the load starts and re-enable it later. (Note I am assuming you are running the load on a separate thread so the GUI does not freeze).
Swing Components keep track of their own states.
My advice:
Initiate the application to a default state.
Adjust the settings in an event driven manner. For instance when JButton A is clicked, enable JButtons B and C and set a JTextField.
Check the states of objects with their builtin methods. Example
if((jButtonA.isEnabled() && jTextField.getText().equals("foobar"))
You can also use the mediator pattern to group related components and their actions.
First: Are they different methods, or a copy-paste-error?
printSelecteds (narrow_list);
printSelected (list);
Second: To disable a button you usually use:
ok.setEnabled (false);
If the file is loaded, you call
ok.setEnabled (true);
to enable the JButton "ok".
I don't see how that is related to your info-text, and to your printSelected(s) method. If you pass the state via the GUI, you might loose the one or the other due to race conditions. Changing a label could be the sink of an state change.
You could have mutual exclusive bit patterns to allow interference:
FILE_OPEN = 1;
SEARCHED = 2;
FRIDAY = 4;
to add them bitwise:
state |= FRIDAY
to ask them up in a binary pattern:
if (state | FILE_OPEN) ....
It doesn't look very elegant to me. I guess I'm not sure what your problem is. :)
To fire an action if some button is pressed, you have to implement an actionListener, which could modify your label as well. But the swing eventloop will already check the state of your components. You seem to duplicate the work partly.
I want to create button with custom look and feel. I have got different images to be set as background of button for normal, mouse over, mouse click, button disabled etc. I have created my own class, extending javax.swing.JButton and overrides paintComponent method. How can i change my button background for all the above given states.
In addition to Steve De Caux's answer, you can:
Add a MouseListener which changes an enum variable, let's call it state on your extended JButton
In your overridden paintComponent take into consideration the current state and paint different backgrounds. Like
if (!getModel().isEnabled()) {
} else if (state == ButtonState.MOUSE_OVER) {
} else if (state == ButtonState.MOUSE_CLICKED) {
}
JButton has a series of simple set methods for rollover, pressed, selected, disabled and disabled selected states - for example
button.setPressedIcon(new ImageIcon("images/button-down.png")
the other methods are :
button.setRolloverIcon()
button.setSelectedIcon()
button.setRolloverSelectedIcon()
button.setDisabledIcon()
button.setDisabledSelectedIcon()
...have fun !
By the way, O'Reilly has a fun book called Swing Hacks with lots of little goodies for playing with swing : Swing Hacks
You could create a custom button UI delegate. This blog entry: http://blog.elevenworks.com/?p=4 has an example for a custom tabbed pane, but the principle is the same. Extend BasicButtonUI, implement the custom rendering your want for the button, and then call setUI() on the button.
This will probably take longer to implement than using the existing button API methods to change the appearance, but it gives you a lot more control.
ImageIcon icon = new ImageIcon("images/icon.gif");
JButton button = new JButton(icon);