List of usage examples for java.awt.event ActionListener ActionListener
ActionListener
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Offset Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = new JPanel(new BorderLayout()); final JTextField textField = new JTextField(); panel.add(textField, BorderLayout.CENTER); frame.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); }//from www . j a va2 s . c om }; button.addActionListener(actionListener); frame.add(button, BorderLayout.SOUTH); frame.setSize(250, 150); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String[] a) { final JColorChooser colorChooser = new JColorChooser(); final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER); previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48)); previewLabel.setSize(previewLabel.getPreferredSize()); previewLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 1, 0)); colorChooser.setPreviewPanel(previewLabel); ActionListener okActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("OK Button"); System.out.println(colorChooser.getColor()); }/* w w w.j av a 2 s .c o m*/ }; ActionListener cancelActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Cancel Button"); } }; final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser, okActionListener, cancelActionListener); dialog.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(); }/* ww w . ja v a 2 s .co m*/ } }; button.addActionListener(actionListener); content.add(scrollPane, BorderLayout.CENTER); content.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
From source file:ResizeSplit.java
public static void main(String args[]) { String title = "Resize Split"; final JFrame vFrame = new JFrame(title); vFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton topButton = new JButton("Top"); JButton bottomButton = new JButton("Bottom"); final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT); splitPane.setTopComponent(topButton); splitPane.setBottomComponent(bottomButton); ActionListener oneActionListener = new ActionListener() { public void actionPerformed(ActionEvent event) { splitPane.setResizeWeight(1.0); vFrame.setSize(300, 250);/*from w ww . jav a 2 s . c o m*/ vFrame.validate(); } }; bottomButton.addActionListener(oneActionListener); ActionListener anotherActionListener = new ActionListener() { public void actionPerformed(ActionEvent event) { splitPane.setResizeWeight(0.5); vFrame.setSize(300, 250); vFrame.validate(); } }; topButton.addActionListener(anotherActionListener); vFrame.getContentPane().add(splitPane, BorderLayout.CENTER); vFrame.setSize(300, 150); vFrame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); final DrawPad drawPad = new DrawPad(); frame.add(drawPad, BorderLayout.CENTER); JButton clearButton = new JButton("Clear"); clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { drawPad.clear();/*from w w w . j a v a2 s .c o m*/ } }); frame.add(clearButton, BorderLayout.SOUTH); frame.setSize(280, 300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JProgressBar pb = new JProgressBar(); JTextArea ta = new JTextArea(10, 20); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(new JScrollPane(ta)); frame.add(pb, BorderLayout.SOUTH); frame.pack();/*from ww w . ja v a 2s . c o m*/ frame.setVisible(true); Timer timer = new Timer(250, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { index++; if (index >= 100) { ((Timer) (e.getSource())).stop(); } ta.append("Line " + index + "\n"); pb.setValue(index); } }); timer.setRepeats(true); timer.setCoalesce(true); timer.start(); }
From source file:Main.java
public static void main(String args[]) { JFrame frame = new JFrame("Key Text Sample"); KeyTextComponent keyTextComponent = new KeyTextComponent(); final JTextField textField = new JTextField(); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { String keyText = actionEvent.getActionCommand(); textField.setText(keyText);/* ww w.jav a 2s. c o m*/ } }; keyTextComponent.addActionListener(actionListener); frame.add(keyTextComponent, BorderLayout.CENTER); frame.add(textField, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return;//w w w. ja va2 s . co m } SystemTray tray = SystemTray.getSystemTray(); Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage("trayIcon.jpg"); PopupMenu menu = new PopupMenu(); MenuItem messageItem = new MenuItem("Show Message"); messageItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("asdf"); } }); menu.add(messageItem); MenuItem closeItem = new MenuItem("Close"); closeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(closeItem); TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu); icon.setImageAutoSize(true); tray.add(icon); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { if (!SystemTray.isSupported()) { System.out.println("SystemTray is not supported"); return;// w w w . jav a 2 s .com } SystemTray tray = SystemTray.getSystemTray(); Toolkit toolkit = Toolkit.getDefaultToolkit(); Image image = toolkit.getImage("trayIcon.jpg"); PopupMenu menu = new PopupMenu(); MenuItem messageItem = new MenuItem("Show Message"); messageItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("asdf"); } }); menu.add(messageItem); MenuItem closeItem = new MenuItem("Close"); closeItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); menu.add(closeItem); TrayIcon icon = new TrayIcon(image, "SystemTray Demo", menu); icon.setImageAutoSize(true); icon.setToolTip("tooltip"); tray.add(icon); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Default Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField(); frame.add(textField, BorderLayout.NORTH); JPanel panel = new JPanel(); JButton defaultButton = new JButton("Default Button"); defaultButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand() + " selected"); }/*from w ww.ja v a 2 s . c om*/ }); panel.add(defaultButton); JButton otherButton = new JButton("Other Button"); otherButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println(actionEvent.getActionCommand() + " selected"); } }); panel.add(otherButton); frame.add(panel, BorderLayout.SOUTH); Keymap keymap = textField.getKeymap(); KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false); keymap.removeKeyStrokeBinding(keystroke); frame.getRootPane().setDefaultButton(defaultButton); frame.setSize(250, 150); frame.setVisible(true); }