List of usage examples for javax.swing JButton addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. From source file:OffsetSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Offset Example"); Container content = frame.getContentPane(); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel("Name: "); label.setDisplayedMnemonic(KeyEvent.VK_N); final JTextField textField = new JTextField(); label.setLabelFor(textField);//from w ww . j ava2s. c o m panel.add(label, BorderLayout.WEST); panel.add(textField, BorderLayout.CENTER); content.add(panel, BorderLayout.NORTH); JButton button = new JButton("Get Offset"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Offset: " + textField.getScrollOffset()); System.out.println("Visibility: " + textField.getHorizontalVisibility()); BoundedRangeModel model = textField.getHorizontalVisibility(); int extent = model.getExtent(); textField.setScrollOffset(extent); } }; button.addActionListener(actionListener); content.add(button, BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); }
From source file:Command.java
public static void main(String[] args) throws IOException { javax.swing.JFrame f = new javax.swing.JFrame("Command Test"); javax.swing.JButton b1 = new javax.swing.JButton("Tick"); javax.swing.JButton b2 = new javax.swing.JButton("Tock"); javax.swing.JLabel label = new javax.swing.JLabel("Hello world"); java.awt.Container pane = f.getContentPane(); pane.add(b1, java.awt.BorderLayout.WEST); pane.add(b2, java.awt.BorderLayout.EAST); pane.add(label, java.awt.BorderLayout.NORTH); b1.addActionListener(Command.parse(label, "setText(\"tick\");")); b2.addActionListener(Command.parse(label, "setText(\"tock\");")); f.pack();/*from w w w .ja va2 s . com*/ f.show(); }
From source file:ElementSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Element Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); JButton button = new JButton("Show Elements"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Document document = textArea.getDocument(); ElementIterator iterator = new ElementIterator(document); Element element = iterator.first(); while (element != null) { System.out.println(element.getStartOffset()); element = iterator.next(); }//from w w w . j a va2s. c om } }; button.addActionListener(actionListener); frame.add(scrollPane, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
From source file:ElementSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Element Example"); Container content = frame.getContentPane(); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); JButton button = new JButton("Show Elements"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Document document = textArea.getDocument(); ElementIterator iterator = new ElementIterator(document); Element element = iterator.first(); while (element != null) { System.out.println(element.getStartOffset()); element = iterator.next(); }// w ww.j a v a 2 s .c om } }; button.addActionListener(actionListener); content.add(scrollPane, BorderLayout.CENTER); content.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Element Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); JButton button = new JButton("Show Elements"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Document document = textArea.getDocument(); ElementIterator iterator = new ElementIterator(document); Element element = iterator.first(); while (element != null) { System.out.println(element.getStartOffset()); element = iterator.next(); }//from ww w. j av a 2s. c om } }; button.addActionListener(actionListener); frame.add(scrollPane, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
From source file:OptionPaneSample.java
public static void main(String args[]) { JFrame f = new JFrame("JOptionPane Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component source = (Component) actionEvent.getSource(); Object response = JOptionPane.showInputDialog(source, "Where do you want to go tomorrow?", "JOptionPane Sample", JOptionPane.QUESTION_MESSAGE, null, new String[] { "A", "B", "C", "D", "E" }, "E"); System.out.println("Response: " + response); }/*from w w w.ja v a 2 s .c o m*/ }; button.addActionListener(actionListener); content.add(button, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); }
From source file:SamplePopup.java
public static void main(String args[]) { JFrame frame = new JFrame("Sample Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component source = (Component) actionEvent.getSource(); Object response = JOptionPane.showInputDialog(source, "Where would you like to go to lunch?", "Select a Destination", JOptionPane.QUESTION_MESSAGE, null, new String[] { "A", "B", "C", "D", "E" }, "B"); System.out.println("Response: " + response); }/* w w w. j ava 2s .co m*/ }; button.addActionListener(actionListener); Container contentPane = frame.getContentPane(); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame("CardLayout"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JPanel buttonPanel = new JPanel(); JButton nextButton = new JButton("Next"); buttonPanel.add(nextButton);/* w w w. j av a 2s. c o m*/ contentPane.add(buttonPanel, BorderLayout.SOUTH); final JPanel cardPanel = new JPanel(); final CardLayout cardLayout = new CardLayout(); cardPanel.setLayout(cardLayout); for (int i = 1; i <= 5; i++) { JButton card = new JButton("Card " + i); card.setPreferredSize(new Dimension(200, 200)); String cardName = "card" + 1; cardPanel.add(card, cardName); } contentPane.add(cardPanel, BorderLayout.CENTER); nextButton.addActionListener(e -> cardLayout.next(cardPanel)); frame.pack(); frame.setVisible(true); }
From source file:SamplePopup.java
public static void main(String args[]) { JFrame frame = new JFrame("Sample Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component source = (Component) actionEvent.getSource(); Object response = JOptionPane.showInputDialog(source, "Where would you like to go to lunch?", "Select a Destination", JOptionPane.QUESTION_MESSAGE, null, new String[] { "A", "B", "C", "D", "E" }, "McDonalds"); System.out.println("Response: " + response); }/*from w w w . java2 s.c om*/ }; button.addActionListener(actionListener); Container contentPane = frame.getContentPane(); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:PassiveTextField.java
public static void main(String[] args) { try {// w w w . j a v a 2s. c om UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } JFrame f = new JFrame("Passive Text Field"); f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS)); final PassiveTextField ptf = new PassiveTextField(32); JTextField tf = new JTextField(32); JPanel p = new JPanel(); JButton b = new JButton("OK"); p.add(b); f.getContentPane().add(ptf); f.getContentPane().add(tf); f.getContentPane().add(p); ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Action event from a text field"); } }; ptf.addActionListener(l); tf.addActionListener(l); // Make the button the default button f.getRootPane().setDefaultButton(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Content of text field: <" + ptf.getText() + ">"); } }); f.pack(); f.setVisible(true); }