private JSlider makeJSlider() { JSlider slider = new JSlider(JSlider.VERTICAL, 0, 10, 1); slider.setMajorTickSpacing(1); slider.setPaintTicks(true); slider.setSnapToTicks(true); Hashtable labelTable = new Hashtable(); for (int i = 0; i <= 10; i += 2) { labelTable.put(new Integer(i), new JLabel(i + " Seconds")); } slider.setLabelTable(labelTable); slider.setPaintLabels(true); return slider; }
public SliderTestFrame() { setTitle("SliderTest"); setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); sliderPanel = new JPanel(); sliderPanel.setLayout(new FlowLayout(FlowLayout.LEFT)); // common listener for all sliders listener = new ChangeListener() { public void stateChanged(ChangeEvent event) { // update text field when the slider value changes JSlider source = (JSlider) event.getSource(); textField.setText("" + source.getValue()); } }; // add a plain slider JSlider slider = new JSlider(); addSlider(slider, "Plain"); // add a slider with major and minor ticks slider = new JSlider(); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); addSlider(slider, "Ticks"); // add a slider that snaps to ticks slider = new JSlider(); slider.setPaintTicks(true); slider.setSnapToTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); addSlider(slider, "Snap to ticks"); // add a slider with no track slider = new JSlider(); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); slider.setPaintTrack(false); addSlider(slider, "No track"); // add an inverted slider slider = new JSlider(); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); slider.setInverted(true); addSlider(slider, "Inverted"); // add a slider with numeric labels slider = new JSlider(); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); addSlider(slider, "Labels"); // add a slider with alphabetic labels slider = new JSlider(); slider.setPaintLabels(true); slider.setPaintTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(5); Dictionary<Integer, Component> labelTable = new Hashtable<Integer, Component>(); labelTable.put(0, new JLabel("A")); labelTable.put(20, new JLabel("B")); labelTable.put(40, new JLabel("C")); labelTable.put(60, new JLabel("D")); labelTable.put(80, new JLabel("E")); labelTable.put(100, new JLabel("F")); slider.setLabelTable(labelTable); addSlider(slider, "Custom labels"); // add a slider with icon labels slider = new JSlider(); slider.setPaintTicks(true); slider.setPaintLabels(true); slider.setSnapToTicks(true); slider.setMajorTickSpacing(20); slider.setMinorTickSpacing(20); labelTable = new Hashtable<Integer, Component>(); // add card images labelTable.put(0, new JLabel(new ImageIcon("nine.gif"))); labelTable.put(20, new JLabel(new ImageIcon("ten.gif"))); labelTable.put(40, new JLabel(new ImageIcon("jack.gif"))); labelTable.put(60, new JLabel(new ImageIcon("queen.gif"))); labelTable.put(80, new JLabel(new ImageIcon("king.gif"))); labelTable.put(100, new JLabel(new ImageIcon("ace.gif"))); slider.setLabelTable(labelTable); addSlider(slider, "Icon labels"); // add the text field that displays the slider value textField = new JTextField(); add(sliderPanel, BorderLayout.CENTER); add(textField, BorderLayout.SOUTH); }
private void makeComponents() { this.getWwd().setPreferredSize(new Dimension(1024, 768)); JPanel panel = new JPanel(new BorderLayout()); { panel.setBorder(new EmptyBorder(10, 0, 10, 0)); JPanel controlPanel = new JPanel(new BorderLayout(0, 10)); controlPanel.setBorder(new EmptyBorder(20, 10, 20, 10)); JPanel btnPanel = new JPanel(new GridLayout(5, 1, 0, 5)); { JButton btn = new JButton("Zoom to Matterhorn"); btn.setActionCommand(ACTION_COMMAND_BUTTON1); btn.addActionListener(this.controller); btnPanel.add(btn); btn = new JButton("DEMO getElevations()"); btn.setActionCommand(ACTION_COMMAND_BUTTON2); btn.addActionListener(this.controller); btnPanel.add(btn); btn = new JButton("DEMO getElevation()"); btn.setActionCommand(ACTION_COMMAND_BUTTON3); btn.addActionListener(this.controller); btnPanel.add(btn); btn = new JButton("DEMO new getElevations"); btn.setActionCommand(ACTION_COMMAND_BUTTON4); btn.addActionListener(this.controller); btnPanel.add(btn); // btn = new JButton("Button 5"); // btn.setActionCommand(ACTION_COMMAND_BUTTON5); // btn.addActionListener(this.controller); // btnPanel.add(btn); } controlPanel.add(btnPanel, BorderLayout.NORTH); JPanel vePanel = new JPanel(new BorderLayout(0, 5)); { JLabel label = new JLabel("Vertical Exaggeration"); vePanel.add(label, BorderLayout.NORTH); int MIN_VE = 1; int MAX_VE = 8; int curVe = (int) this.getWwd().getSceneController().getVerticalExaggeration(); curVe = curVe < MIN_VE ? MIN_VE : (curVe > MAX_VE ? MAX_VE : curVe); JSlider slider = new JSlider(MIN_VE, MAX_VE, curVe); slider.setMajorTickSpacing(1); slider.setPaintTicks(true); slider.setSnapToTicks(true); Hashtable<Integer, JLabel> labelTable = new Hashtable<Integer, JLabel>(); labelTable.put(1, new JLabel("1x")); labelTable.put(2, new JLabel("2x")); labelTable.put(4, new JLabel("4x")); labelTable.put(8, new JLabel("8x")); slider.setLabelTable(labelTable); slider.setPaintLabels(true); slider.addChangeListener( new ChangeListener() { public void stateChanged(ChangeEvent e) { double ve = ((JSlider) e.getSource()).getValue(); ActionEvent ae = new ActionEvent(ve, 0, ACTION_COMMAND_VERTICAL_EXAGGERATION); controller.actionPerformed(ae); } }); vePanel.add(slider, BorderLayout.SOUTH); } controlPanel.add(vePanel, BorderLayout.SOUTH); panel.add(controlPanel, BorderLayout.SOUTH); this.layerPanel = new LayerPanel(this.getWwd(), null); panel.add(this.layerPanel, BorderLayout.CENTER); } getContentPane().add(panel, BorderLayout.WEST); }
public NumberField(NumberOption option) { super(option); panel = new JPanel(new GridBagLayout()); this.step = option.getStep(); Double min = option.getMin(); Double max = option.getMax(); Double defl = new Double(option.getDefault()); // Normalize parameters if (min != null && defl.compareTo(min) < 0) { defl = min; } else if (max != null && defl.compareTo(max) > 0) { defl = max; } if (min != null) { sliderMin = (int) (min.doubleValue() / step); } else { sliderMin = SLIDER_DEFAULT_MIN; } if (max != null) { sliderMax = (int) (max.doubleValue() / step); } else { sliderMax = SLIDER_DEFAULT_MAX; } // Create spinner SpinnerNumberModel spinnerModel = new SpinnerNumberModel(defl, min, max, new Double(step)); spinner = new JSpinner(spinnerModel); ((JSpinner.DefaultEditor) spinner.getEditor()).getTextField().setColumns(FIELD_WIDTH); // Create slider slider = new JSlider(sliderMin, sliderMax, sliderIndex(defl)); slider.setPaintLabels(false); slider.setPaintTicks(false); slider.setSnapToTicks(false); // Add listeners. The spinner is the master and the slider is // the slave. spinner.addChangeListener( new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int newIndex = sliderIndex((Double) spinner.getValue()); if (slider.getValue() != newIndex) { slider.setValue(newIndex); } fireChangeEvent(); } }); slider.addChangeListener( new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int newIndex = slider.getValue(); if (newIndex != sliderIndex((Double) spinner.getValue())) { spinner.setValue(new Double(newIndex * step)); } } }); // Create enable checkbox configureEnableToggle( option.isInitiallyEnabled(), string(option.getDisabledValue()), Arrays.asList((JComponent) spinner, slider)); // Add to the panel panel.add(spinner); GridBagConstraints c = new GridBagConstraints(); c.insets = new Insets(0, 8, 0, 0); c.fill = GridBagConstraints.HORIZONTAL; c.weightx = 1; panel.add(slider, c); }