Ron's Brain

Windmills do not work that way!
8 total messages. For real this time.
Gadolinium
05/14/2006 @ 18:47:03 EDT

So basically im writing a popup button box in java. I have everything working except i need somehow for the class to wait until a button is pressed to return a boolean value back to the program. Any Ideas?

Hydrogen
05/14/2006 @ 21:37:18 EDT

ActionListener

And doesn't Java already have some sort of a popup thingy? Or am I completely misunderstanding what you're trying to do?

Gadolinium
05/14/2006 @ 22:10:32 EDT

Well maybe, but I cant find one that I like. I dont know how to use the wait method i keep getting a runtime error of current thread not owner.

here is my current code


import javax.swing.AbstractButton;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class ButtonDemo extends JPanel
implements ActionListener {
private boolean actionDone=false;
protected JButton b1, b2, b3;
private static JFrame frame2 =null;

public ButtonDemo() {
b1 = new JButton("Yes");
b1.setVerticalTextPosition(AbstractButton.BOTTOM);
b1.setHorizontalTextPosition(AbstractButton.LEFT);
b1.setMnemonic(KeyEvent.VK_D);
b1.setActionCommand("Yes");

b2 = new JButton("No");
b2.setVerticalTextPosition(AbstractButton.BOTTOM);
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setMnemonic(KeyEvent.VK_M);
b2.setActionCommand("No");
b1.addActionListener(this);
b2.addActionListener(this);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
String message="Yes or no?";

JLabel label = new JLabel(message);
panel1.add(label);
add(panel1);
panel2.add(b1);
panel2.add(b2);
add(panel2);

}

public synchronized void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());

if ("Yes".equals(e.getActionCommand())) {
actionDone=true;
notifyAll();
}
else if("No".equals(e.getActionCommand())) {
actionDone=true;
notifyAll();
}
}
private boolean actionDone()
{return actionDone();}

private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame2=frame;
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
ButtonDemo newContentPane = new ButtonDemo();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);


frame.pack();
frame.setVisible(true);
}

public synchronized void buttonInput() {
createAndShowGUI();

try {
wait();
} catch (InterruptedException e) { }

System.out.println("Here");
}
public static void main(String args[])
{
ButtonDemo b=new ButtonDemo();
b.runMe();}
public void runMe()
{ buttonInput();}
}



Basically what I want in place of the button input method would have it return a boolean at the end after a button is pressed. When creating this I was trying to make it completly encapsulated so that all i would do it call buttonInput and have it return a boolean true for yes and false for no.

Hassium
05/16/2006 @ 15:54:08 EDT

o/~ If you're happy and you know it {return actionDone();} o/~

Gadolinium
05/16/2006 @ 20:49:05 EDT

Well I need major help. I need to get the wait mehod to work. It is absolutly necesary to my final project please I cannot get it to work properly.

Hydrogen
05/16/2006 @ 21:33:50 EDT

Sounds like you're trying to write the Dialog class.

Gadolinium
05/16/2006 @ 21:44:24 EDT

I have other things that I cant use dialouge class for. This is my simplest object. I need to be able to use wait and I have no idea why my notify's dont wake up the the waiting thread.

Strontium
06/09/2006 @ 18:07:09 EDT

If ActionListener isn't getting the job done, maybe you should call ActionJackson.

Recent users
Logged on users that have pretended to enjoy this site within the last five minutes
There are no logged on users. How sad!