Example usage for javax.swing JButton JButton

List of usage examples for javax.swing JButton JButton

Introduction

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

Prototype

public JButton(Action a) 

Source Link

Document

Creates a button where properties are taken from the Action supplied.

Usage

From source file:MnemonicSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Mnemonics");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container content = frame.getContentPane();
    content.setLayout(new GridLayout(1, 0));

    JButton button1 = new JButton("Warning");
    button1.setMnemonic(KeyEvent.VK_W);
    content.add(button1);/*from w  w  w. j a  v a 2  s .  c om*/

    JButton button2 = new JButton("Warning");
    button2.setMnemonic(KeyEvent.VK_H);
    content.add(button2);

    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final JPopupMenu menu = new JPopupMenu();

    JMenuItem item = new JMenuItem("Item Label");
    //  item.addActionListener(actionListener);
    menu.add(item);//w w  w.  j a  va2s . c o m

    JButton component = new JButton("button");
    component.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }

        public void mouseReleased(MouseEvent evt) {
            if (evt.isPopupTrigger()) {
                menu.show(evt.getComponent(), evt.getX(), evt.getY());
            }
        }
    });

}

From source file:SamplePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Sample Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            Object response = JOptionPane.showInputDialog(source, "Where would you like to go to lunch?",
                    "Select a Destination", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "B");
            System.out.println("Response: " + response);
        }/*from  ww w.j a va2s.c o  m*/
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:SamplePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Sample Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            Object response = JOptionPane.showInputDialog(source, "Where would you like to go to lunch?",
                    "Select a Destination", JOptionPane.QUESTION_MESSAGE, null,
                    new String[] { "A", "B", "C", "D", "E" }, "McDonalds");
            System.out.println("Response: " + response);
        }//from   w w  w. j  a v a  2 s  .  co  m
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:VerticalSplit.java

public static void main(String args[]) {
    JFrame vFrame = new JFrame("Vertical Split");
    vFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JComponent leftButton = new JButton("Left");
    JComponent rightButton = new JButton("Right");
    final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    splitPane.setOneTouchExpandable(true);
    splitPane.setLeftComponent(leftButton);
    splitPane.setRightComponent(rightButton);
    ActionListener oneActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            splitPane.resetToPreferredSizes();
        }// w w w  . j a v  a2s .c  o m
    };
    ((JButton) rightButton).addActionListener(oneActionListener);
    ActionListener anotherActionListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            splitPane.setDividerLocation(10);
            splitPane.setContinuousLayout(true);
        }
    };
    ((JButton) leftButton).addActionListener(anotherActionListener);
    vFrame.getContentPane().add(splitPane, BorderLayout.CENTER);
    vFrame.setSize(300, 150);
    vFrame.setVisible(true);

}

From source file:ButtonModel.java

public static void main(String[] args) {
    final JButton ok = new JButton("ok");

    JCheckBox cb = new JCheckBox("Enabled", true);

    ok.setBounds(40, 30, 80, 25);/*  ww  w  .j a va  2s  .  co m*/
    ok.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            DefaultButtonModel model = (DefaultButtonModel) ok.getModel();
            if (model.isEnabled())
                System.out.println("Enabled: true");
            else
                System.out.println("Enabled: false");

            if (model.isArmed())
                System.out.println("Armed: true");
            else
                System.out.println("Armed: false");

            if (model.isPressed())
                System.out.println("Pressed: true");
            else
                System.out.println("Pressed: false");
        }

    });

    cb.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            if (ok.isEnabled())
                ok.setEnabled(false);
            else
                ok.setEnabled(true);
        }
    });

    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.add(ok);
    f.add(cb);
    f.setSize(350, 250);
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:AlignY.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Y Alignment");
    JPanel contentPane = (JPanel) frame.getContentPane();

    BoxLayout layout = new BoxLayout(contentPane, BoxLayout.X_AXIS);
    contentPane.setLayout(layout);/*from w ww.  j  a v  a 2  s  . c  o m*/

    JButton button = new JButton("0.0");
    button.setAlignmentY(Component.TOP_ALIGNMENT);
    contentPane.add(button);
    button = new JButton("1.0");
    button.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    contentPane.add(button);
    button = new JButton("0.5");
    button.setAlignmentY(Component.CENTER_ALIGNMENT);
    contentPane.add(button);

    frame.setSize(200, 100);
    frame.show();
}

From source file:NoButtonPopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("NoButton Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Ask");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Component source = (Component) actionEvent.getSource();
            int response = JOptionPane.showOptionDialog(source, "", "Empty?", JOptionPane.DEFAULT_OPTION,
                    JOptionPane.QUESTION_MESSAGE, null, new Object[] {}, null);
            System.out.println("Response: " + response);
        }// w w w.  j a v  a  2  s.c o m
    };
    button.addActionListener(actionListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JList<String> list = new JList<String>(new String[] { "one", "two", "three", "four", "five" });
    JScrollPane scrollPane = new JScrollPane(list);
    JButton btn = new JButton(new AbstractAction() {
        {/*from   w w w  .j  a v  a2 s.  c o m*/
            putValue(NAME, "Select");
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            list.setSelectedIndex(0);
        }
    });
    JPanel panel = new JPanel();
    panel.add(scrollPane);
    panel.add(btn);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:ColorSamplePopup.java

public static void main(String args[]) {

    JFrame frame = new JFrame("JColorChooser Sample Popup");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JButton button = new JButton("Pick to Change Background");

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Color initialBackground = button.getBackground();
            Color background = JColorChooser.showDialog(null, "Change Button Background", initialBackground);
            if (background != null) {
                button.setBackground(background);
            }//from w  ww  .  j ava  2  s .c  o m
        }
    };
    button.addActionListener(actionListener);
    frame.add(button, BorderLayout.CENTER);

    frame.setSize(300, 100);
    frame.setVisible(true);
}