JTextField change value automatically while button pressed - java

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.

Related

Change button color after updating text field

There are 20 JTextField and one calculate button in panel.
after entering data to text field and I click button, program calculate as well.
What I want to do, after first click for button, is there any updates in any text fields, button colors should be change as green (it means you have to know something already change and you have to calculate again).
How can I proceed with that, should I put action listener for each text field or any other short code exist?
Your question is not clear. As far my concern to yr question you can track button click event and put code there for check jtextfield value and on the bases of value you can take decision to change the color of the button

Button remains focused after clicking

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.

Default selected JButton

So, I was just wondering. Say I had a simple game of pong consisting of two JButtons as pongs that move with key input, a smaller JButton for the ball moving in a timer and a text box for a score. When I made this, I had to click on the button before I could move it. Is there a way to make it so that the button is selected by default when the program runs so that I can just press keys to move it straight away without clicking it first? Thanks.
Use the setDefaultButton method of the JFrame's root pane:
myFrame.getRootPane().setDefaultButton(button);
In frames constructor use below code after making button:
this.getRootPane().setDefaultButton(button);
I think you can select one by default with requestFocus().
Something like:
defaultJButton.requestFocus();
You should do that at the initialization or every time you want to restart, reset the state or similar.

Retaining Focus for KeyboardListener

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.

How to unselect JButton

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

Categories

Resources