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 frame = new JFrame("GridLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new GridLayout(3, 0));

    for (int i = 1; i <= 9; i++) {
        buttonPanel.add(new JButton("Button  " + i));
    }/*from   w w  w  . jav a2  s  . co m*/

    contentPane.add(buttonPanel, BorderLayout.CENTER);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Active Example");
    UIManager.put("LabelFactory", new ActiveLabel());
    final JPanel panel = new JPanel();
    JButton button = new JButton("Get");
    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            JLabel label = (JLabel) UIManager.get("LabelFactory");
            panel.add(label);//w  w w. ja  va2 s  .c  o  m
            panel.revalidate();
        }
    };
    button.addActionListener(actionListener);

    Container contentPane = frame.getContentPane();
    contentPane.add(panel, BorderLayout.CENTER);
    contentPane.add(button, BorderLayout.SOUTH);
    frame.setSize(200, 200);
    frame.setVisible(true);
}

From source file:ASoftBevelBorder.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Soft Bevel Borders");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Border raisedBorder = new SoftBevelBorder(SoftBevelBorder.RAISED);
    Border loweredBorder = new SoftBevelBorder(SoftBevelBorder.LOWERED);
    JButton raisedButton = new JButton("Raised");
    raisedButton.setBorder(raisedBorder);
    JButton loweredButton = new JButton("Lowered");
    loweredButton.setBorder(loweredBorder);
    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(raisedButton);//from  w w  w.  ja va2s  .com
    contentPane.add(loweredButton);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JFileChooser fc = new JFileChooser();
    fc.setMultiSelectionEnabled(true);//from w  ww . j a v  a2 s  . com
    fc.setCurrentDirectory(new File("C:\\tmp"));
    JButton btn1 = new JButton("Show Dialog");
    btn1.addActionListener(e -> fc.showDialog(frame, "Choose"));
    JButton btn2 = new JButton("Show Open Dialog");
    btn2.addActionListener(e -> {
        int retVal = fc.showOpenDialog(frame);
        if (retVal == JFileChooser.APPROVE_OPTION) {
            File[] selectedfiles = fc.getSelectedFiles();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < selectedfiles.length; i++) {
                sb.append(selectedfiles[i].getName());
                sb.append("\n");
            }
            JOptionPane.showMessageDialog(frame, sb.toString());
        }
    });
    JButton btn3 = new JButton("Show Save Dialog");
    btn3.addActionListener(e -> fc.showSaveDialog(frame));
    Container pane = frame.getContentPane();
    pane.setLayout(new GridLayout(3, 1, 10, 10));
    pane.add(btn1);
    pane.add(btn2);
    pane.add(btn3);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

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

    MyModel myArrayClass = new MyModel(25);

    MyView panel = new MyView(myArrayClass);
    myArrayClass.myView = panel;//from  w w  w  . j a v a 2  s. co  m

    JButton sortButton = new JButton("Sort");
    sortButton.addActionListener(e -> performSorting(myArrayClass));
    f.getContentPane().add(sortButton, BorderLayout.NORTH);
    f.getContentPane().add(panel, BorderLayout.CENTER);
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame();
    frame.setUndecorated(true);/*ww  w.  j a va 2 s .c  o m*/
    JButton button = new JButton("Close Me");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });

    frame.addMouseListener(new MouseAdapter() {
        public void mousePressed(MouseEvent e) {
            point.x = e.getX();
            point.y = e.getY();
        }
    });
    frame.addMouseMotionListener(new MouseMotionAdapter() {
        public void mouseDragged(MouseEvent e) {
            Point p = frame.getLocation();
            frame.setLocation(p.x + e.getX() - point.x, p.y + e.getY() - point.y);
        }
    });

    frame.setSize(300, 300);
    frame.setLocation(200, 200);
    frame.setLayout(new BorderLayout());

    frame.getContentPane().add(button, BorderLayout.NORTH);
    frame.getContentPane().add(new JLabel("Drag Me", JLabel.CENTER), BorderLayout.CENTER);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    final JFrame frame = new JFrame();
    Container contentPane = frame.getContentPane();
    JButton b = new JButton("Hide for 5");
    ActionListener action = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            frame.setVisible(false);/* w  w w .j a v  a 2  s. c o  m*/
            TimerTask task = new TimerTask() {
                public void run() {
                    frame.setVisible(true);
                }
            };
            Timer timer = new Timer();
            timer.schedule(task, 5000);
        }
    };
    b.addActionListener(action);
    AncestorListener ancestor = new AncestorListener() {
        public void ancestorAdded(AncestorEvent e) {
            System.out.println("Added");
            dumpInfo(e);
        }

        public void ancestorMoved(AncestorEvent e) {
            System.out.println("Moved");
            dumpInfo(e);
        }

        public void ancestorRemoved(AncestorEvent e) {
            System.out.println("Removed");
            dumpInfo(e);
        }

        private void dumpInfo(AncestorEvent e) {
            System.out.println("\tAncestor: " + name(e.getAncestor()));
            System.out.println("\tAncestorParent: " + name(e.getAncestorParent()));
            System.out.println("\tComponent: " + name(e.getComponent()));
        }

        private String name(Container c) {
            return (c == null) ? null : c.getName();
        }
    };
    b.addAncestorListener(ancestor);
    contentPane.add(b, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:ASoftBevelBorder.java

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

    Border raisedBorder = new SoftBevelBorder(SoftBevelBorder.RAISED);
    Border loweredBorder = new SoftBevelBorder(SoftBevelBorder.LOWERED);
    JButton raisedButton = new JButton("Raised");
    raisedButton.setBorder(raisedBorder);

    JButton loweredButton = new JButton("Lowered");
    loweredButton.setBorder(loweredBorder);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new GridLayout(1, 2));
    contentPane.add(raisedButton);//from  w  ww  . j a v a  2  s  . c  om
    contentPane.add(loweredButton);
    frame.setSize(300, 100);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    DefaultTableModel model;/*from  ww  w . j a v a  2s.  co m*/
    JTable t = new JTable(model = new DefaultTableModel(0, 1));
    for (int i = 0; i < 10; i++) {
        model.addRow(new Object[] { i });
    }
    JButton removeSelected = new JButton("remove");
    removeSelected.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            int[] selectedRows = t.getSelectedRows();
            for (int i = selectedRows.length - 1; i >= 0; i--) {
                model.removeRow(selectedRows[i]);
                ;
            }
        }
    });
    JFrame f = new JFrame();
    f.add(new JScrollPane(t));
    f.add(removeSelected, BorderLayout.SOUTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

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

    JButton loadButton = new JButton("Display Image");
    loadButton.addActionListener(ev -> {
        JFileChooser fc = new JFileChooser(System.getProperty("user.home"));
        fc.addChoosableFileFilter(/*w  w w.  j  a  va2  s  .c o m*/
                new FileNameExtensionFilter("Image files", new String[] { "png", "jpg", "jpeg", "gif" }));
        if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
            try {
                Image image = ImageIO.read(fc.getSelectedFile());
                if (image != null) {
                    JPanel panel = new JPanel(new BorderLayout(10, 10));
                    panel.add(new JLabel(fc.getSelectedFile().toString()), BorderLayout.NORTH);
                    panel.add(new JLabel(new ImageIcon(image)));
                    JOptionPane.showMessageDialog(frame, panel);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    frame.add(loadButton);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}