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 final void main(String args[]) throws Exception {
    JFrame f = new JFrame();
    DefaultListModel<String> dlm = new DefaultListModel<String>();
    JList<String> list = new JList<>(dlm);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.add(new JScrollPane(list));
    f.add(new JButton("Add") {
        {//www  .j  a  v a 2  s. co  m
            addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    dlm.addElement("A");
                }
            });
        }
    }, BorderLayout.SOUTH);

    f.pack();
    f.setVisible(true);
}

From source file:JFileChooserTest.java

public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JComboBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Select File");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                System.out.println(selectedFile.getName());
            }//from  ww w  . j ava  2  s  .  com
        }
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
}

From source file:GridBagButtons.java

public static void main(final String args[]) {
    final JFrame frame = new JFrame("GridBagLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridBagLayout());
    JButton button;//from  w  w  w . ja v a  2s .c o m
    // Row One - Three Buttons
    button = new JButton("One");
    addComponent(frame, button, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Two");
    addComponent(frame, button, 1, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Three");
    addComponent(frame, button, 2, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    // Row Two - Two Buttons
    button = new JButton("Four");
    addComponent(frame, button, 0, 1, 2, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Five");
    addComponent(frame, button, 2, 1, 1, 2, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    // Row Three - Two Buttons
    button = new JButton("Six");
    addComponent(frame, button, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    button = new JButton("Seven");
    addComponent(frame, button, 1, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
    frame.setSize(500, 200);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("BoxLayout  Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JPanel hPanel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(hPanel, BoxLayout.X_AXIS);
    hPanel.setLayout(boxLayout);/*  ww  w  . java2 s.  c o m*/

    for (int i = 1; i <= 5; i++) {
        hPanel.add(new JButton("Button  " + i));
    }

    contentPane.add(hPanel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    SpinnerNumberModel model = new SpinnerNumberModel(0.0, -1000.0, 1000.0, 0.1);
    JSpinner s = new JSpinner(model);
    JSpinner.NumberEditor editor = new JSpinner.NumberEditor(s);
    s.setEditor(editor);//w w  w  .jav a2s  .c o  m
    JTextField stepText = new JTextField(10);
    JButton bStepSet = new JButton("Set Step");
    bStepSet.addActionListener(e -> {
        Double val = Double.parseDouble(stepText.getText().trim());
        model.setStepSize(val);
    });
    JFrame f = new JFrame();
    Container c = f.getContentPane();
    c.add(s);
    JPanel southPanel = new JPanel();
    southPanel.add(stepText);
    southPanel.add(bStepSet);
    c.add(southPanel, BorderLayout.SOUTH);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:TwoButtons.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel basic = new JPanel();
    basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
    f.add(basic);/*w ww.  j  a v a 2s .  c  o  m*/

    basic.add(Box.createVerticalGlue());

    JPanel bottom = new JPanel();
    bottom.setAlignmentX(1f);
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));

    JButton ok = new JButton("OK");
    JButton close = new JButton("Close");

    bottom.add(ok);
    bottom.add(Box.createRigidArea(new Dimension(5, 0)));
    bottom.add(close);
    bottom.add(Box.createRigidArea(new Dimension(15, 0)));

    basic.add(bottom);
    basic.add(Box.createRigidArea(new Dimension(0, 15)));

    f.setSize(300, 250);

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:ALineBorder.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Line Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border thinBorder = LineBorder.createBlackLineBorder();
    Border thickBorder = new LineBorder(Color.white, 12);
    Border roundedBorder = new LineBorder(Color.black, 12, true);
    JButton thinButton = new JButton("1 Pixel");
    thinButton.setBorder(thinBorder);/*from  ww w  .j  a  v  a2 s .c  o m*/
    JButton thickButton = new JButton("12 Pixel");
    thickButton.setBorder(thickBorder);
    JButton roundedButton = new JButton("Rounded 12 Pixel");
    roundedButton.setBorder(roundedBorder);
    Container contentPane = frame.getContentPane();
    contentPane.add(thinButton, BorderLayout.NORTH);
    contentPane.add(thickButton, BorderLayout.CENTER);
    contentPane.add(roundedButton, BorderLayout.SOUTH);
    frame.pack();
    frame.setSize(300, frame.getHeight());
    frame.setVisible(true);
}

From source file:FlowLayoutChangingGap.java

public static void main(String[] args) {

    JFrame aWindow = new JFrame("This is a Flow Layout");
    aWindow.setBounds(50, 50, 500, 500);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 20, 30);
    Container content = aWindow.getContentPane(); // Get the content pane
    content.setLayout(flow); // Set the container layout mgr
    for (int i = 1; i <= 6; i++) {
        content.add(new JButton("Press " + i)); // Add a Button to content pane
    }//from   w  ww  .  java2 s  .c  o  m
    aWindow.setVisible(true); // Display the window
}

From source file:MainClass.java

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

    frame.setLayout(new GridLayout(3, 3));

    for (int i = 9; i > 0; i--) {
        JButton button = new JButton(Integer.toString(i));
        frame.add(button, 0);/*from w  w w.jav a  2 s .  c  o m*/
    }

    final Container contentPane = frame.getContentPane();
    Comparator<Component> comp = new Comparator<Component>() {
        public int compare(Component c1, Component c2) {
            Component comps[] = contentPane.getComponents();
            List list = Arrays.asList(comps);
            int first = list.indexOf(c1);
            int second = list.indexOf(c2);
            return second - first;
        }
    };

    FocusTraversalPolicy policy = new SortingFocusTraversalPolicy(comp);
    frame.setFocusTraversalPolicy(policy);

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

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout());
    gui.setPreferredSize(new Dimension(400, 100));

    JDesktopPane dtp = new JDesktopPane();
    gui.add(dtp, BorderLayout.CENTER);

    JButton newFrame = new JButton("Add Frame");

    ActionListener listener = new ActionListener() {
        private int disp = 10;

        @Override//from  ww w  .  j  a  v a2 s.c  om
        public void actionPerformed(ActionEvent e) {
            JInternalFrame jif = new JInternalFrame();
            dtp.add(jif);
            jif.setLocation(disp, disp);
            jif.setSize(100, 100);
            disp += 10;
            jif.setVisible(true);
        }
    };
    newFrame.addActionListener(listener);

    gui.add(newFrame, BorderLayout.PAGE_START);

    JFrame f = new JFrame();
    f.add(gui);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLocationByPlatform(true);

    f.pack();
    f.setVisible(true);
}