Text Field disabling in NetBeans - java

I want to ask if there is a way to make the text field active and inactive according to the radio button.
For example, the textfield will be inactive and when the user click on the radio button, the textfield will be active.
I am using Java language and NetBeans program

You could have two radio buttons for representing the active/inactive state. Add an action listener to each and when the 'active' one is pressed you call setEditable(true) on the JTextField and when the 'inactive' JRadioButton is called you call setEditable(false).
JTextField textField = new JTextField();
JRadioButton activeButton = new JRadioButton("Active");
JRadioButton inactiveButton = new JRadioButton("Inactive");
activeButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textField.setEditable(true);
}
});
inactiveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
textField.setEditable(false);
}
});

Related

Transfer letters from buttons to jLabel component

How can I transfer the value of a button and display it as a string in a JLabel? For example, when a button with a value of "A" is pressed, I want the text of the JLabel component set to "A".
Add listener to JButton and if it's clicked then setText("A") on your JLabel
Use the listener on button click and set Text to jLabel.
jButton.setOnClickListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
JButton button = (JButton)actionEvent.getSource();
jLabel.setText(button.getText());
}
});

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.

display text box after clicking button- java

I'm new to Java and I'm working on an airplane seat reservation program. I would like to know how to display a JTextfield text box and a JLabel after clicking a button that is already displayed in a JFrame window. I have tried to add the button after an if statement but it doesn't work. I have also tried button.setVisible(false) and then making it true later. I have a button.actionlistener(this) for each of the buttons used.
public void actionPerformed(ActionEvent event){
Container contentPane = null;
JButton clickedButton = (JButton) event.getSource();
if (event.getSource() == coach){
five.setForeground(Color.green);
eleven.setForeground(Color.green);
six.setForeground(Color.green);
two.setForeground(Color.black);
if (event.getSource() == five) {
inputLine = new JTextField();
inputLine.setBounds(110, 180, 185, 22);
contentPane.add(inputLine);
}
} else if (event.getSource() == firstClass){
two.setForeground(Color.green);
five.setForeground(Color.black);
eleven.setForeground(Color.black);
six.setForeground(Color.black);
}
Am not totally clear by your information, whether u mean to display the created textfields and labels on the created button or create the same on the created button.
But I have some code for you, see if it helps.
JTextField[] desc=new JTextField[20];
JButton more=new JButton("More");
Container c=getContentPane();
public void actionPerformed(ActionEvent a1)
{
String s1=a1.getActionCommand();
String s="";
for(i=0;i<cnt;i++)
s=s+desc[i].getText();
if(s1.equals("More"))
{
System.out.println("Created");
desc[i]=new JTextField();
desc[i].setBounds(50,y,150,30);
c.add(desc[i]);
}
}
Same way create for JLabel.

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
}
});

Categories

Resources