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);
}
}