How can I get an Object's name in java - java

I have a group of radio buttons that doesn't includes any text on frame 1.
Every button has a j label l, which means that when I choose one radio button, I want to change the icon of a specific j label.
I want to know which button is selected to send it to frame 2 by a constructor.
So I want send the variable of the current radio button that's clicked.
How can i do that?
public String getSelectedButtonText(ButtonGroup buttonGroup) {
for (
Enumeration<AbstractButton> buttons = buttonGroup.getElements();
buttons.hasMoreElements();
) {
AbstractButton button = buttons.nextElement();
if (button.isSelected()) {
return button.getText();
}
}
return null;
}

JRadioButton radioA = new JRadioButton(0);
JRadioButton radioB = new JRadioButton(0);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(radioA);
buttonGroup.add(radioB);
JLabel label = new JLabel("The label");
When you click the button to go to the next frame....
if(radioA.isSelected()) {
label.setText("Radio A is Selected");
gotoNextFrame();
}
else if(radioB.isSelected()) {
label.setText("Radio B is Selected");
gotoNextFrame();
}
More - How do I get which JRadioButton is selected from a ButtonGroup

Related

Changing button color after click and resetting it Java

public class ButtonsActionListener implements ActionListener {
String[] buttons = { "Button1", "Button2", "Button3", "Button4"};
for (String btn: buttons ) {
JButton button = new JButton(btn);
this.add(button);
button.addActionListener(this);
}
}
#Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
btn.setBackground(Color.Red);
}
}
What Im trying to do is when the user clicks on for example Button1, Button1's color should turn to grey and when I click on BUtton3, Button1 color should go back to normal and Button3 should turn grey. I dont know how to check the previous click
Your actionPerformed function changes the background of what is currently clicked, so it will not allow you to change other JButton objects without a very rudimentary condition. You should store all of your buttons as unique variables for such cases.

Have a Button Group in Java where all buttons can be deselected?

I want to have a Button Group in which either only one option is selected or none of them are. At the moment, I can get it to have no options ticked by default, and then if one of them is ticked only one of them can be ticked, but I also want to be able to untick the button that was selected. Is this possible?
EDIT: Ideally without having a clear all button, as it would ruin the symmetry of my GUI. Also, here is my code thus far:
ButtonGroup option = new ButtonGroup();
for(int i = 0; i < n; i++) {
JCheckBox check = new JCheckBox("", false);
option.add(check);
row3b.add(check);
}
Just use the clearSelection() method of ButtonGroup :
ButtonGroup.clearSelection()
Clears the selection such that none of the buttons in the ButtonGroup
are selected.
This snippet demonstrates how you can clear selections using ButtonGroup.clearSelection():
//The buttons
JFrame frame = new JFrame("Button test");
JPanel panel = new JPanel();
JRadioButton btn1 = new JRadioButton("Button1");
JRadioButton btn2 = new JRadioButton("Button2");
JButton clearbutton = new JButton("Clear");
panel.add(btn1);
panel.add(btn2);
panel.add(clearbutton);
frame.add(panel);
frame.setVisible(true);
frame.pack();
//The Group, make sure only one button is selected at a time in the group
ButtonGroup btngroup = new ButtonGroup();
btngroup.add(btn1);
btngroup.add(btn2);
btn1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do whatever you want here
}
});
btn2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Do whatever you want here
}
});
clearbutton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// Clear all selections
btngroup.clearSelection();
}
});
As you can see, this creates two JRadioButtons and adds them to group then makes a button that clears selections. Really simple. Or you could create your own radio button class that allows for the unchecking of the button, which is also doable relatively easily.

How do you edit a JComboBox dropdown arrow?

How do you remove the background of a JComboBox dropdown arrow, leaving only the small triangle? The dropdown list was created using the JComboBox tool in Netbeans version 8.
#Drew I don't think the OP wants the button to disappear - he just wants the button to have no visible manifestation other than the arrow triangle:
JComboBox<String> box = new JComboBox<>();
final Color bg = box.getBackground();
box.setUI(new BasicComboBoxUI() {
#Override
protected JButton createArrowButton() {
JButton b = super.createArrowButton();
b.setBackground(bg);
b.setBorderPainted(false);
return b;
}
});

Selecting radio buttons to make text appear

Using radio buttons displayed on a panel, is it possible to select the radio button and then display some text on the panel explaining what the user has selected?
So here is a list of radio buttons
public void RadioButtons() {
btLdap = new JRadioButton ("Ldap");
btLdap.setBounds(60,85,100,20);
panelHolder.add(btLdap);
btKerbegos = new JRadioButton ("Kerbegos");
btKerbegos.setBounds(60,115,100,20);
panelHolder.add(btKerbegos);
btSpnego =new JRadioButton("Spnego");
btSpnego.setBounds(60,145,100,20);
panelHolder.add(btSpnego);
btSaml2 = new JRadioButton("Saml2");
btSaml2.setBounds(60,175,100,20);
panelHolder.add(btSaml2);
}
User selects btLdap
btLdap.setSelected(true);
Now how do you make the text appear on the panel not a message box
If you want to display a text when a radio button is selected you could use ActionListener.
final JTextArea textArea = new JTextArea();
add(textArea);
JRadioButton radioButton = new JRadioButton();
add(radioButton);
radioButton.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
textArea.setText("Selected");
}
});
JRadioButton radioButton2 = new JRadioButton();
add(radioButton2);
radioButton2.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
textArea.setText("Selected 2");
}
});
radioButton.setSelected(true);
When the first is selected it will change the text of the JTextArea with The first is selected!, same the second radio button but with The second is selected.
As you said, radioButton.setSelected(true); setSelected is used to select/deselect a radio button.
In this example i used textArea, but you can use everything which have a method to change the text it contains (an image too!)
Official DOC, here.
Anyway, actionPerformed is not called when setSelected is used so i would go to something like a method
private void updateText(int index)
{
String text = null;
switch (index)
{
case 0:
text = "Selected";
break;
case 1:
text = "Selected 2";
break;
}
textArea.setText(text);
}
And then call updateText(0 or 1 etc.) when you want to select setSelected another radio button and update the text too.
All this is useful, if you want to show a "what happens if you press it" message, but if you just want to change the text of the area with the text of the radio button, just use
textArea.setText(e.getActionCommand());
Here is some Example how to use an ActionListener on a JRadioButton:
public class ListenerExample extends JFrame implements ActionListener {
private JRadioButton check = new JRadioButton("hello");
private JLabel label = new JLabel();
public ListenerExample() {
check.addActionListener(this);
add(check);
add(label);
setLayout(new FlowLayout());
setSize(800, 600);
setVisible(true);
}
public static void main(String[] args) {
new ListenerExample();
}
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JRadioButton) {
JRadioButton button = (JRadioButton) e.getSource();
label.setText(String.valueOf(check.isSelected()));
}
}
}
Using an anonymous inner class you will have something like:
yourRadioButton.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent event ) {
// here you will write the code that you want to
// be executed when your radio is clicked
yourLabel.setText( "Some text..." );
// if you want to exeute it onlye when it is selected
// you will need to do this
if ( yourRadioButton.isSelected() ) {
// some code here
}
}
});
In Java 8, recently released, you can use a lambda expression to register your listener. Something like:
yourRadioButton.addActionListener( event -> {
// here you will write the code that you want to
// be executed when your radio is clicked
yourLabel.setText( "Some text..." );
// if you want to exeute it onlye when it is selected
// you will need to do this
if ( yourRadioButton.isSelected() ) {
// some code here
}
});

Java Radio button clear/deselect

I am trying to deselect JRadio button while clear button is pressed. I have tried googling and gone thru lots of forum the only fix i could find is create an invisible button and while clear button is pressed select invisible one. Is there any other method i can use? My code is as follows
public class deselectRadioBtn extends JFrame {
private JRadioButton[] buttons; // array for JRadio buttons
public deselectRadioBtn() {
super("Deselect Radio");
for (int nbrOfButtons = 0; nbrOfButtons < options.length; nbrOfButtons++) {
//create new JRadioButtons and labels and add ( ) around label
buttons[nbrOfButtons] = new JRadioButton(( nbrOfButtons + radioLabel[nbrOfButtons] ));
//add buttons to eastPanel
rightPanel.add(options[nbrOfButtons]);
}//end for (JRadio)
//Create a ButtonGroup object, add buttons to the group
ButtonGroup optionSelect = new ButtonGroup();
optionSelect.add(buttons[0]);
optionSelect.add(buttons[1]);
optionSelect.add(buttons[2]);
optionSelect.add(buttons[3]);
}
This is just a piece of code and i havent include full code.
Use ButtonGroup.clearSelection().

Categories

Resources