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 › Checkboxes - turn on but not off?
Page Index Toggle Pages: 1
Checkboxes - turn on but not off?? (Read 1067 times)
Checkboxes - turn on but not off??
Nov 19th, 2009, 12:19pm
 
Hi...

I'm just starting to play with some Swing gui elements, so that I can have a separate JFrame window with checkboxes, radiobuttons, sliders, etc, for controlling sketch parameters. I set up thi simple sketch with one rect, and a window with two checkboxes, to test the system. The checkboxes turn on fine, but won't turn off - i.e when you check the "smooth" box it turns smoothing on, but when you uncheck it it doesn't turn it off. Same with the "size box".

Any clues, anyone? Thanks in advance.

Code:
 
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import java.awt.*;
import java.awt.event.*;

JFrame frame;
JPanel panel;
JLabel label;
JCheckBox Smooth, big;
int mode =1; //1=small, 2=big;


void setup()
{
size(400, 400);
label = new JLabel("Drawing Options");
frame= new JFrame("Drawing");
panel = new JPanel();
Smooth = new JCheckBox("smooth");
big= new JCheckBox("big");
DrawListener listener = new DrawListener();
Smooth.addItemListener(listener);
big.addItemListener(listener);

panel.add(label);
panel.add(Smooth);
panel.add(big);

frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);

noStroke();
background(255);
fill(0);

}

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

class DrawListener implements ItemListener
{
public void itemStateChanged(ItemEvent event)
{
if(Smooth.isSelected()) smooth();
else noSmooth();

if (big.isSelected()) mode=2;
else mode=1;
}
}
Re: Checkboxes - turn on but not off??
Reply #1 - Nov 19th, 2009, 12:37pm
 
Duh!

Forgot to put the background(255); statement to delete the old stuff once it was changed. Well, my system of combining Processing and Swing Java elements seems to work!
Re: Checkboxes - turn on but not off??
Reply #2 - Nov 19th, 2009, 1:33pm
 
Quote:
my system of combining Processing and Swing Java elements seems to work!

It might work, there was once a library (SpringGUI) to use Swing components in sketches (but it was deprecated in recent versions).
But authors doesn't guarantee it will work on all platforms...
Re: Checkboxes - turn on but not off??
Reply #3 - Nov 19th, 2009, 1:43pm
 
Now I see there is a whole thread dealing with this:

http://processing.org/discourse/yabb2/num_1252048266.html

Well, I'll keep going...the way I am approaching this seems to work so far. If I run into problems I'll let you know.
Re: Checkboxes - turn on but not off??
Reply #4 - Nov 23rd, 2009, 7:24pm
 
I have checkboxes, sliders and radiobuttons working in a separate JFrame, and it all works fine. Just thought I'd report this, in case anyone else was planning to use Swing components to build a gui for their processing application. Note that I only imported the classes I am using, not the whole Swing library. Also, by using a separate processing class (which is contained within the the main class) as the listener, the gui automatically has access to the variables in the main class.

This is a basic prototype - but I have implemented this in a more complex program - my abstract painting generator - and it works fine.

Code:

import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
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;
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();
 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);
 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);
 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();
}

//note -add this class as an extra tab in processing:

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);
  }
 
}  
Page Index Toggle Pages: 1