Here’s what I’ve got.
In this bit of code I’m creating a panel that contains 7 sliders, and a combobox.
public JPanel createOscPanel(final Osc o)
{
//create a panel of 2 rows, 4 columns*/
JPanel panel = gc.createComponentPanel(o.NAME, 2, 4);
/*set up combobox panel*/
JPanel cbPanel = new JPanel();
cbPanel.setLayout(new BoxLayout(cbPanel, BoxLayout.Y_AXIS));
/*label*/
JPanel lbPanel = new JPanel();
lbPanel.setLayout(new BoxLayout(lbPanel, BoxLayout.X_AXIS));
lbPanel.setAlignmentY(LEFT_ALIGNMENT);
JLabel cbLabel = new JLabel("Wave Type:");
lbPanel.add(cbLabel);
cbPanel.add(lbPanel);
/*combobox*/
final String[] comboItems = {"Sine", "Custom", "FSaw", "FSquare"};
JComboBox cb = new JComboBox(comboItems);
cb.setMaximumSize(new Dimension (100, 20));
cbPanel.add(cb);
/*add sliders*/
panel.add(gc.createSliderPanel(o.getAmp(), DEFAULT_SLIDER_GRAINS));
panel.add(gc.createSliderPanel(o.getFreq(), DEFAULT_SLIDER_GRAINS));
panel.add(gc.createSliderPanel(o.getPhase(), DEFAULT_SLIDER_GRAINS));
panel.add(cbPanel);
panel.add(gc.createSliderPanel(o.getWidth(), DEFAULT_SLIDER_GRAINS));
panel.add(gc.createSliderPanel(o.getSlope(), DEFAULT_SLIDER_GRAINS));
panel.add(gc.createSliderPanel(o.getCurve(), DEFAULT_SLIDER_GRAINS));
panel.add(gc.createSliderPanel(o.getFourier(), (o.getFourier().MAX - o.getFourier().MIN)));
final Runnable disableCustomSliders = new Runnable() /*this is how do a method-in-method in Java*/
{
public void run()
{
//I need to disable sliders here.
}
};
final Runnable enableCustomSliders = new Runnable() /*this is how do a method-in-method in Java*/
{
public void run()
{
//I need to enable sliders here.
}
};
cb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
int value = cb.getSelectedIndex();
switch(value)
{
case 0: {o.setWaveShape(Osc.WaveShape.SINE); disableCustomSliders.run();} break;
case 1: {o.setWaveShape(Osc.WaveShape.CUSTOM); enableCustomSliders.run();} break;
case 2: {o.setWaveShape(Osc.WaveShape.FSAW); disableCustomSliders.run();} break;
case 3: {o.setWaveShape(Osc.WaveShape.FSQUARE);disableCustomSliders.run();} break;
}
refresh();
}
});
return panel;
}
When you select the Sine, Fsaw, FSquare items in the combobox, you need to disable the Curve, Width and Slope sliders, and when you select the Custom item, you need to re-enable them.
here is the createSliderPanel code.
public JPanel createSliderPanel(
final Parameter parameter,
final int grains)
{
/*create and assign components*/
final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createRaisedBevelBorder());
final JLabel label = new JLabel(parameter.NAME);
final JLabel valueLabel = new JLabel();
/*set up slider*/
int curr = valueToSlider(parameter.MIN.doubleValue(), parameter.MAX.doubleValue(),
parameter.getValue().doubleValue(), parameter.SCALETYPE, parameter.Q, grains);
final JSlider slider = new JSlider(JSlider.VERTICAL, 0, grains, curr);
slider.setPreferredSize(new Dimension(SLIDER_WIDTH, SLIDER_HEIGHT));
slider.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(valueLabel);
panel.add(slider);
panel.add(label);
/*slider move event*/
slider.addChangeListener(new ChangeListener() {
/* (non-Javadoc)
* @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
*/
public void stateChanged(ChangeEvent event) {
double value = sliderToValue(parameter.MIN.doubleValue(), parameter.MAX.doubleValue(),slider.getValue(), parameter.SCALETYPE, parameter.Q, grains);
updateSliderLabel(parameter, valueLabel, parameter.getValue());
/*set the parameter value*/
if (parameter.CLASSTYPE.equals(Double.class))
parameter.setValue(value);
else if (parameter.CLASSTYPE.equals((Integer.class)))
parameter.setValue((int)value);
gui.refresh();
}
});
/*need to reset the valueLabel to it's correct format*/
updateSliderLabel(parameter, valueLabel, parameter.getValue());
return panel;
}
So the question is – how do I reference those sliders in order to do .setEnabled(false) on them?
The immediate and reasonably simple solution I can think of is to refactor createSliderPanel, such that you pass in the parent panel, and add the slider panel to the parent inside the method, and then return the slider.
Is there a better concept that adheres to OO and MVC?
One final question re: effectiveness and etiquette on Stack Overflow – is posting these large chunks of code good manners and effective? or does it make it harder to answer the question?
I, personally, would create a custom component instead of building the slider panel from scratch. This would give you the opportunity to supply “control” methods that could be used change the enabled state of the whole component as well as provide getters and setters that could be used to retrieve the values of the sliders as you need