Example usage for javax.swing JButton addActionListener

List of usage examples for javax.swing JButton addActionListener

Introduction

In this page you can find the example usage for javax.swing JButton addActionListener.

Prototype

public void addActionListener(ActionListener l) 

Source Link

Document

Adds an ActionListener to the button.

Usage

From source file:ScrollDemo2.java

public void init() {
    JRadioButton form[][] = new JRadioButton[12][5];
    String counts[] = { "", "0-1", "2-5", "6-10", "11-100", "101+" };
    String categories[] = { "Household", "Office", "Extended Family", "Company (US)", "Company (World)", "Team",
            "Will", "Birthday Card List", "High School", "Country", "Continent", "Planet" };
    JPanel p = new JPanel();
    p.setSize(600, 400);/*from  ww  w .  j ava 2s. c  o m*/
    p.setLayout(new GridLayout(13, 6, 10, 0));
    for (int row = 0; row < 13; row++) {
        ButtonGroup bg = new ButtonGroup();
        for (int col = 0; col < 6; col++) {
            if (row == 0) {
                p.add(new JLabel(counts[col]));
            } else {
                if (col == 0) {
                    p.add(new JLabel(categories[row - 1]));
                } else {
                    form[row - 1][col - 1] = new JRadioButton();
                    bg.add(form[row - 1][col - 1]);
                    p.add(form[row - 1][col - 1]);
                }
            }
        }
    }
    scrollpane = new JScrollPane(p);

    // Add in some JViewports for the column and row headers
    JViewport jv1 = new JViewport();
    jv1.setView(new JLabel(new ImageIcon("columnlabel.gif")));
    scrollpane.setColumnHeader(jv1);
    JViewport jv2 = new JViewport();
    jv2.setView(new JLabel(new ImageIcon("rowlabel.gif")));
    scrollpane.setRowHeader(jv2);

    // And throw in an information button
    JButton jb1 = new JButton(new ImageIcon("question.gif"));
    jb1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JOptionPane.showMessageDialog(null, "This is an Active Corner!", "Information",
                    JOptionPane.INFORMATION_MESSAGE);
        }
    });
    scrollpane.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, jb1);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
}

From source file:Main.java

public Main() {
    JPanel panel = new JPanel();
    panel.setLayout(null);//from   www.  j a  va 2 s .c om

    model = new DefaultListModel();
    list = new JList(model);
    list.setBounds(150, 30, 220, 150);

    JButton okButton = new JButton("Ok");
    okButton.setBounds(30, 35, 80, 25);

    okButton.addActionListener(this);

    panel.add(okButton);
    panel.add(list);
    add(panel);

    setSize(420, 250);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

}

From source file:components.FileChooserDemo2.java

public FileChooserDemo2() {
    super(new BorderLayout());

    //Create the log first, because the action listener
    //needs to refer to it.
    log = new JTextArea(5, 20);
    log.setMargin(new Insets(5, 5, 5, 5));
    log.setEditable(false);//  w  w  w  .j a  v  a2s  .  co m
    JScrollPane logScrollPane = new JScrollPane(log);

    JButton sendButton = new JButton("Attach...");
    sendButton.addActionListener(this);

    add(sendButton, BorderLayout.PAGE_START);
    add(logScrollPane, BorderLayout.CENTER);
}

From source file:Main.java

public Main(int axis) {
    super(BoxLayout.Y_AXIS);
    container = new Box(axis);
    container.setAlignmentX(Box.LEFT_ALIGNMENT);
    add(container);/*w w  w .  j  a  v a  2 s  . c o m*/

    JTextArea text = new JTextArea();
    container.add(new JScrollPane(text));

    JButton split = new JButton("Split");
    split.setAlignmentX(Box.LEFT_ALIGNMENT);
    split.addActionListener(e -> {
        JTextArea t = new JTextArea();
        container.add(new JScrollPane(t));
        revalidate();
    });
    add(split);

    JButton axisChanger = new JButton("Change Axis");
    axisChanger.setAlignmentX(Box.LEFT_ALIGNMENT);
    axisChanger.addActionListener(e -> {
        Box newContainer;
        if (((BoxLayout) container.getLayout()).getAxis() == BoxLayout.X_AXIS) {
            newContainer = Box.createVerticalBox();
        } else {
            newContainer = Box.createHorizontalBox();
        }
        for (Component c : container.getComponents()) {
            container.remove(c);
            newContainer.add(c);
        }
        remove(container);
        add(newContainer, 0);
        container = newContainer;
        container.setAlignmentX(Box.LEFT_ALIGNMENT);
        revalidate();
    });
    add(axisChanger);

}

From source file:SetValueAtToSetValue.java

public SetValueAtToSetValue() {
    final AbstractTableModel model = new MyModel();
    final JTable table = new JTable(model);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    model.setValueAt(new Integer(1), 0, 0);

    JButton button = new JButton("Increment selected cell");
    getContentPane().add(button, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = table.getSelectedRow();
            int column = table.convertColumnIndexToModel(table.getSelectedColumn());
            int currentValue = ((Integer) model.getValueAt(row, column)).intValue();
            model.setValueAt(new Integer(currentValue + 1), row, column);
        }//w w w .  j a v  a2s.c o m
    });

    pack();
}

From source file:Main.java

public Main() {
    final AbstractTableModel model = new MyModel();
    final JTable table = new JTable(model);
    getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
    model.setValueAt(new Integer(1), 0, 0);

    JButton button = new JButton("Increment selected cell");
    getContentPane().add(button, BorderLayout.SOUTH);
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int row = table.getSelectedRow();
            int column = table.convertColumnIndexToModel(table.getSelectedColumn());
            int currentValue = ((Integer) model.getValueAt(row, column)).intValue();
            model.setValueAt(new Integer(currentValue + 1), row, column);
        }/*  w w w  .  ja  v a 2s  .  c o  m*/
    });

    pack();
}

From source file:Main.java

private JButton makeButton(String caption) {
    JButton b = new JButton(caption);
    b.setActionCommand(caption);/*from   w ww  . j a v a  2  s  .  c  o m*/
    b.addActionListener(this);
    getContentPane().add(b);
    return b;
}

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

public CloneTest1(String s) {
    super(s);/*from   www  .  j  a v a2s  .co m*/
    lastValue = 100D;
    series = new TimeSeries("Random Data");
    TimeSeriesCollection timeseriescollection = new TimeSeriesCollection(series);
    JFreeChart jfreechart = createChart(timeseriescollection);
    JFreeChart jfreechart1 = null;
    try {
        jfreechart1 = (JFreeChart) jfreechart.clone();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    XYPlot xyplot = (XYPlot) jfreechart1.getPlot();
    TimeSeriesCollection timeseriescollection1 = (TimeSeriesCollection) xyplot.getDataset();
    series = timeseriescollection1.getSeries(0);
    ChartPanel chartpanel = new ChartPanel(jfreechart1);
    JButton jbutton = new JButton("Add New Data Item");
    jbutton.setActionCommand("ADD_DATA");
    jbutton.addActionListener(this);
    JPanel jpanel = new JPanel(new BorderLayout());
    jpanel.add(chartpanel);
    jpanel.add(jbutton, "South");
    chartpanel.setPreferredSize(new Dimension(500, 270));
    setContentPane(jpanel);
}

From source file:JTextPaneDemo.java

public JTextPaneDemo() {
    super("JTextPane Demo");

    JScrollPane scrollPane = new JScrollPane(textPane);
    getContentPane().add(scrollPane, BorderLayout.CENTER);

    setEndSelection();/*from   w  w w  .  j  a  va  2 s.co m*/
    textPane.insertIcon(new ImageIcon("java2sLogo.GIF"));
    insertText("\nWebsite for: www.java2s.com \n\n", BOLD_BLACK);

    setEndSelection();
    insertText("                                    ", BLACK);
    setEndSelection();
    insertText("\n      Java            " + "                                    " + "Source\n\n", ITALIC_GRAY);

    insertText(" and Support. \n", BLACK);

    setEndSelection();
    JButton manningButton = new JButton("Load the web site for www.java2s.com");
    manningButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            textPane.setEditable(false);
            try {
                textPane.setPage("http://www.java2s.com");
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    });
    textPane.insertComponent(manningButton);

    setSize(500, 450);
    setVisible(true);
}

From source file:ColorChooserTest.java

public ColorChooserPanel() {
    JButton modalButton = new JButton("Modal");
    modalButton.addActionListener(new ModalListener());
    add(modalButton);//w  w w .j a  v  a 2  s  .  c o  m

    JButton modelessButton = new JButton("Modeless");
    modelessButton.addActionListener(new ModelessListener());
    add(modelessButton);

    JButton immediateButton = new JButton("Immediate");
    immediateButton.addActionListener(new ImmediateListener());
    add(immediateButton);
}