I have one button which set time on Timer. So now I push. Button change time on Timer and this is still selected. In this frame I have keyListener so when I push space Timer with start but now when I push space it push again this button because it is still selected. How I can improve this button when I push it, it will not be selected ?
I'm not sure of having fully understood your question.
If you want to disable click on a JButton:
JButton b = new JButton();
b.setEnabled(false);
If you want to unselect it:
b.setSelected(false);
You could also find useful prevent a button to gain focus:
b.setFocusable(false);
You can change the focus by calling the correct method in KeyboardFocusManager
KeyboardFocusManager kfm = KeyboardFocusManger.getCurrentKeyboardFocusManager();
kfm.focusNextComponent();
This causes focus to move to the next component, whatever it is. This has the advantage of being independent of what the component is, so that if the UI changes, this still moves to the "next" component rather than a specific component you specify to receive focus.
If your problem is that you do not want the user to press the button while other things are happening, you should consider disabling the button (as explained in a previous answer) so that it cannot be activated in any way. You need to enable it again, of course, as soon as it is legal to use it again.
rc
Related
I'm getting some trouble with a toolbar I made. In this I have 4 JButtons and below a JTable, which is set on focus when my windows shows in order to see the JButtons unfilled.
This is how I set each JButton:
btn = new JButton("New");
btn.setFocusPainted(false);
btn.setBackground(SystemColor.window);
btn.setVerticalTextPosition(SwingConstants.BOTTOM);
btn.setHorizontalTextPosition(SwingConstants.CENTER);
btn.addActionListener(new ActionListener() {
...
});
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
toolBar_General.add(btn);
btn.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/file11.png")));
Example of my toolbar at start: http://www.tiikoni.com/tis/view/?id=0ef950b
But after clicking on a JButton, it remains filled after itsactionListener does his work.
Example of my toolbar after clicking a JButton: http://www.tiikoni.com/tis/view/?id=9e88ea4
My question is: how can I mantain my buttons contentAreaFilled property true (so the user can tab between components in my window and see which control he has selected) and unfocus my button after the user click?
The result I would like to have is that of the first photo.
I think the call you are looking for is:
btn.setRequestFocusEnabled(false);
this will stop your buttons from taking over the focus when the mouse clicks them, achieving the goal of not having the keyboard focus painted for mouse users. However it still lets keyboard-centric users "tab over" to them.
If one of your buttons gets the initial focus, however, then the keyboard focus will still render there. In that case, you probably want to select some other sensible component to place the initial keyboard focus onto by calling:
otherComponent.requestFocusInWindow();
Are you talking about the input Focus? (the dotted rectangle a component shows when it has the focus)
You can explicitly disable a component getting the focus by calling setFocusable(false) on the component (e.g. every button). It does not prevent you from clicking on the buttons, but it does prevent you from cycling the focus with the tab-key to it.
I have been searching on the web for this but have not found a solution.
I have a 2D array of JButtons. When the user clicks the button, my application currently adds an ImageIcon to that button and does setEnabled(false) to the button.
The problem with this is that it greys out the image. I want the image to stay in full colour but I want to make the JButton not clickable. It would seem that setEnabled() is not the method that I want.
I have considered removing the ActionListener from it yet this would still allow the action of a click (even though nothing would happen).
How do I make a JButton unclickable without disabling it or removing the ActionListener?
JButton derives disabled icon by graying out original icon. if you want the button to be disabled but still the icon in full color, set its disabled icon explictly to full color image using following method
button.setDisabledIcon(button.getIcon());
checkout the javadoc for AbstractButton#setDisabledIcon(Icon)
I have a jTextField with two JButtons (Up arrow and Down arrow buttons). Clicking the Up arrow button the numeric value in the textfield increases by 1 (++), and clicking the Down arrow button the numeric value in the textfield decreases by 1 (--).
What I want know is how to automatically "scroll/change" the value while the button is pressed?
Thank you
What you probably want is a JSpinner. More specifically a SpinnerNumberModel.
Here is a link to a demo
http://docs.oracle.com/javase/tutorial/uiswing/components/spinner.html
The JSpinner is the best way to do this.
But if you want a different implementation, I would suggest using a MouseListener attached to the JButtons. When one of the button is pressed (the mousePressed event), a javax.swing.Timer is started. Every x milliseconds (depending on how fast you want the number increased/decreased) a check is made to see if the JButton is still pressed and if the mouse is still over the JButton. If it is, the number is increased/decreased. When the user releases the mouse (the mouseReleased event), the Timer is stopped/cancelled.
I never did this, so I don't know for sure that it works. But this is the way I would try it.
I have a JFrame which contains most everything in the application. It has a KeyListener attached, and it also has several buttons and textfield on it. The problem it, when a button is clicked or a textfield is selected, it gets focus and shortcuts don't work. Of course, one can tab out of them, but to do this you must tab through EVERYTHING (each button, each textfield) before giving the window focus again.
Is there a sensible way to only require one tab to return focus to the frame from the textfield, and no tabs to return focus to the frame from a button click?
A WindowListener doesn't seem like the best way to do this, but if it's the only way I suppose I can forge forward there.
Thanks in advance!
It has a KeyboardListener attached
I have never heard of the KeyboardListener class so I can only guess what you are trying to do.
My guess is that you should NOT be using a listener of any kind.
Instead you should be using Key Bindings.
If you only need this for the textfield, you can add a keyListener to the textfield and when the user presses tab use yourJFrame.requestFocus(). Otherwise refer you may want to use a window manager or a key map.
Under certain circumstances, I need a JTabbedPane to remain on one pane until the user supplies certain information. Essentially, when this circumstance occurs, I need the current pane to become modal.
How can I implement this? I was thinking I could catch whatever event is triggered when the pane changes, and reset back to the pane I want to stick on. But I'm worried that this won't be quite right, that depending on when the event actually fires the transition to the new pane will happen after I call the method to set the pane to the pane I want, or some other similar race condition. Is there a better way? Is there a way I can make this approach work?
I would suggest setting the other tabs to disabled. This has a positive effect of providing the user feedback that they cannot click out of the tab. Otherwise they may be madly clicking and wondering why it will not let them leave the tab.
Simply set them enabled again after the required fields are completed.
just disable the JTabbedPane:
pane.setEnabled(false);
and enable it if all fields are correctly set (or whatever condition)
You could use a CardLayout along with JPanels to do what you want and not use JTabbedPanes. Since you need to use the tabbed panes, I would suggest that once the condition has been reached that you want to force the user to stay on that tab set that tab to be the selected one by using.
setTabComponentAt(int index, Component component)
or
setSelectedIndex(int index)
Set a flag indicating that the user should not be able to proceed until completing whatever it is you want them to do and have all the other tabs be disabled using setEnabledAt(int index, boolean enabled)
.
Once the user has completed what they needed in order to continue set the flag accordingly and reenable the other tabs.
I haven't the time to try that solution out but I think it should work.