How to select a JRadioButton from ButtonGroup? - java

I try to do an image creator program in java (with squares/circles/etc)
I have a few JRadioButtons in a ButtonGroup that symbolizes my program's "mode" (if I draw a circle, something else/if I move the objects).
When I click on different modes, the "mode" changes and I'm able to do what I want.
My problem is when I try to change the mode by double-clicking on an object. I do it in a MouseListener. I'm able to select the object, to change the "mode", but I can't change the selected JRadio Button on my ButtonGroup.
I searched for a while (since the setSelected() is not working). I know that ButtonGroup can have only a button selected at once. How could I deselect the curent one and select the one I need (the first one).
Thank you for any advices.

From the docs:
public void setSelected(boolean b)
Sets the state of the button. Note that this method does not trigger
an actionEvent. Call doClick to perform a programatic action change.
As mentioned here use:
radioBtn.doClick();

I created a small method that allow me to set any radio group button. Very convenient if you don't want to use if for any radio button.
public void setButtonGroup(int rdValue, Enumeration elements ){
while (elements.hasMoreElements()){
AbstractButton button = (AbstractButton)elements.nextElement();
if(Integer.parseInt(button.getActionCommand())==rdValue){
button.setSelected(true);
}
}
}
then
setButtonGroup(yourValue, yourButtonGroup.getElements());

Related

Java disable mouse click over the jList

I made a game "Who Wants To Be A Millionaire" with jList filed. In the jList I listed the prises see this picture below
The game is started with the prise number 1 and increase the number if the answer was OK. If I move the mouse over the prises I can modify the prise position with the mouse click as well. This is what I want to disable. The jList need to have only for show the prises without modify with the mouse click.
I also try to use disable of the jList but than all the colors are changed and I don't find where can I adjust the disabled colors.
What is the best solution for my need ?
The simplest way to achieve that is to add a ListSelectionListener to restore the proper index into your JList.
Take a look at an example:
list.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent e) {
list.setSelectedIndex(myIndex);
}
});
list.setSelectedIndex(0); //0 plays no role, since listener will select myIndex
The selected index will always remain my index, no matter what.
Note: If you want to change the index later, you must change the value of myIndex variable without forgetting firing the selection listener as well. More accurately:
myIndex = 15;
list.setSelectedIndex(0); //0 plays no role, since the selection listener uses myIndex
Another way (more complex in my opinion) is to read and follow Disable JList Cell Selection Property.

Java Swing Buttons

So I have three buttons: add, edit and save. I am using ActionListener and getActionCommand to determine which button was pressed.
If I press add, then save, it has to add a new person in my table. If I press edit, then save, it has to edit that person in the table.
Is there any way to determine which button was pressed before save so that I know which way I go in Save button?
Yes. Store which button was pressed in an instance variable in your class (I don't mean in your listener class).
by using getSource() we can do it
and add some conditions like flag=1 in add button, flag=2 in edit button
if flag=1 then add new record to a table
if flag=2 then edit existing record.
A better way than using ActionListeners directly is to use Actions:
http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html
I always felt like getSource was a big hack, don't know why you would ever need to use it.

Grayed out textfield unless checkbox ticked

How, using JTextField, can I create my program to enable/disable a textfield depending on whether or not a checkbox is ticked?
I have an option which, if checked, needs to take input. If not checked, I'd like the text field to remain grayed out with the user unable to enter text.
mchq08 did not give a complete answer since his code will do nothing if the JCheckBox is unchecked. You don't need the if block as all you'd need is a single line of code in your item listener
checkBox.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent){
// the line below is the line that matters, that enables/disables the text field
textField.setEnabled(itemEvent.getStateChange() == ItemEvent.SELECTED);
}
});
You can do that task with the check box with a simple if/else statement within an event listener.
You would want to put this into an event Listener for the check Box so when the event is fired it will allow it to editable. You could also go a step more and create another if/else if the check box is false and delete the text within the text box and set it to editable.
if(checkBox.isEnabled()){
textBox.setEditable(true);
}
Link CheckBox
Link TextBox

Clearing a group of radio buttons in Java

I have a program which prompts users to select a choice out of four options (from a group of RadioButtons).
Once the user has made a choice, he/she clicks a button and then receives a message. After closing the window, the user will go back to the first window and make a new selection if desired. What I want is for the radiobuttons to be totally clear.
So far I have implemented a method to actually unselect the radiobuttons and works well (clears the values of the variables), what it doesn't do is to remove the black spot from the previously selected radiobutton. In the other hand this same method works fine with unselecting and unchecking checkboxes.
Any tip to fix this little issue?
Here's my code:
public void clean() {
jRadioButton1.setSelected(false);
jRadioButton2.setSelected(false);
jRadioButton3.setSelected(false);
jRadioButton4.setSelected(false);
jCheckBox1.setSelected(false);
jCheckBox2.setSelected(false);
}
make them group of buttons and then buttonGroup1.clearSelection();

Java Swing - make two JLists "siblings" - i.e. only one item in either can be selected

I have a JPanel which contains two JLists - both can have an item in them selected - as expected.
What I'd love to be able to do is have it so that only one item in either to be selected
Can anyone help me with either
a) "marrying" them so this can be the case
b) giving me some tips for the best practice to write listeners which can preside over them both and unselect all the elements of one when the other is selected - I'd rather avoid this if possible as I can see it getting ugly!!
Thanks :)
I think the best solution, also for the user, is putting a radio button next with a category label to each list, so you clearly disable the other each time you select one.
I can imagine the user clicking values on the first list, then clicking on the next one and seeing all the values he clicked are gone, with logical frustration...
Then when you are taking the values from the form, just take the enabled ones
The listener is not that difficult nor ugly to write. I would
make sure the lists only support single selection
add the same selection listener to both lists' selection model
This listener can be implemented as
public void valueChanged(ListSelectionEvent e){
if ( e.isAdjusting()) return;
ListSelectionModel sourceSelectionModel = (ListSelectionModel) e.getSource();
if ( !sourceSelectionModel.isSelectionEmpty() ){
//still need to implement the findOtherSelectionModel method
ListSelectionModel other = findOtherSelectionModel( sourceSelectionModel );
other.clearSelection();
}
}
Note that clearing the selection will trigger the listener again, but due to the isSelectionEmpty check you will not end up with a loop. Another approach would be to disable the listener (e.g. with a boolean flag) right before you call clearSelection on the other list.

Categories