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:JDK6TabbedPaneExample.java

public void add() {
    final JPanel content = new JPanel();
    JPanel tab = new JPanel();
    tab.setOpaque(false);//  w  w  w .j a va 2 s . c  om

    JLabel tabLabel = new JLabel("Tab " + (++tabCounter));

    JButton tabCloseButton = new JButton(closeXIcon);
    tabCloseButton.setPreferredSize(closeButtonSize);
    tabCloseButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            int closeTabNumber = tabbedPane.indexOfComponent(content);
            tabbedPane.removeTabAt(closeTabNumber);
        }
    });

    tab.add(tabLabel, BorderLayout.WEST);
    tab.add(tabCloseButton, BorderLayout.EAST);

    tabbedPane.addTab(null, content);
    tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, tab);
}

From source file:MainClass.java

public MainClass() {
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(350, 200);//from   w  w w  . j  a v  a2s.co  m

    JTable table = new JTable(qtm);
    JScrollPane scrollpane = new JScrollPane(table);
    JPanel commandPanel = new JPanel();
    commandPanel.setLayout(new GridLayout(3, 2));
    commandPanel.add(new JLabel("Enter the Host URL: "));
    commandPanel.add(hostField = new JTextField());
    commandPanel.add(new JLabel("Enter your query: "));
    commandPanel.add(queryField = new JTextField());
    commandPanel.add(new JLabel("Click here to send: "));

    JButton jb = new JButton("Search");
    jb.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            qtm.setHostURL(hostField.getText().trim());
            qtm.setQuery(queryField.getText().trim());
        }
    });
    commandPanel.add(jb);
    getContentPane().add(commandPanel, BorderLayout.NORTH);
    getContentPane().add(scrollpane, BorderLayout.CENTER);
}

From source file:SwingThreadTest.java

public SwingThreadFrame() {
    setTitle("SwingThreadTest");

    final JComboBox combo = new JComboBox();
    combo.insertItemAt(Integer.MAX_VALUE, 0);
    combo.setPrototypeDisplayValue(combo.getItemAt(0));
    combo.setSelectedIndex(0);/*from   w w  w .j av a2s  .co  m*/

    JPanel panel = new JPanel();

    JButton goodButton = new JButton("Good");
    goodButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            new Thread(new GoodWorkerRunnable(combo)).start();
        }
    });
    panel.add(goodButton);
    JButton badButton = new JButton("Bad");
    badButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            new Thread(new BadWorkerRunnable(combo)).start();
        }
    });
    panel.add(badButton);

    panel.add(combo);
    add(panel);
    pack();
}

From source file:JumbledImage.java

public void buildUI() {
    final JumbledImage ji = new JumbledImage(imageSrc);
    add("Center", ji);
    JButton jumbleButton = new JButton("Jumble");
    jumbleButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JButton b = (JButton) e.getSource();
            ji.jumble();/*from  w  w  w.j a  va2 s.  c o m*/
            ji.repaint();
        };
    });
    Dimension jumbleSize = ji.getPreferredSize();
    resize(jumbleSize.width, jumbleSize.height + 40);
    add("South", jumbleButton);
}

From source file:GetApplets.java

private void createGUI() {
    JPanel contentPane = new JPanel(new BorderLayout());
    contentPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.BLACK),
            BorderFactory.createEmptyBorder(10, 10, 10, 10)));
    setContentPane(contentPane);//from   w w  w.jav a2  s.  c  o m

    JButton b = new JButton("Click to call getApplets()");
    b.addActionListener(this);
    add(b, BorderLayout.PAGE_START);

    textArea = new JTextArea(5, 40);
    textArea.setEditable(false);
    JScrollPane scroller = new JScrollPane(textArea);
    add(scroller, BorderLayout.CENTER);
}

From source file:WelcomeApplet.java

 public void init()
{
   EventQueue.invokeLater(new Runnable()
      {//from  w ww  . j a v a  2s.co m
         public void run()
         {
            setLayout(new BorderLayout());

            JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);
            label.setFont(new Font("Serif", Font.BOLD, 18));
            add(label, BorderLayout.CENTER);

            JPanel panel = new JPanel();

            JButton cayButton = new JButton("Cay Horstmann");
            cayButton.addActionListener(makeAction("http://www.horstmann.com"));
            panel.add(cayButton);

            JButton garyButton = new JButton("Gary Cornell");
            garyButton.addActionListener(makeAction("mailto:gary_cornell@apress.com"));
            panel.add(garyButton);

            add(panel, BorderLayout.SOUTH);
         }
      });
}

From source file:com.codecrate.shard.ui.binding.JFileChooserBinding.java

protected JComponent doBindControl() {
    File value = (File) getValue();

    component.setSelectedFile(value);/*from w  w w .  j  av  a 2  s .  com*/
    component.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            String prop = evt.getPropertyName();
            if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
                File file = (File) evt.getNewValue();
                controlValueChanged(file);

                updateLabel(file);
            }
        }
    });

    updateLabel(value);

    JPanel panel = new JPanel();
    panel.add(label);

    JButton browseButton = new JButton("Browse");
    browseButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            component.showDialog(getActiveWindow().getControl(), "Ok");
        }
    });
    panel.add(browseButton);
    return panel;
}

From source file:TextTransferTest.java

public TextTransferFrame() {
    setTitle("TextTransferTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    textArea = new JTextArea();
    add(new JScrollPane(textArea), BorderLayout.CENTER);
    JPanel panel = new JPanel();

    JButton copyButton = new JButton("Copy");
    panel.add(copyButton);//from   w  w  w  .ja v a  2s . co  m
    copyButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            copy();
        }
    });

    JButton pasteButton = new JButton("Paste");
    panel.add(pasteButton);
    pasteButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            paste();
        }
    });

    add(panel, BorderLayout.SOUTH);
}

From source file:net.kamhon.ieagle.swing.table.KTableTestPanel.java

private Component createNorthPanel() {
    FormBuilder builder = new KFormBuilder();

    JButton btnSelect = new JButton("Select Checked");
    btnSelect.addActionListener(new ActionListener() {
        @Override/*from  www .  j av a2  s .co  m*/
        public void actionPerformed(ActionEvent e) {
            int[] selectedRows = table.getSelectedRows();
            int selectedRow = table.getSelectedRow();
            int rowCount = table.getSelectedRowCount();

            String s = "selectedRows = " + CollectionUtil.toLog(Arrays.asList(selectedRows)) + "\n"
                    + "selectedRow = " + selectedRow + "\n" + "rowCount = " + rowCount;

            JideOptionPane.showMessageDialog(null, s);
        }
    });

    JButton btnRow = new JButton("loop row");
    btnRow.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Student student = table.getRecord(0);
            log.debug("student = " + student);

            JideOptionPane.showMessageDialog(createPanel(), "First record is " + student.getName());
        }
    });

    JPanel btnPanel = ButtonBarFactory.buildCenteredBar(btnSelect, btnRow);
    builder.addButtonPanel(btnPanel);

    return builder.getPanel();
}

From source file:TextComponentTest.java

public TextComponentFrame() {
    setTitle("TextComponentTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    final JTextField textField = new JTextField();
    final JPasswordField passwordField = new JPasswordField();

    JPanel northPanel = new JPanel();
    northPanel.setLayout(new GridLayout(2, 2));
    northPanel.add(new JLabel("User name: ", SwingConstants.RIGHT));
    northPanel.add(textField);// ww w . ja  va2s. c  om
    northPanel.add(new JLabel("Password: ", SwingConstants.RIGHT));
    northPanel.add(passwordField);

    add(northPanel, BorderLayout.NORTH);

    final JTextArea textArea = new JTextArea(8, 40);
    JScrollPane scrollPane = new JScrollPane(textArea);

    add(scrollPane, BorderLayout.CENTER);

    // add button to append text into the text area

    JPanel southPanel = new JPanel();

    JButton insertButton = new JButton("Insert");
    southPanel.add(insertButton);
    insertButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            textArea.append("User name: " + textField.getText() + " Password: "
                    + new String(passwordField.getPassword()) + "\n");
        }
    });

    add(southPanel, BorderLayout.SOUTH);

    // add a text area with scroll bars

}