We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › question about GUI event handling
Page Index Toggle Pages: 1
question about GUI event handling (Read 2883 times)
question about GUI event handling
Apr 28th, 2005, 10:16pm
 
Hello,

Now I'm doing simple work using customized GUI interface.
The problem at the monent is button handling.
What I'd like to do is showing JFileChooser by pressing a button, but because my button check the mouse-click in the draw() loop as below, there's a problem.

void draw() {
 ...
 if(aButton.pressed()) {
   JFrame f = new JFrame("File Selection Test");
   JFileChooser fc = new JFileChooser();
   fc.showOpenDialog(f);
 }
}

and in a MyButton class,
boolean pressed() {
 if(mouseX>=x && mouseX<=(x+w) &&
          mouseY>=y && mouseY<=(y+t)) {
   if(mousePressed) {
     return true  
   } else {
     return false;
   }
 }else {
   return false;
}

because this loop runs by framerate, though I pressed mouse button(or I thought that) one time, it actually runs more than one time, and it crashes.
I found if I use AWT button instead this isn't a problem, but then actual P5 palette(or pane) is not the same as button's, so I need to think about the layout.

So, my questions are,
1) Is it right approach to open file-selection window?
2) Are there another way to use component's events?
3) Is it possible to add AWT components into main P5 pane directly?

Thanks!
Sou
Re: question about GUI event handling
Reply #1 - Apr 29th, 2005, 12:19am
 
1) not really.. use inputFile() or inputFile("Choose a file"), which is a wrapper on the file chooser class that just returns the selected file (or null) for you. this might not be documented Wink

2) not sure i understand.

3) short answer is no, long answer is that you should instead be making your own java application, and you can embed a PApplet from processing inside of that, since a PApplet is just a Component like any other. unfortunately, to do that sort of thing you should use something like eclipse since processing's not really made for it.
Re: question about GUI event handling
Reply #2 - Apr 29th, 2005, 6:01am
 
Thanks, Fry.

I've check inputFile(). Finally inputFile() or FileDialog works fine in PDE(though sometimes gets error), but I've found it doesn't work properly in Eclipse. For example, if I use the code below in a loop or mouse-event method, (when using in setup(), that's ok)

File file = inputFile();
if(file!=null) {
 System.out.println(file.getName());
}

in PDE it works, but in Eclipse line3 which prints filename runs before dialog popup or freeze. It seems strange to me Sad
Re: question about GUI event handling
Reply #3 - Apr 29th, 2005, 6:31am
 
You probably want to declare
Code:
File file; 

as a global,
initialize it in setup (or through some triggered event),
Code:
void setup() {
size(200, 200, P3D); // don't forget this line
file = inputFile();
}

then in draw, work with the file
Code:
void draw() {
if(file != null) {
println(file.getName());
}
}
Re: question about GUI event handling
Reply #4 - Apr 29th, 2005, 8:16am
 
moving this over to the "integration" board.. for eclipse questions, it's mostly community supported or you're on your own since it's not something that casey and i can really support..
Re: question about GUI event handling
Reply #5 - Apr 30th, 2005, 11:14am
 
Thanks, Damkjer and Fry.

What I'd actually like to do is to make the FileDialog pop whenever I click a certain button, so I put a line which calls inputFile() method in a mousePressed() method as below.

void mousePressed() {
 ...
 if(aButton(pressed()) {
   file = inputFile();
   ...  
 }
}

Sometimes it works and sometimes fails and freeze. And to me it seems to be related with P5 applet's timing.
Page Index Toggle Pages: 1