List of usage examples for javax.swing JButton addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setLayout(new FlowLayout()); frame.setVisible(true);/*w w w. jav a 2 s . c o m*/ JButton button1 = new JButton(""); JButton button2 = new JButton(""); frame.getContentPane().add(button1); frame.getContentPane().add(button2); button1.addActionListener(e -> { System.out.println("Lambda"); }); button2.addActionListener(Main::doSomething); }
From source file:org.eclipse.swt.snippets.Snippet337.java
public static void main(String args[]) { display = new Display(); EventQueue.invokeLater(() -> { JFrame mainFrame = new JFrame("Main Window"); mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); mainFrame.addWindowListener(new Snippet337.CloseListener()); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new FlowLayout()); JButton launchBrowserButton = new JButton("Launch Browser"); launchBrowserButton.addActionListener(e -> { JFrame childFrame = new JFrame(); final Canvas canvas = new Canvas(); childFrame.setSize(850, 650); childFrame.getContentPane().add(canvas); childFrame.setVisible(true); display.asyncExec(() -> { Shell shell = SWT_AWT.new_Shell(display, canvas); shell.setSize(800, 600); Browser browser = new Browser(shell, SWT.NONE); browser.setLayoutData(new GridData(GridData.FILL_BOTH)); browser.setSize(800, 600); browser.setUrl("http://www.eclipse.org"); shell.open();//from ww w. j ava 2 s . c om }); }); mainPanel.add(new JTextField("a JTextField")); mainPanel.add(launchBrowserButton); mainFrame.getContentPane().add(mainPanel, BorderLayout.CENTER); mainFrame.pack(); mainFrame.setVisible(true); }); display.addListener(SWT.Close, event -> EventQueue.invokeLater(() -> { Frame[] frames = Frame.getFrames(); for (int i = 0; i < frames.length; i++) { frames[i].dispose(); } })); while (!display.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } }
From source file:Main.java
public static void main(String[] args) { JPanel gui = new JPanel(new BorderLayout(2, 3)); JPanel buttonConstrsint = new JPanel(new FlowLayout(FlowLayout.CENTER)); JButton getQuotesButton = new JButton("Load"); buttonConstrsint.add(getQuotesButton); gui.add(buttonConstrsint, BorderLayout.NORTH); getQuotesButton.addActionListener(e -> { String[] columnNames = { "First Name", "Last Name", "Sport", "# of Years", "Vegetarian" }; Object[][] data = { { "A", "B", "Snowboarding", new Integer(5), new Boolean(false) }, { "C", "D", "Pool", new Integer(10), new Boolean(false) } }; JTable table = new JTable(data, columnNames); JScrollPane scrollPane = new JScrollPane(table); table.setFillsViewportHeight(true); gui.add(scrollPane, BorderLayout.CENTER); gui.revalidate();/*from w w w . jav a 2 s . co m*/ gui.repaint(); }); JOptionPane jOptionPane = new JOptionPane(gui); JDialog dialog = jOptionPane.createDialog(new JFrame(), "title"); dialog.setSize(200, 200); dialog.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JButton close = new JButton("Close me programmatically"); final JFrame f = new JFrame("Close Me"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(close);// w w w .j a v a 2 s . c om close.addActionListener(e -> { f.dispose(); }); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); device.setFullScreenWindow(frame);/*from ww w .j a v a 2 s .c o m*/ device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); frame.addWindowListener(new WindowAdapter() { @Override public void windowIconified(WindowEvent we) { if (programmatic) { programmatic = false; frame.setState(JFrame.NORMAL); } } }); JButton btn = new JButton(); btn.setText("Btn"); final JPanel panel = new JPanel(); panel.add(btn); frame.add(panel); btn.addActionListener(e -> { programmatic = true; JOptionPane.showMessageDialog(panel, "Sample"); }); frame.setVisible(true); }
From source file:CombinationFormatter.java
public static void main(String argv[]) { // a demo main() to show how CombinationFormatter could be used int comb1[] = { 35, 11, 19 }; int comb2[] = { 10, 20, 30 }; final JFormattedTextField field1 = new JFormattedTextField(new CombinationFormatter()); field1.setValue(comb1);/*from www. j a v a 2 s. com*/ final JFormattedTextField field2 = new JFormattedTextField(new CombinationFormatter()); field2.setValue(comb2); JPanel pan = new JPanel(); pan.add(new JLabel("Change the combination from")); pan.add(field1); pan.add(new JLabel("to")); pan.add(field2); JButton b = new JButton("Submit"); b.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent ae) { try { field1.commitEdit(); // make sure current edit (if any) gets // committed field2.commitEdit(); } catch (java.text.ParseException pe) { } int oldc[] = (int[]) field1.getValue(); int newc[] = (int[]) field2.getValue(); // // code to validate oldc[] and change to newc[] goes here // } }); pan.add(b); JFrame f = new JFrame("CombinationFormatter Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(pan); f.setSize(360, 100); f.setVisible(true); }
From source file:ActionButtonSample.java
public static void main(String args[]) { JFrame frame = new JFrame("DefaultButton"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String command = actionEvent.getActionCommand(); System.out.println("Selected: " + command); }//from w w w . j a v a2 s .co m }; Container content = frame.getContentPane(); content.setLayout(new GridLayout(2, 2)); JButton button1 = new JButton("Text Button"); button1.setMnemonic(KeyEvent.VK_B); button1.setActionCommand("First"); button1.addActionListener(actionListener); content.add(button1); Icon warnIcon = new ImageIcon("Warn.gif"); JButton button2 = new JButton(warnIcon); button2.setActionCommand("Second"); button2.addActionListener(actionListener); content.add(button2); JButton button3 = new JButton("Warning", warnIcon); button3.setActionCommand("Third"); button3.addActionListener(actionListener); content.add(button3); String htmlButton = "<html><sup>HTML</sup> <sub><em>Button</em></sub><br>" + "<font color=\"#FF0080\"><u>Multi-line</u></font>"; JButton button4 = new JButton(htmlButton); button4.setActionCommand("Fourth"); button4.addActionListener(actionListener); content.add(button4); JRootPane rootPane = frame.getRootPane(); rootPane.setDefaultButton(button2); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Clip Image"); Container contentPane = frame.getContentPane(); final Clipboard clipboard = frame.getToolkit().getSystemClipboard(); Icon icon = new ImageIcon("jaeger.jpg"); final JLabel label = new JLabel(icon); label.setTransferHandler(new ImageSelection()); JScrollPane pane = new JScrollPane(label); contentPane.add(pane, BorderLayout.CENTER); JButton copy = new JButton("Copy"); copy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { TransferHandler handler = label.getTransferHandler(); handler.exportToClipboard(label, clipboard, TransferHandler.COPY); }//from w w w . j a v a 2s.c o m }); JButton clear = new JButton("Clear"); clear.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { label.setIcon(null); } }); JButton paste = new JButton("Paste"); paste.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Transferable clipData = clipboard.getContents(clipboard); if (clipData != null) { if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) { TransferHandler handler = label.getTransferHandler(); handler.importData(label, clipData); } } } }); JPanel p = new JPanel(); p.add(copy); p.add(clear); p.add(paste); contentPane.add(p, BorderLayout.SOUTH); frame.setSize(300, 300); frame.show(); }
From source file:MainClass.java
public static void main(String[] a) { final int STRING_POSITION = 1; Object buttonColors[][] = { { Color.RED, "RED" }, { Color.BLUE, "BLUE" }, { Color.GREEN, "GREEN" }, { Color.BLACK, "BLACK" }, null, // separator { Color.CYAN, "CYAN" } }; JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ActionListener actionListener = new TheActionListener(); JToolBar toolbar = new JToolBar(); toolbar.setRollover(true);/*from w w w . j ava 2s. com*/ for (Object[] color : buttonColors) { if (color == null) { toolbar.addSeparator(); } else { Icon icon = MetalIconFactory.getTreeComputerIcon(); JButton button = new JButton(icon); button.setActionCommand((String) color[STRING_POSITION]); button.addActionListener(actionListener); toolbar.add(button); } } Action action = new ShowAction(toolbar); JButton button = new JButton(action); toolbar.add(button); Container contentPane = frame.getContentPane(); contentPane.add(toolbar, BorderLayout.NORTH); JTextArea textArea = new JTextArea(); JScrollPane pane = new JScrollPane(textArea); contentPane.add(pane, BorderLayout.CENTER); frame.setSize(350, 150); frame.setVisible(true); }
From source file:com.adito.upgrade.Upgrade.java
/** * @param args//w w w . j a v a 2 s .co m * @throws Exception on any error */ public static void main(String[] args) throws Exception { boolean gui = System.getProperty("os.name").toLowerCase().startsWith("windows") || System.getenv("DISPLAY") != null; if (args.length == 2 || !gui) { Upgrader upgrader = new CommandLineUpgrader(args); upgrader.upgrade(); } else { JFrame f = new JFrame("0.1.16 to 0.2.5+ Upgrader"); final Upgrader upgrader = new GUIUpgrader(); f.setIconImage(new ImageIcon(Upgrade.class.getResource("upgrader-32x32.png")).getImage()); f.getContentPane().setLayout(new BorderLayout()); f.getContentPane().add((JPanel) upgrader, BorderLayout.CENTER); f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); JPanel bp = new JPanel(new FlowLayout(FlowLayout.RIGHT)); final JButton start = new JButton("Start"); ; final JButton close = new JButton("Close"); start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { start.setEnabled(false); close.setEnabled(false); upgrader.upgrade(); } catch (Exception ex) { upgrader.error("Failed to upgrade.", ex); } finally { close.setEnabled(true); } } }); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (close.isEnabled()) System.exit(0); } }); bp.add(start); bp.add(close); f.getContentPane().add(bp, BorderLayout.SOUTH); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt) { if (close.isEnabled()) System.exit(0); } }); f.setSize(new Dimension(480, 460)); UIUtil.positionComponent(SwingConstants.CENTER, f); f.setVisible(true); } }