Default selected JButton - java

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.

Related

Update JFrame window

I'm doing a simple java application that essentially shows a certain amount of letters (ABCDE etc) from an array, each one displayer in a portion of a grid. There are two buttons, one that will shift the letters to the left (so that one shift will become BCDEA and the right shift will go EABCD).
I've got the shifting and everything else working, as I've tested using a System output. But how do I get the window to refresh and show me the updated JLabels? They stay the same (ABCDE) after I shift them.
I've tried revalidate() and repaint() both inside the buttons' ActionListeners and on the shift method that they call, but nothing happens. Any tips on this?
I've tried revalidate() and repaint()
You only use those methods when you create a new component and add the component to a visible GUI. So it sounds like you are trying to remove/add the labels in the new order you want the labels to be displayed.
Maybe an easier approach is to leave the label in the same order but just change the text on each label. Then all you need to do is
label.setText();
and the label will repaint itself automatically without you invoking revalidate() and repaint().

JTextField change value automatically while button pressed

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.

Trouble with Java GUI

I am creating a GUI for my project. I am completely stuck at one point. I was able to create a GUI which will make some simple queries like the release number, command name, and few other boolean questions.
Now I want the user to press CONTINUE button to move to the next page of the GUI form. I have created the continue button, but now I need to register an event to it. This is where I am stuck. I have no idea what event can I register to it which will move the GUI to next page. (By moving to the next page I mean, the different GUI pages we see when we are installing a software for our computer.)
For instance, if I am installing iTunes, I'd first select the radio button for "I accept the terms and conditions" and then I'd press the CONTINUE or the NEXT button to move ahead. If I need to come back, I'd press the BACK button.
One logical answer would be to create another GUI form and then link it to the one I created first.
EDIT:: this is the first time I am working in Java, so I might have ignored some obvious facts.
Look into using a CardLayout, adding several JPanels to the CardLayout-using container, and then to swap to the next view (the next JPanel), call the layout's next(...) method in the JButton's ActionListener. You could also randomly access components held by the CardLayout using its show(...) method.
To learn more about this including sample code, please have a look at the CardLayout Tutorial, and the API.
Well, register an ActionListener or an Action with the button. To do that wizard style, have a look at the CardLayout layout manager to switch cards or use a tabbed pane, hide the tabs and switch them inside the action or action listener.
if i understood, i think you should use the CardLayout
If you are using Swing, you can using several instances of JPanel over a one instance of JFrame.
You can have something like that structure:
JFrame
+------JPanel:root
|
+---JPanel:current // This panel change by other instance
|
+---JPanel:controlPanel // This panel contains you button
In you button need add a ActionListener with the method addActionListener
The panel control may need changes your elements, may the text of button or remove the listener and change by other.
I hope that can help

Rectangular Java Swing Radio buttons?

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

How do I Click a JButton without the user Clicking it?

I have a JButton and when a player clicks it it tell my Action Listener that a button is clicked. What i want to know is that is there a command or something that acts as if a player clicked the button.
Like Tic Tac Toe, i have it so 2 players can play against each other, but i want to add the option for a computer player vs a human player. But since the computer cant actually click the button, i am lost.
Edit:
would it be as easy as gridButton2.click() (Name of button).click();
Pretty much. All you need to do is use the doClick() function. See the API for more information.
for the tic-tac-toe thing, you don't need the computer to click a button. you just have to wait until the human makes a move, then have the computer choose its move and execute the code that happens if the button gets clicked.
Just call directly the actionPerformed() method from the ActionListenerimplementing class. You can do it the following way:
actionPerformed(new ActionEvent(gridButton2, ActionEvent.ACTION_FIRST, "youractioncommand"));

Categories

Resources