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

public static void main(String[] argv) throws Exception {
    InputMap inputMap = new InputMap();

    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");

    JButton component = new JButton("button");

    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);

    System.out.println(inputMap.get(KeyStroke.getKeyStroke("F2")));

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InputMap inputMap = new InputMap();

    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");

    JButton component = new JButton("button");

    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);

    System.out.println(inputMap.keys().length);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InputMap inputMap = new InputMap();

    inputMap.put(KeyStroke.getKeyStroke("F2"), "actionName");

    JButton component = new JButton("button");

    inputMap.setParent(component.getInputMap(JComponent.WHEN_FOCUSED));
    component.setInputMap(JComponent.WHEN_FOCUSED, inputMap);

    inputMap.remove(KeyStroke.getKeyStroke("F2"));

}

From source file:CreatingSerifItalicBoldFont.java

public static void main(String args[]) {
    JFrame f = new JFrame("JColorChooser Sample");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JButton button = new JButton("Pick to Change Background");

    Font myFont = new Font("Serif", Font.ITALIC | Font.BOLD, 12);

    button.setFont(myFont);//from  w ww.j a v a 2  s  . c  o m

    f.add(button, BorderLayout.CENTER);
    f.setSize(300, 200);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {

    JFrame frame = new JFrame();
    frame.setLayout(new FlowLayout());
    frame.setVisible(true);/*from   w  ww  .  j av a  2s  .  c o  m*/

    JButton button1 = new JButton("");
    JButton button2 = new JButton("");

    frame.getContentPane().add(button1);
    frame.getContentPane().add(button2);

    button1.addActionListener(e -> {
        System.out.println("Lambda");
    });

    button2.addActionListener(Main::doSomething);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel(new GridLayout(10, 10, 10, 10));

    for (int i = 0; i < 100; i++) {
        panel.add(new JButton(String.valueOf(i)));
    }//w w w.j  a  v  a2 s  .  c om

    frame.add(panel);

    frame.setSize(600, 600);
    frame.setVisible(true);
}

From source file:GettingSettingSelectedItem.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton jButton1 = new JButton("Button");

    String[] mystring = { "Java", "JBuilder", "JFC", "Swing" };
    final JList jList1 = new JList(mystring);
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Object contents = jList1.getSelectedValue();
            System.out.println(contents);
        }//  w ww  .j  a  va2  s  .  co m
    });

    frame.add(jList1, "Center");
    frame.add(jButton1, "South");

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

From source file:Main.java

public static void main(String[] args) {
    JButton btnA = new JButton("A");
    JButton btnB = new JButton("B");

    btnA.setPreferredSize(new Dimension(50, 25));
    btnB.setPreferredSize(new Dimension(100, 25));

    JPanel btnAPanel = new JPanel();
    JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    btnAPanel.add(btnA);/* w ww  .  j  a  v a  2 s .com*/
    btnBPanel.add(btnB);

    JPanel topPanel = new JPanel(new GridLayout(1, 0));
    topPanel.add(new JLabel("hi"));
    topPanel.add(btnAPanel);
    topPanel.add(btnBPanel);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.NORTH);

    mainPanel.setPreferredSize(new Dimension(400, 300));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(mainPanel);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    AbstractButton jb = new JButton("Press Me");

    jb.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ev) {
            System.out.println("ItemEvent!");
        }//from   ww  w.  java  2  s .c  om
    });

    jb.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent ev) {
            System.out.println("ChangeEvent!");
        }
    });

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(jb);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JButton button = new JButton("Show Input Dialog Box");
    button.addActionListener(e -> {//from   www  . j  a  v  a 2 s .  co m
        JTextField xField = new JTextField(5);
        JTextField yField = new JTextField(5);
        JPanel myPanel = new JPanel();
        myPanel.add(new JLabel("x:"));
        myPanel.add(xField);
        myPanel.add(Box.createHorizontalStrut(15));
        myPanel.add(new JLabel("y:"));
        myPanel.add(yField);

        int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values",
                JOptionPane.OK_CANCEL_OPTION);
        if (result == JOptionPane.OK_OPTION) {
            System.out.println("x value: " + xField.getText());
            System.out.println("y value: " + yField.getText());
        }
    });
    JPanel panel = new JPanel();
    panel.add(button);
    frame.add(panel);
    frame.setSize(400, 400);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}