How to make Java Swing Radio Buttons unselectable but not greyed out? - java

I am developing a Java Swing App, and I want to use JRadioButton objects to show state. I don't want the user to have the ability to select them. If I use the button's .setEnabled(false) method, the radio button is greyed out.
I don't want the Radio Button to grey out. Is there a way to override this?

Well You Could Do Something Like:
boolean rButtonEnabled = false;
JRadioButton rButton = new JRadioButton();
rButton.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e) {
rdbtnNewRadioButton.setSelected(rButtonEnabled);
}
});
It's not the prettyist solution, but I hope it helps.
To store the new value perhaps you can create a new class extending JRadioButton.
While this does not use the .setEnabled(), it is effectively the same

Related

Setting Actor To Listen For Button Click In LibGDX

Background Information: I am currently working in a Dialog class I have extended for my game. Inside of this dialog's content table I have both an Image and a Table (lets call it ioTable). Inside of ioTable I have a combination of both Labels and TextFields. The idea is that the dialog becomes a sort of form for the use to fill out.
Next, inside of the Dialog's button table, I want to include a "Clear" TextButton (clearButton). The idea that clearButton will clear any values written to the TextFields of ioTable.
My Question: Is is possible to add a listener to each of the TextFields of ioTable that will trigger when clearButton is pressed. As always, any other creative solution is more than welcome.
You could just give the EventListener a reference to the table you want to clear:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
#Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
If you see yourself creating multiple clearButtons, you could easily wrap this in a helper method or extend TextButton.

Fast double key pressed on Jbutton leads results in same operation twice

1> I have a JButton in Jframe.
2> The click of JButton opens new instance of another JFrame.
The problem is when a Key is pressed very fast on the above Jbutton .Two instances of the same JFrame opens up.
I have to open these frames. I knows there are other options also not using the Jframes as I read.
I managed to solve this problem for Doulbl click of Mouce by setMultiClickThreshHold('time in miliseconds'). But it worked only for mouse.
Tried some other stuffs which I got in google, But none worked.
Is there any other way to solve this issue?
For full control of how often/quickly again an Action is triggered, implement it to disable itself in its actionPerformed. Something like:
singlePerform = new AbstractAction("DoSomthing") {
#Override
public void actionPerformed(ActionEvent e) {
setEnabled(false);
doSomething();
}
};
JButton button = new JButton(singlePerform);
When it's safe for doSomething to be triggered again, simply re-enable the Action:
singlePerform.setEnabled(true);

How to enable a disabled JRadioButton

I have two JRadioButton with ImageIcon for each one. Because of the ImageIcons I'm using, I need to give the appearance that one button is selected and the other one is not selected. To do this I'm trying to disable the other button, which automatically changes the ImageIcon to disabled appearance.
Problem is that when I click on the disabled JRadioButton, nothing happens, not even the ActionListener on the JRadioButton is getting called.
Is there a way to enable a disabled JRadioButton by clicking directly on it? Once it's disabled, it's ActionListener no longer gets called, so I can't enable it by clicking on it.
Basically I'm trying to give the appearance that when one is selected, the other is not selected, using ImageIcons.
//Below part of my code how I initialize the buttons
ButtonGroup codeSearchGroup = new ButtonGroup();
searchAllDocs = new JRadioButton(new ImageIcon(img1));
searchCurrDoc = new JRadioButton(new ImageIcon(img2));
RadioListener myListener = new RadioListener();
searchAllDocs.addActionListener(myListener);
searchCurrDoc.addActionListener(myListener);
codeSearchGroup.add(searchAllDocs);
codeSearchGroup.add(searchCurrDoc);
//Below listener class for buttons
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == searchAllDocs){
searchAllDocs.setEnabled(true);
System.out.println("Search All documents pressed. Disabling current button...");
searchCurrDoc.setEnabled(false);
}
else{
searchCurrDoc.setEnabled(true);
System.out.println("Search Current document pressed. Disabling all button...");
searchAllDocs.setEnabled(false);
}
}
}
Thanks in advance.
The ActionListener wont fire in disabled mode, but mouse events will.
Thus simply add a MouseAdapter to JRadioButton and override mouseClicked(..) and call setEnable(true) within overridden method like so:
JRadioButton jrb = new JRadioButton("hello");
jrb.setEnabled(false);
jrb.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent me) {
super.mouseClicked(me);
JRadioButton jrb = (JRadioButton) me.getSource();
if (!jrb.isEnabled()) {//the JRadioButton is disabled so we should enable it
//System.out.println("here");
jrb.setEnabled(true);
}
}
});
though I must say there is a bit of skewed logic at play. If something is disabled its done so for a reason, thus we shouldnt allow users to be able to enable. and if we do there should be a control system where we can choose to have the button enabled/disable it wouldnt become the control system itself.

Multiple JComboBox Event Handling in Same Class

I have two JComboBox and one button. I am trying to do that if I select an item from the two combo box individually and press the button called search. Then the two selected items from the two combo box will save in a new two separate string.
Please anyone help me to solve the problem.
Here is the code snippet
//here is the strings that in the combo box
String lc[] = {"Kolabagan-Dhaka", "Gabtoli-Dhaka", "Fakirapul-Dhaka", "Shaymoli-Dhaka"};
String rc[] = {"Banani-Bogra", "Rangpur","Shatrasta-Bogra"};
//here is my two jcombo box
JComboBox lcCombo = new JComboBox(lc);
JComboBox rcCombo = new JComboBox(rc);
// here is my search button
JButton searchButton = new JButton("Search");
There are two ways to go about this. The first is to have one class that implements ActionListener and in the implementation, check the source (ActionEvent.getSource()). Based on which component sourced the event, you take the appropriate action.
The other option (and my preference) is to create an ActionListener for each component that requires one. You can use anonymous classes if you don't want to explicitly define one for each case. This way each listener knows exactly what component caused the event and what action to take.
Example:
JComboBox lcCombo = new JComboBox(lc);
lcCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do left stuff
}
});
JComboBox rcCombo = new JComboBox(rc);
rcCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
//do right stuff
}
});
To expand on unholysampler, once you have the ActionListener working you can use lcCombo.getSelectedIndex() to check which item has been selected.

How to change the button backgrounds inside JOptionPane

I was wondering if anybody knew if it was possible to change the background color on the buttons inside a JOptionPane. I know how to change the entire JOptionPane background using a UIManager, but know what I want is to set the individual okButton, cancelButton, and so on within the JOptionPane to separate individual colors. If I can do this, how would I do this?
Thanks for the help.
There is no direct way to do this.
But if you really want to give it a try, then you will need to read the JOptionPane API which gives code that shows you how to manually create and display a JOptionPane without using the showXXX methods.
Using this approach you now have access to the actuall JDialog. Then you can use Darryl's SwingUtils to access the individual buttons and then set the background.
The code would be something like:
JButton ok = SwingUtils.getDescendantOfType(JButton.class, dialog, "Text", "Ok");
ok.setBackground(...);
Simplest would be to just create your own JDialog and set the button characteristics to your heart's content.
You can use your own buttons with yours characteristics in showOptionDialog. I guess it is not the best solution, but it simply works.
JButton button = new JButton("OK");
button.setBackground(Color.BLACK);
button.setForeground(Color.WHITE);
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent actionEvent) {
JOptionPane.getRootFrame().dispose();
}
});
JButton[] buttons = { button };
OptionPane.showOptionDialog(null, "Test Message", "Dialog", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, new ImageIcon(), buttons, buttons[0]);
Add the the line of code below before your JOptionPane
UIManager.put("Button.background", Color.white);
JOptionPane.showMessageDialog(null, "Project, Please");

Categories