List of usage examples for javax.swing JButton addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. From source file:CustomTreeCellRenderer.java
public static void main(String[] args) { ImageIcon iconWhite = new ImageIcon("white.jpg"); ImageIcon iconBlack = new ImageIcon("black.jpg"); ;/* www . j av a 2 s .c om*/ JFrame frame = new JFrame(); frame.setContentPane(new JPanel(new BorderLayout())); JTree tree = new JTree(); frame.getContentPane().add(tree); CustomTreeCellRenderer renderer = new CustomTreeCellRenderer(); renderer.setRendererIcon(iconWhite); tree.setCellRenderer(renderer); JPanel panelButtons = new JPanel(); JButton buttonWhite = new JButton(""); buttonWhite.setIcon(iconWhite); JButton buttonBlack = new JButton(""); buttonBlack.setIcon(iconBlack); buttonBlack.addActionListener(e -> { renderer.setRendererIcon(iconBlack); tree.repaint(); }); panelButtons.add(buttonBlack); panelButtons.add(buttonWhite); frame.getContentPane().add(panelButtons, BorderLayout.SOUTH); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(800, 600);//from w ww . j a v a 2 s. c o m frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); device.setFullScreenWindow(frame); device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); frame.setVisible(true); JButton btn = new JButton(); btn.setText("Button"); JPanel panel = new JPanel(); panel.add(btn); frame.add(panel); btn.addActionListener(e -> { JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane"); }); }
From source file:ChangeLook.java
public static void main(String args[]) { final JFrame frame = new JFrame("Change Look"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String lafClassName = null; lafClassName = actionEvent.getActionCommand(); String finalLafClassName = lafClassName; try { UIManager.setLookAndFeel(finalLafClassName); SwingUtilities.updateComponentTreeUI(frame); } catch (Exception exception) { JOptionPane.showMessageDialog(frame, "Can't change look and feel", "Invalid PLAF", JOptionPane.ERROR_MESSAGE); }/* w w w. j av a2 s . c o m*/ } }; UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels(); JComboBox comboBox = new JComboBox(new String[] { "a", "b" }); JPanel panel = new JPanel(); for (int i = 0, n = looks.length; i < n; i++) { JButton button = new JButton(looks[i].getName()); button.setActionCommand(looks[i].getClassName()); button.addActionListener(actionListener); panel.add(button); } frame.add(comboBox, BorderLayout.NORTH); frame.add(panel, BorderLayout.SOUTH); frame.setSize(350, 150); frame.setVisible(true); }
From source file:Toolbars.java
public static void main(String[] args) { JToolBar toolbar1 = new JToolBar(); JToolBar toolbar2 = new JToolBar(); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); JButton newb = new JButton(new ImageIcon("new.png")); JButton openb = new JButton(new ImageIcon("open.png")); JButton saveb = new JButton(new ImageIcon("save.png")); toolbar1.add(newb);/*from w w w .ja va 2s.c o m*/ toolbar1.add(openb); toolbar1.add(saveb); toolbar1.setAlignmentX(0); JButton exitb = new JButton(new ImageIcon("exit.png")); toolbar2.add(exitb); toolbar2.setAlignmentX(0); exitb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); panel.add(toolbar1); panel.add(toolbar2); JFrame f = new JFrame(); f.add(panel, BorderLayout.NORTH); f.setSize(300, 200); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:MainClass.java
public static void main(String[] args) { MutableTreeNode root = new DefaultMutableTreeNode("A"); MutableTreeNode beams = new DefaultMutableTreeNode("B"); MutableTreeNode gears = new DefaultMutableTreeNode("C"); root.insert(beams, 0);/* ww w . j a v a 2 s . c o m*/ root.insert(gears, 1); beams.insert(new DefaultMutableTreeNode("4 "), 0); beams.insert(new DefaultMutableTreeNode("6 "), 1); beams.insert(new DefaultMutableTreeNode("8 "), 2); beams.insert(new DefaultMutableTreeNode("12 "), 3); gears.insert(new DefaultMutableTreeNode("8t"), 0); gears.insert(new DefaultMutableTreeNode("24t"), 1); gears.insert(new DefaultMutableTreeNode("40t"), 2); final DefaultTreeModel model = new DefaultTreeModel(root); final JTree tree = new JTree(model); final JTextField nameField = new JTextField("16t"); final JButton button = new JButton("Add a part"); button.setEnabled(false); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TreePath tp = tree.getSelectionPath(); MutableTreeNode insertNode = (MutableTreeNode) tp.getLastPathComponent(); int insertIndex = 0; if (insertNode.getParent() != null) { MutableTreeNode parent = (MutableTreeNode) insertNode.getParent(); insertIndex = parent.getIndex(insertNode) + 1; insertNode = parent; } MutableTreeNode node = new DefaultMutableTreeNode(nameField.getText()); model.insertNodeInto(node, insertNode, insertIndex); } }); JPanel addPanel = new JPanel(new GridLayout(2, 1)); addPanel.add(nameField); addPanel.add(button); tree.addTreeSelectionListener(new TreeSelectionListener() { public void valueChanged(TreeSelectionEvent e) { TreePath tp = e.getNewLeadSelectionPath(); button.setEnabled(tp != null); } }); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); frame.getContentPane().add(new JScrollPane(tree)); frame.getContentPane().add(addPanel, BorderLayout.SOUTH); 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); }//from w ww .j a v a 2s . c o m }); frame.add(jList1, "Center"); frame.add(jButton1, "South"); frame.setSize(300, 200); frame.setVisible(true); }
From source file:PaginationExample.java
public static void main(String args[]) { try {/* w ww . j a va2 s .co m*/ String cn = UIManager.getSystemLookAndFeelClassName(); UIManager.setLookAndFeel(cn); // Use the native L&F } catch (Exception cnf) { } JFrame f = new JFrame("Printing Pagination Example"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); JButton printButton = new JButton("Print Pages"); printButton.addActionListener(new PaginationExample()); f.add("Center", printButton); f.pack(); f.setVisible(true); }
From source file:OverlaySample.java
public static void main(String args[]) { ActionListener generalActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { JComponent comp = (JComponent) actionEvent.getSource(); System.out.println(actionEvent.getActionCommand() + ": " + comp.getBounds()); }// w ww .j ava 2s .co m }; ActionListener sizingActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { setupButtons(actionEvent.getActionCommand()); } }; JFrame frame = new JFrame("Overlay Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(); LayoutManager overlay = new OverlayLayout(panel); panel.setLayout(overlay); Object settings[][] = { { "Small", new Dimension(25, 25), Color.white }, { "Medium", new Dimension(50, 50), Color.gray }, { "Large", new Dimension(100, 100), Color.black } }; JButton buttons[] = { smallButton, mediumButton, largeButton }; for (int i = 0, n = settings.length; i < n; i++) { JButton button = buttons[i]; button.addActionListener(generalActionListener); button.setActionCommand((String) settings[i][0]); button.setMaximumSize((Dimension) settings[i][1]); button.setBackground((Color) settings[i][2]); panel.add(button); } setupButtons(SET_CENTRAL); JPanel actionPanel = new JPanel(); actionPanel.setBorder(BorderFactory.createTitledBorder("Change Alignment")); String actionSettings[] = { SET_MINIMUM, SET_MAXIMUM, SET_CENTRAL, SET_MIXED }; for (int i = 0, n = actionSettings.length; i < n; i++) { JButton button = new JButton(actionSettings[i]); button.addActionListener(sizingActionListener); actionPanel.add(button); } Container contentPane = frame.getContentPane(); contentPane.add(panel, BorderLayout.CENTER); contentPane.add(actionPanel, BorderLayout.SOUTH); frame.setSize(400, 300); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Button"); frame.add(button);/* w w w . j a v a 2s .c o m*/ frame.setAlwaysOnTop(true); frame.setSize(500, 500); frame.setLocation(500, 500); button.addActionListener(e -> { JOptionPane optionPane = new JOptionPane("Option Pane"); optionPane.showMessageDialog(frame, "Message!"); }); frame.setVisible(true); }
From source file:pwr.lab5.Window.java
public static void main(String[] args) { try {//ww w . j a v a2 s . c o m JFrame frame = new JFrame(); Window win = new Window(); // frame.add(pane); frame.add(win); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.pack(); JButton tab[] = new JButton[] { win.jButton1, win.jButton2, win.jButton3, win.jButton4, win.jButton5 }; for (JButton jb : tab) { jb.addActionListener(win); } } catch (Exception e) { JOptionPane.showMessageDialog(null, e.getMessage()); } }