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 {
    // Retrieve the icon
    Icon icon = new ImageIcon("icon.gif");

    // Create an action with an icon
    Action action = new AbstractAction("Button Label", icon) {
        // This method is called when the button is pressed
        public void actionPerformed(ActionEvent evt) {
            // Perform action
        }// w  ww . j a  va2  s .  c om
    };

    // Create the button; the icon will appear to the left of the label
    JButton button = new JButton(action);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridBagLayout());
    for (int i = 0; i < 25; i++) {
        JTextField field = new JTextField("Field " + i, 20);
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.gridy = i;//from  w ww  .j a  v  a 2s.  com
        panel.add(field, constraints);
    }
    JScrollPane scrollPane = new JScrollPane(panel);
    JButton removeButton = new JButton("Remove Field");
    removeButton.addActionListener(e -> {
        if (panel.getComponentCount() >= 1) {
            panel.remove(panel.getComponentCount() - 1);
            scrollPane.revalidate();
            scrollPane.repaint();
        }
    });
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(640, 480);
    f.setLocation(200, 200);
    f.getContentPane().add(scrollPane);
    f.getContentPane().add(removeButton, BorderLayout.SOUTH);
    f.setVisible(true);
}

From source file:FlowLayoutSettingGaps.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Flow Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flow = new FlowLayout(); // Create a layout manager
    flow.setHgap(35); // Set the horizontal gap
    Container content = aWindow.getContentPane(); // Get the content pane
    content.setLayout(flow); // Set the container layout mgr
    // Now add six button components
    for (int i = 1; i <= 6; i++) {
        content.add(new JButton("Press " + i)); // Add a Button to content pane
    }//from   www.  j a v  a2  s .c  o  m
    aWindow.setVisible(true); // Display the window
}

From source file:Main.java

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

    JTextPane editorPane = new JTextPane();
    editorPane.setSelectedTextColor(Color.red);

    // set content as html
    // editorPane.setContentType("text/html");
    editorPane.setText("<p color='#FF0000'>Cool!</p>");

    // added <u></u> to underlone button
    JButton label = new JButton("button");

    label.setAlignmentY(0.85f);//from w w  w  . ja  v a  2s  .  co m

    label.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseReleased(MouseEvent e) {

            if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
                JOptionPane.showMessageDialog(null, "Hello!");
            }
        }
    });

    editorPane.insertComponent(label);
    frame.getContentPane().add(editorPane);
    frame.pack();
    frame.setVisible(true);
}

From source file:JScrollPaneHeadersandCorners.java

public static void main(String args[]) {
    final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" },

    };/*from www.ja v  a  2 s  .com*/
    final Object headers[] = { "English", "#" };

    JFrame frame = new JFrame("Table Printing");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final JTable table = new JTable(rows, headers);
    JScrollPane scrollPane = new JScrollPane(table);
    frame.add(scrollPane, BorderLayout.CENTER);
    scrollPane.setCorner(JScrollPane.UPPER_RIGHT_CORNER, new JButton("..."));

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

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame(Main.class.getSimpleName());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel btmPanel = new JPanel(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.weighty = 1.0;//from   www  .j a  va 2s .c o m
    gbc.weightx = 1.0;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.insets = new Insets(5, 5, 5, 5);
    gbc.anchor = GridBagConstraints.NORTH;
    JButton comp = new JButton("Panel-1");
    btmPanel.add(comp, gbc);
    JButton comp2 = new JButton("Panel-2");
    btmPanel.add(comp2, gbc);
    JButton comp3 = new JButton("Panel-3");
    comp3.setPreferredSize(new Dimension(comp.getPreferredSize().width, comp.getPreferredSize().height + 10));
    btmPanel.add(comp3, gbc);
    frame.add(btmPanel);
    frame.pack();
    frame.setVisible(true);
}

From source file:ACompoundBorder.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Compound Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border lineBorder = LineBorder.createBlackLineBorder();
    Border bevelBorder = BorderFactory.createRaisedBevelBorder();
    Border bevelLineBorder = new CompoundBorder(bevelBorder, lineBorder);
    JButton bevelLineButton = new JButton("Bevel Line");
    bevelLineButton.setBorder(bevelLineBorder);
    Border redBorder = BorderFactory.createLineBorder(Color.MAGENTA, 2);
    Border orangeBorder = BorderFactory.createLineBorder(Color.BLUE, 2);
    Border yellowBorder = BorderFactory.createLineBorder(Color.YELLOW, 5);
    Border greenBorder = BorderFactory.createLineBorder(Color.GREEN, 2);
    Border blueBorder = BorderFactory.createLineBorder(Color.ORANGE, 4);
    Border magentaBorder = BorderFactory.createLineBorder(Color.RED, 3);
    Border twoColorBorder = new CompoundBorder(magentaBorder, blueBorder);
    Border threeColorBorder = new CompoundBorder(twoColorBorder, greenBorder);
    Border fourColorBorder = new CompoundBorder(threeColorBorder, yellowBorder);
    Border fiveColorBorder = new CompoundBorder(fourColorBorder, orangeBorder);
    Border sixColorBorder = new CompoundBorder(fiveColorBorder, redBorder);
    JButton rainbowButton = new JButton("Rainbow");
    rainbowButton.setBorder(sixColorBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(bevelLineButton);//from  w w  w. ja  va  2 s  .  c  o  m
    contentPane.add(rainbowButton);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("LabelFor Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel("Username");
    JTextField textField = new JTextField();
    label.setDisplayedMnemonic(KeyEvent.VK_U);
    label.setLabelFor(textField);//from w ww.  ja v a2  s .co m
    Container box = Box.createHorizontalBox();
    box.add(label);
    box.add(textField);
    frame.add(box, BorderLayout.NORTH);
    frame.add(new JButton("Submit"), BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:CardLayoutTest.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Card Layout");
    final Container contentPane = frame.getContentPane();
    final CardLayout layout = new CardLayout();
    contentPane.setLayout(layout);/*from ww w.  ja  v a 2 s.c o m*/
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            layout.next(contentPane);
        }
    };
    for (int i = 0; i < 5; i++) {
        String label = "Card " + i;
        JButton button = new JButton(label);
        contentPane.add(button, label);
        button.addActionListener(listener);
    }
    frame.setSize(300, 200);
    frame.show();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    final ConfirmDialog dialog = new ConfirmDialog(f);
    final JTree tree = new JTree();
    tree.setVisibleRowCount(5);/*from   w ww . j ava 2s. co m*/
    final JScrollPane treeScroll = new JScrollPane(tree);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton b = new JButton("Choose Tree Item");
    b.addActionListener(e -> {
        int result = dialog.showConfirmDialog(treeScroll, "Choose an item");
        if (result == ConfirmDialog.OK_OPTION) {
            System.out.println(tree.getSelectionPath());
        } else {
            System.out.println("User cancelled");
        }
    });
    JPanel p = new JPanel(new BorderLayout());
    p.add(b);
    p.setBorder(new EmptyBorder(50, 50, 50, 50));
    f.setContentPane(p);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);

}