Re: getting a JSlider to expand
In article <jNZdk.72234$Jx.23947@pd7urf1no>,
thufir <hawat.thufir@gmail.com> wrote:
red, green and blue are instance of LabelSliderText, which extends
JPanel. How can I get the three "rows", beans, to line up vertically?
I suppose I could pass in:
"red "
"green"
"blue "
so that the spacing matches up?
[...]
Can you use setHorizontalAlignment on the JLabel?
Also, I can't figure out how to make the JSlider expand horizontally when
the JFrame is enlarged. The surrounding JPane, "green " for instance,
does seem to expand.
Can you set the horizontal component of your JSlider's preferred size
to a large number and add it to a panel having a suitable layout?
<sscce>
import java.awt.*;
import javax.swing.*;
/**
* JSliderTest
* @author John B. Matthews
*/
public class JSliderTest extends JPanel {
public static void main(String args[]) {
JFrame frame = new JFrame("Sliders!");
frame.setContentPane(new JSliderTest());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 200);
frame.setVisible(true);
}
public JSliderTest() {
this.setLayout(new GridLayout(3, 1));
this.add(genSliderPanel("Red"));
this.add(genSliderPanel("Green"));
this.add(genSliderPanel("Blue"));
}
private JPanel genSliderPanel(String name) {
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
JLabel label = new JLabel(name);
label.setHorizontalAlignment(JLabel.RIGHT);
label.setMinimumSize(new Dimension(60, 0));
panel.add(label);
JSlider slider = new JSlider();
slider.setPreferredSize(new Dimension(Short.MAX_VALUE, 0));
panel.add(slider);
return panel;
}
}
</sscce>
I'm sure you already looked at JColorChooser.
[...]
--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews