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 & HelpPrograms › Button in gui cannot call invokeAndWait[SOLVED]
Page Index Toggle Pages: 1
Button in gui cannot call invokeAndWait[SOLVED] (Read 770 times)
Button in gui cannot call invokeAndWait[SOLVED]
Nov 25th, 2009, 1:45pm
 
I wrote this small program to test the use of a a gui in a separate JFrame. It works fine with the radiobuttons, sliders, and checkboxes, but when I tried to add a button for saving the sketch to an image, I got a "cannot call invokeAndWait from the event dispatcher thread" error. This must be some sort of conflict between processing and swing's event handling?

[How could I get around this - use a JFileChooser instead? I would then have to get the text from the File object. to use in the processing loadImage method... myFile.getName(), maybe. I will try this and see if it works.] - edit - this is for the loading file issue, not saving -but has same problem.

Any other suggestions welcome Cool

Code:
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

JFrame frame;
JPanel panel;
JLabel label;
JSlider Slider;
JButton button;
JRadioButton Red, Blue, Green;
ButtonGroup Group;
JCheckBox Smooth, big;
int mode =1; //1=small, 2=big;
int angle;


void setup()
{
 size(400, 400);
 rectMode(CENTER);
 label = new JLabel("Drawing Options");
 frame= new JFrame("Drawing");
 panel = new JPanel();
 button= new JButton("Save");
 Smooth = new JCheckBox("smooth");
 big= new JCheckBox("big");
 Red = new JRadioButton("Red", true);
 Green = new JRadioButton("Green");
 Blue= new JRadioButton("Blue");
 Group = new ButtonGroup();
 Group.add(Red);
 Group.add(Blue);
 Group.add(Green);
 guilistener listener = new guilistener();
 Smooth.addItemListener(listener);
 big.addItemListener(listener);
 Green.addActionListener(listener);
 Red.addActionListener(listener);
 Blue.addActionListener(listener);
 button.addActionListener(listener);
 Slider = new JSlider (JSlider.HORIZONTAL, 0, 360, 0);
 Slider.setMajorTickSpacing (50);
 Slider.setMinorTickSpacing (10);
 Slider.setPaintTicks (true);
 Slider.setPaintLabels (true);
 Slider.addChangeListener(listener);
 panel.add(label);
 panel.add(Smooth);
 panel.add(big);
 panel.add(Red);
 panel.add(Blue);
 panel.add(Green);
 panel.add(Slider);
 panel.add(button);
 frame.getContentPane().add(panel);
 frame.pack();
 frame.setVisible(true);
 
 noStroke();
 background(255);
 fill(255,0,0);
 
}

void draw()
{
 background(255);
 pushMatrix();
 translate(width/2, height/2);
 rotate(radians(angle));
 if (mode==1)
 rect(0, 0 ,100, 100);
 else rect(0,0,200, 200);
 popMatrix();
}


class guilistener implements ItemListener, ChangeListener, ActionListener
{
 public void itemStateChanged(ItemEvent event) // for checkboxes
 {
   if(Smooth.isSelected()) smooth();
   else noSmooth();
   
   if (big.isSelected()) mode=2;
   else mode=1;
 }
  public void stateChanged (ChangeEvent event) //for sliders
  {
  angle = Slider.getValue();
  }
 
  public void actionPerformed(ActionEvent event) //for radio buttons
  {
 Object source = event.getSource();
 
   if (source==Green) fill(0,255,0);
   else if (source==Red) fill(255,0,0);
   else if (source==Blue) fill(0,0,255);
   else if(source==button)
   {
String savepath=selectOutput();
if(savepath!=null) save(savepath);
   }
  }
 
}  




Re: Button in gui "cannot call invokeAndWait...."
Reply #1 - Nov 25th, 2009, 2:24pm
 
I solved this by using a JFileChooser instead - replacing the save method from the above program with:

Code:

else if(source==button)
   {
chooser.showSaveDialog(frame);
save(chooser.getSelectedFile().getPath());
   }


And you have to import javax.swing.JFileChooser and declare and initialise the JFileChooser chooser.
Page Index Toggle Pages: 1