Example usage for javax.swing.event ChangeListener ChangeListener

List of usage examples for javax.swing.event ChangeListener ChangeListener

Introduction

In this page you can find the example usage for javax.swing.event ChangeListener ChangeListener.

Prototype

ChangeListener

Source Link

Usage

From source file:Test.java

public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.add(new JButton("Button on Tab 0"));
    tabs.addTab("Tab 0", p);

    p = new JPanel();
    p.add(new JTextField("Text on Tab 1"));
    tabs.addTab("Tab 1", p);

    tabs.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            System.out.println("Tab=" + tabs.getSelectedIndex());
        }/*from  www  .j a  va2 s  .c om*/
    });
    getContentPane().add(tabs, "Center");
    pack();
}

From source file:TabbedPane1.java

public void init() {
    for (int i = 0; i < flavors.length; i++)
        tabs.addTab(flavors[i], new JButton("Tabbed pane " + i));
    tabs.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            txt.setText("Tab selected: " + tabs.getSelectedIndex());
        }//from   www  . j  av a 2 s. co m
    });
    Container cp = getContentPane();
    cp.add(BorderLayout.SOUTH, txt);
    cp.add(tabs);
}

From source file:ColorComposite.java

public ColorComposite() {
    super();//from  w  w w  .j a v a 2  s  . co m
    Container container = getContentPane();

    canvas = new MyCanvas();
    container.add(canvas);

    JPanel panel = new JPanel();

    JLabel label = new JLabel("Color-Composite: ");

    JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 100, 65);
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider tempSlider = (JSlider) e.getSource();
            alphaValue = (float) (tempSlider.getValue() / 100.0);
            textField.setText(Float.toString(alphaValue));
            canvas.repaint();
        }
    });

    textField = new JTextField("0.65", 4);

    panel.add(label);
    panel.add(slider);
    panel.add(textField);

    container.add(BorderLayout.SOUTH, panel);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    setSize(450, 450);
    setVisible(true);
}

From source file:Main.java

public void makeUI() {
    String[] zones = { "Asia/Tokyo", "Asia/Hong_Kong", "Asia/Calcutta", "Europe/Paris", "Europe/London",
            "America/New_York", "America/Los_Angeles" };
    JLabel[] labels = new JLabel[zones.length];
    SimpleDateFormat[] formats = new SimpleDateFormat[zones.length];
    JFrame frame = new JFrame();

    Calendar cal = Calendar.getInstance();
    Date date = cal.getTime();/*from   w  ww .  j  a  va2s  .c om*/
    SpinnerDateModel model = new SpinnerDateModel();
    model.setValue(date);
    JSpinner spinner = new JSpinner(model);
    spinner.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            Date date = (Date) ((JSpinner) e.getSource()).getValue();
            for (int i = 0; i < labels.length; i++) {
                labels[i].setText(formats[i].format(date));
            }
        }
    });
    SimpleDateFormat format = ((JSpinner.DateEditor) spinner.getEditor()).getFormat();
    format.setTimeZone(TimeZone.getTimeZone(zones[0]));
    format.applyPattern("yyyy-MM-dd HH:mm:ss");
    JPanel panel = new JPanel(new GridLayout(zones.length, 2, 10, 10));
    for (int i = 0; i < zones.length; i++) {
        formats[i] = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
        formats[i].setTimeZone(TimeZone.getTimeZone(zones[i]));
        JLabel label = new JLabel(zones[i]);
        labels[i] = new JLabel(formats[i].format(date));
        panel.add(label);
        panel.add(labels[i]);
    }
    frame.setLayout(new BorderLayout());
    frame.add(spinner, BorderLayout.NORTH);
    frame.add(panel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:ButtonScroll.java

public ButtonScroll() {
    super("Scrolling Programmatically");
    setSize(400, 400);//from   ww  w . j  av  a 2  s.  c o  m
    getContentPane().setLayout(new BorderLayout());

    ImageIcon shuttle = new ImageIcon("largeJava2sLogo.GIF");
    pgVertical = shuttle.getIconHeight() / 5;
    pgHorzontal = shuttle.getIconWidth() / 5;
    JLabel lbl = new JLabel(shuttle);

    viewport = new JViewport();
    viewport.setView(lbl);
    viewport.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            enableButtons(ButtonScroll.this.viewport.getViewPosition());
        }
    });
    getContentPane().add(viewport, BorderLayout.CENTER);

    JPanel pv = new JPanel(new BorderLayout());
    upButton = createButton("up", 'u');
    ActionListener lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            movePanel(0, -1);
        }
    };
    upButton.addActionListener(lst);
    pv.add(upButton, BorderLayout.NORTH);

    downButton = createButton("down", 'd');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            movePanel(0, 1);
        }
    };
    downButton.addActionListener(lst);
    pv.add(downButton, BorderLayout.SOUTH);
    getContentPane().add(pv, BorderLayout.EAST);

    JPanel ph = new JPanel(new BorderLayout());
    leftButton = createButton("left", 'l');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            movePanel(-1, 0);
        }
    };
    leftButton.addActionListener(lst);
    ph.add(leftButton, BorderLayout.WEST);

    rightButton = createButton("right", 'r');
    lst = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            movePanel(1, 0);
        }
    };
    rightButton.addActionListener(lst);
    ph.add(rightButton, BorderLayout.EAST);
    getContentPane().add(ph, BorderLayout.SOUTH);

    WindowListener wndCloser = new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    };
    addWindowListener(wndCloser);

    setVisible(true);
    movePanel(0, 0);
}

From source file:SineDraw.java

public Main() {
    super(new BorderLayout());
    add(BorderLayout.CENTER, sines);
    adjustCycles.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            sines.setCycles(((JSlider) e.getSource()).getValue());
        }// www  . j a v  a  2 s  . co m
    });
    add(BorderLayout.SOUTH, adjustCycles);
}

From source file:biomine.bmvis2.pipeline.SizeSliderOperation.java

@Override
public JComponent getSettingsComponent(final SettingsChangeCallback v, final VisualGraph graph) {
    final JSlider sizeSlider;
    sizeSlider = new JSlider(0, graph.getAllNodes().size() + 10);
    sizeSlider.setValue(graph.getNodes().size());

    sizeSlider.addChangeListener(new ChangeListener() {
        @Override/*w  w w. java  2 s  .  co m*/
        public void stateChanged(ChangeEvent arg0) {
            targetSize = sizeSlider.getValue();
            v.settingsChanged(false);
        }
    });

    return sizeSlider;
}

From source file:AlphaCompositeDemo.java

public AlphaCompositeDemo() {
    super();/*from w ww.jav  a2s  . c om*/
    Container container = getContentPane();

    canvas = new MyCanvas();
    container.add(canvas);

    rulesBox = new JComboBox(rulesLabels);
    rulesBox.setSelectedIndex(0);
    rulesBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    rulesBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            canvas.compositeRule = rules[cb.getSelectedIndex()];
            canvas.repaint();
        }
    });

    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(25);
    slider.setMinorTickSpacing(25);
    slider.setPaintLabels(true);
    slider.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            JSlider slider = (JSlider) e.getSource();
            canvas.alphaValue = (float) slider.getValue() / 100;
            canvas.repaint();
        }
    });

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(1, 3));
    panel.add(rulesBox);
    panel.add(new JLabel("Alpha Adjustment x E-2: ", JLabel.RIGHT));
    panel.add(slider);
    container.add(panel, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
    setSize(500, 300);
    setVisible(true);
}

From source file:org.jfree.chart.demo.bottom_slider.java

/**
 * Create the panel.//ww  w .ja v  a 2  s .  c o  m
 */

public bottom_slider(Color color, ArrayList<JFreeChart> chart, final Graphic graph, int num) {
    setBounds(5, 380, 740, 100);
    setLayout(null);
    charts = chart;
    plot = charts.get(num).getXYPlot();

    table = new JTable(
            new Object[][] { { "Slider", "X", "Y", "X1-X2", "Y1-Y2" }, { "Slider1", null, null, null, null },
                    { "Slider2", null, null, null, null } },
            new String[] { "Slider", "X", "Y", "X1-X2", "Y1-Y2" });
    setBackground(color);
    table.setBounds(255, 22, 485, 48);
    add(table);

    slider_2 = new JSlider();
    slider_2.setBounds(5, 22, 250, 20);
    slider_2.setBackground(color);
    slider_2.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            try {
                int value = slider_2.getValue();

                ValueAxis domainAxis = plot.getDomainAxis();
                Range range = domainAxis.getRange();

                X1 = domainAxis.getLowerBound() + (value / 100.0) * range.getLength();

                int i = 0;
                for (; i < graph.mas_x.size(); i++) {
                    //   System.out.println("X"+  Arrays.toString(graph.mas_x.toArray()));
                    if (graph.mas_x.get(i).doubleValue() <= X1) {
                    } else {
                        break;
                    }
                }
                Y1 = graph.mas_y.get(i).doubleValue();
            } catch (Exception e1) {
                if (e.toString() == "java.lang.IndexOutOfBoundsException")
                    Y1 = graph.mas_y.get(graph.mas_y.size() - 1).doubleValue();
            }
            plot.removeDomainMarker(distanceTiers2);
            distanceTiers2 = new ValueMarker(X1);
            distanceTiers2.setPaint(Color.BLACK);
            plot.addDomainMarker(distanceTiers2);

            table.setValueAt("" + new BigDecimal(X1).setScale(3, RoundingMode.UP).doubleValue(), 1, 1);
            table.setValueAt("" + new BigDecimal(Y1).setScale(3, RoundingMode.UP).doubleValue(), 1, 2);
            table.setValueAt("" + new BigDecimal(X2).setScale(3, RoundingMode.UP).doubleValue(), 2, 1);
            table.setValueAt("" + new BigDecimal(Y2).setScale(3, RoundingMode.UP).doubleValue(), 2, 2);

            table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(),
                    2, 3);
            table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(),
                    1, 3);
            table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(),
                    2, 4);
            table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(),
                    1, 4);
        }

    });
    add(slider_2);

    slider_3 = new JSlider();
    slider_3.setBounds(5, 55, 250, 20);
    slider_3.setBackground(color);
    slider_3.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            try {
                int value = slider_3.getValue();

                ValueAxis domainAxis = plot.getDomainAxis();
                Range range = domainAxis.getRange();

                X2 = domainAxis.getLowerBound() + (value / 100.0) * range.getLength();

                plot.removeDomainMarker(distanceTiers1);
                distanceTiers1 = new ValueMarker(X2);
                distanceTiers1.setPaint(Color.BLACK);

                plot.addDomainMarker(distanceTiers1);
                int i = 0;
                for (; i < graph.mas_x.size(); i++) {

                    if (graph.mas_x.get(i).doubleValue() <= X2) {
                    } else {
                        break;
                    }

                }

                Y2 = graph.mas_y.get(i).doubleValue();

            } catch (Exception e1) {
                if (e.toString() == "java.lang.IndexOutOfBoundsException")
                    Y1 = graph.mas_y.get(graph.mas_y.size() - 1).doubleValue();
            }
            table.setValueAt("" + new BigDecimal(X1).setScale(3, RoundingMode.UP).doubleValue(), 1, 1);
            table.setValueAt("" + new BigDecimal(Y1).setScale(3, RoundingMode.UP).doubleValue(), 1, 2);
            table.setValueAt("" + new BigDecimal(X2).setScale(3, RoundingMode.UP).doubleValue(), 2, 1);
            table.setValueAt("" + new BigDecimal(Y2).setScale(3, RoundingMode.UP).doubleValue(), 2, 2);

            table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(),
                    2, 3);
            table.setValueAt("" + new BigDecimal(Math.abs(X2 - X1)).setScale(3, RoundingMode.UP).doubleValue(),
                    1, 3);
            table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(),
                    2, 4);
            table.setValueAt("" + new BigDecimal(Math.abs(Y2 - Y1)).setScale(3, RoundingMode.UP).doubleValue(),
                    1, 4);
        }
    });
    add(slider_3);
}

From source file:SliderTest.java

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());
        }// www .  ja v  a2  s.c om
    };

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