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[] args) {
    JFrame jframe = new JFrame();
    jframe.setSize(500, 200);//  w w w . jav a 2s . co m
    jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JTextPane jTextPane = new JTextPane();
    jTextPane.setEditorKit(new HTMLEditorKit());
    JButton btn = new JButton("Print");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                jTextPane.setContentType("text/html");
                boolean done = jTextPane.print();
                if (done) {
                    System.out.println("Printing is done");
                } else {
                    System.out.println("Error while printing");
                }
            } catch (Exception pex) {
                pex.printStackTrace();
            }
        }
    });
    jframe.add(btn, BorderLayout.SOUTH);
    jframe.add(jTextPane);
    jframe.setVisible(true);
}

From source file:Main.java

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

    // Set the content pane's layout as SpringLayout
    SpringLayout springLayout = new SpringLayout();
    contentPane.setLayout(springLayout);

    // Add two JButtons to the content pane
    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Little Bigger   Button   2");
    contentPane.add(b1);//from  www.ja v a  2s .  co  m
    contentPane.add(b2);

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

From source file:GridBagLayoutColumnSpanHORIZONTAL.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;/*from   w ww .ja  va2s  .  c o m*/
    gbc.gridy = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, first column"), gbc);
    pane.add(new JButton("Second row"), gbc);
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    pane.add(new JButton("Third row, spans two columns"), gbc);
    gbc.gridwidth = 1;
    gbc.fill = GridBagConstraints.NONE;
    gbc.gridx = GridBagConstraints.RELATIVE;
    pane.add(new JButton("First row, second column"), gbc);
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    final Action action = new AbstractAction("Action Name") {
        public void actionPerformed(ActionEvent evt) {
            System.out.println("action");
        }//from   ww w .ja  v a 2  s  . c om
    };

    JFrame frame = new JFrame();
    JButton button = new JButton(action);

    JTextField textfield = new JTextField();
    textfield.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("F2"),
            action.getValue(Action.NAME));
    textfield.getActionMap().put(action.getValue(Action.NAME), action);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    Icon icon = new ImageIcon("java2s.gif");
    JButton b = new JButton(icon);
    JScrollPane pane = new JScrollPane(b);
    AdjustmentListener hListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Horizontal: ");
            dumpInfo(e);/*from w  ww  .ja va  2 s.  c o m*/
        }
    };
    JScrollBar hBar = pane.getHorizontalScrollBar();
    hBar.addAdjustmentListener(hListener);
    AdjustmentListener vListener = new AdjustmentListener() {
        public void adjustmentValueChanged(AdjustmentEvent e) {
            System.out.println("Vertical: ");
            dumpInfo(e);
        }
    };
    JScrollBar vBar = pane.getVerticalScrollBar();
    vBar.addAdjustmentListener(vListener);
    contentPane.add(pane, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:SelectingButton.java

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

    ActionListener aListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            boolean selected = aButton.getModel().isSelected();
            System.out.println("Action - selected=" + selected + "\n");
        }//from w ww .j a v  a 2  s .co  m
    };

    ChangeListener cListener = new ChangeListener() {
        public void stateChanged(ChangeEvent event) {
            AbstractButton aButton = (AbstractButton) event.getSource();
            ButtonModel aModel = aButton.getModel();
            boolean armed = aModel.isArmed();
            boolean pressed = aModel.isPressed();
            boolean selected = aModel.isSelected();
            System.out.println("Changed: " + armed + "/" + pressed + "/" + selected);
        }
    };

    theButton.addActionListener(aListener);
    theButton.addChangeListener(cListener);
    Container contentPane = frame.getContentPane();
    contentPane.add(theButton, BorderLayout.CENTER);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    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 ww  .j  av  a 2  s  .  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);

    HierarchyListener hierarchyListener = new HierarchyListener() {
        public void hierarchyChanged(HierarchyEvent e) {
            long flags = e.getChangeFlags();
            if ((flags & HierarchyEvent.SHOWING_CHANGED) == HierarchyEvent.SHOWING_CHANGED) {
                splitPane.setDividerLocation(.75);
            }
        }
    };
    splitPane.addHierarchyListener(hierarchyListener);

    vFrame.add(splitPane, BorderLayout.CENTER);
    vFrame.setSize(300, 150);
    vFrame.setVisible(true);
}

From source file:Main.java

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

    Border myRaisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.PINK, Color.RED,
            Color.PINK, Color.RED);
    JButton myRaisedButton = new JButton("My Raised");
    myRaisedButton.setBorder(myRaisedBorder);

    Container contentPane = frame.getContentPane();
    contentPane.add(myRaisedButton);/* w ww.  j  av a 2s . co m*/
    frame.setSize(300, 200);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    String[][] data = { { "Data", "Data" } };
    String[] col = { "Col", "Col" };
    final DefaultTableModel model = new DefaultTableModel(data, col);
    JTable table = new JTable(model);
    JButton addRow = new JButton("Add Empty Row");
    addRow.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            model.addRow(new Object[] {});
        }/*from w ww .j  a  v a 2 s .  co  m*/
    });
    JPanel panel = new JPanel(new BorderLayout());
    panel.add(new JScrollPane(table));
    panel.add(addRow, BorderLayout.SOUTH);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:TryGridLayout.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Grid Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout grid = new GridLayout(3, 4, 30, 20);
    Container content = aWindow.getContentPane();
    content.setLayout(grid);/*from w w w  .  ja  va2  s .com*/
    JButton button = null;
    for (int i = 1; i <= 10; i++) {
        content.add(button = new JButton(" Press " + i));
    }
    aWindow.pack();
    aWindow.setVisible(true);
}