I have a JFrame (called FTask) with public void method. Example code:
public void clear() {
jTable1.clearSelection();
jButton1.setEnabled(false);
jButton3.setEnabled(false);
jButton2.setEnabled(false);
jTextArea1.setText(null);
}
Then, I have JDialog with a button. I want when I click the button, frame do the 'clear' method to the frame.
I've tried:
FTask ft = new FTask();
ft.clear();
But it didn't work.
I've tried:
FTask ft = new FTask();
ft.clear();
But it didn't work.
No, it wouldn't. This code is creating a new (2nd instance) of the frame that is not set visible. What you need is a reference to the original frame.
This can be fixed in a number of ways, too broad to go into here, and is Object Oriented Programming 101 that should be mastered before trying to write GUI'd apps. - which add their own complications.
You have to use actionlistener in order to run the code when the button is clicked.
JButton button = new JButton("Click me");
//Add action listener to button
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
if(e.getSource() == button){
System.out.println("You clicked the button");
//In your case:
ft.clear();
}
}
});
As #Menno said, you have to use ActionListener in order to detect Button Clicks
Here is the Java 8 Style:
JButton button = new JButton("Click me");
//Add action listener to button
button.addActionListener(
ae -> ft.clear();
);
// Add button to frame
add(button);
Related
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.
I am creating a GUI application using Java. There are different sections in the main frame, each having a particular functionality. The header panel contains the buttons Submit, Undo, Shuffle, Help and Quit. Here is the sample code:
JPanel header = new JPanel();
JButton submit = new JButton("Submit");
header.add(submit);
JButton undo= new JButton("Undo");
header.add(undo);
JButton shuffle= new JButton("Shuffle");
header.add(shuffle);
JButton help= new JButton("Help");
header.add(help);
JButton quit = new JButton("Quit");
header.add(quit);
Further down my code, I need to check that the button clicked is not in the header panel (there are buttons in other panels too).
public void actionPerformed(ActionEvent e)
{
String clicked = e.getActionCommand();
if(!clicked.equals("Submit") && !clicked.equals("Undo") && !clicked.equals("Help")&& !clicked.equals("Quit") && !clicked.equals("Shuffle")){
//some code here
Is there any alternate neater way to check that the button clicked is not in the header panel? I will need to do something similar in another panel which contains more buttons. Using IF statements to check for each button is inefficient and untidy, I believe.
Honestly, I'd just implement actions for every button. It may require more code but it's a better solution. But if you insist to go this way you can try this
public void actionPerformed(ActionEvent e) {
if (!Arrays.asList(PANEL.getComponents()).stream().filter(b -> b instanceof JButton)
.map(b -> (JButton) b).filter(b ->
b.getText().equals(e.getActionCommand())).findFirst().isPresent()) {
// execute code if button is not a child of PANEL
}
}
You can use addActionListener for every button separately.
I have created and added added a button to JDialog as follows:
JDialog dialog = new JDialog();
dialog.setLayout(new GridLayout(6, 1));
dialog.add(new JButton("test"));
This adds the button JDialog. But is there anyway I could add ActionListener to it?
I know this is possible if I create a whole new button itself like:
JButton button = new JButton("test");
button.addActionListener....
dialog.add(button);
But I am wondering if I can do without this.
So far I reached to the point dialog.getRootPane().getContentPane().getComponent(1) but stuck here with no idea on a way to implement an actionListener. Any help would be appreciated.
I don't think there is a way to add listeners while initializing the JButton.
Initializing the button and adding the listener will do as you said.
The other way, you can have an utility method to create a JButton with listeners as below.
dialog.add(getButton("Test", new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
// Action Logic
}
}));
private JButton getButton(String name, ActionListener listener) {
JButton button = new JButton(name);
button.addActionListener(listener);
return button;
}
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");
}
I am creating an application that has 1 JFrame java file and 1 JDialog java file.
In the JFrame, I have a button and when pressed I want it to display what I have designed in the JDialog.
So for example, my JFrame java file is called MMainView.java and my JDialog is called OptionView.java. So when the button in MMainView.java is pressed I want to display the JDialog I have designed in OptionView.java.
So in my MMainView.java file, I have a function that is called when that button is pressed. How do I display the dialog in OptionView.java?
SOLVED
For those wondering. This is what I did:
private JDialog optionView; ~~> JDialog Declaration
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (optionView == null) {
JFrame mainFrame = myApp.getApplication().getMainFrame();
optionView = new OptionView(mainFrame, true);
optionView.setLocationRelativeTo(mainFrame);
}
myApp.getApplication().show(optionView);
}
Sounds like you want to create an ActionListener for your button, and set the visibility of the JDialog to true when you press the button.
Something on these lines:
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent actionevent)
{
//set the visibility of the JDialog to true in here
}
});
Let's say your button is called myBtn.
The class should look like this.
public class MMainView extends JFrame
implements ActionListener
You should use a listener for the button.
JButton myBtn = new JButton();
myBtn.addActionListener(this);
And finally:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == myBtn) {
new OptionView();
You don't really need the if, it's just in case you want to add more buttons for the actionPerformed.
First register the button in "MainView.java", like below.
b1.addActionListener(this);
b1.setName("OpenJDialog");
//this is to read in actionperformed method incase you have more than one button
// in action performed method call the dialogue class
public void actionPerformed(ActionEvent ae){
JButton jb = (JButton)ae.getSource();
String str = jb.getName();
if(str.equals("OpenJDialog"){
new OptionView();
//I am assuming u are configuring jdialog content in OptionView constructor
}
}