Transfer letters from buttons to jLabel component - java

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

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.

Reverting color change in Jbutton when another Jbutton selected

My GUI has a list of filtered entries from a db file. For each entry, I have a select Jbutton which shows more details for each entry.
What I am trying to achieve is to have the select JButton change color when pressed, and revert to original color when another entry's button is selected.
The code I have at the moment will change the selected color's button just fine, but remains the same color when another entry's Details button is selected:
//within for loop for each object in database
JButton selectedButton = new JButton("Details");
selectButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
entry.vehicleSelected();
if(e.getSource() instanceof JButton )
((JButton)e.getSource()).setBackground(Color.RED);
((JButton)e.getSource()).setOpaque(true);
((JButton)e.getSource()).setContentAreaFilled(false);
}});
How can I change the code so I can get this effect?
You can introduce a field JButton buttonToReset where the last pressed button is saved and use it
public void actionPerformed(ActionEvent e) {
entry.vehicleSelected();
if(e.getSource() instanceof JButton )
((JButton)e.getSource()).setBackground(Color.RED);
((JButton)e.getSource()).setOpaque(true);
((JButton)e.getSource()).setContentAreaFilled(false);
if (buttonToReset!=null) {
buttonToReset.setBackground(UIManager.getColor("Button.background"));
}
buttonToReset = ((JButton)e.getSource());
}});

Listener for button press wherever in the frame

I have a frame and it has to listen to button press whatever element is selected. Is it possible to make a global listener for this frame, or I have to write listeners for every element I have?
Write a MouseListener/MouseAdapter and share the same instance with all the elements you want to react to.
This other question will give you a clue:
Java MouseListener
I'm not sure this is what you want.
You can make those buttons use the same action listener,
//set up some buttons...
bt1 = new JButton("BUTTON1");
bt2 = new JButton("BUTTON2");
//use the same actionListener
bt1.addActionListener(someActionListener);
bt2.addActionListener(someActionListener);
then in actionPerformed method you can check which button was pressed.
public void actionPerformed(ActionEvent e){
JButton pressedButton = (JButton)e.getSource();
//check which button was pressed
if(pressedButton ==bt1)
System.out.println("Button 1 do something");
else if(pressedButton ==bt2)
System.out.println("Button 2 do something");
}

JAVA: Activating a jFrameButton on "enter" key press when a jTextField is selected

I have a program with a JFrame GUI and I want a button to be clicked when the user hits the Enter key from within a JTextField. Yes I have tried
rootPane.setDefaultButton(jButton5);
but it only works when the text field isn't selected. How could I have it click the button when the text field is selected?
Thank you in advance :D
JTextField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jButton5.doClick();
}});
JTextField is you text field component. Then when you click the enter key, doClick method of JButton is invoked which Programmatically perform a "click". This does the same thing as if the user had pressed and released the button.
You'll have to add actionListener to textField and override method actionPerformed()
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Text=" + text.getText());
//Your logic
}
});

Text Field disabling in NetBeans

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

Categories

Resources