List of usage examples for java.awt.event ActionListener ActionListener
ActionListener
From source file:ContainerTest.java
public static void main(String args[]) { JFrame frame = new JFrame(); Container contentPane = frame.getContentPane(); ContainerListener cont = new ContainerListener() { ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Selected: " + e.getActionCommand()); }//w ww .j a va2 s. c o m }; public void componentAdded(ContainerEvent e) { Component c = e.getChild(); if (c instanceof JButton) { JButton b = (JButton) c; b.addActionListener(listener); } } public void componentRemoved(ContainerEvent e) { Component c = e.getChild(); if (c instanceof JButton) { JButton b = (JButton) c; b.removeActionListener(listener); } } }; contentPane.addContainerListener(cont); contentPane.setLayout(new GridLayout(3, 2)); contentPane.add(new JButton("First")); contentPane.add(new JButton("Second")); contentPane.add(new JButton("Third")); contentPane.add(new JButton("Fourth")); contentPane.add(new JButton("Fifth")); frame.setSize(300, 200); frame.show(); }
From source file:ColorChooserSample.java
public static void main(String args[]) { JFrame f = new JFrame("JColorChooser Sample"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container content = f.getContentPane(); final JButton button = new JButton("Pick to Change Background"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color initialBackground = button.getBackground(); Color background = JColorChooser.showDialog(null, "JColorChooser Sample", initialBackground); if (background != null) { button.setBackground(background); }/*from ww w . jav a2 s. c o m*/ } }; button.addActionListener(actionListener); content.add(button, BorderLayout.CENTER); f.setSize(300, 200); f.setVisible(true); }
From source file:Main.java
public static void main(String[] a) throws Exception { if (SystemTray.isSupported()) { SystemTray tray = SystemTray.getSystemTray(); trayIcon.setImageAutoSize(true); trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("In here"); trayIcon.displayMessage("Tester!", "Some action performed", TrayIcon.MessageType.INFO); }/*from w w w . j a v a2 s .com*/ }); try { tray.add(trayIcon); } catch (AWTException e) { System.err.println("TrayIcon could not be added."); } } }
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);/*from www . j a v a2 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:Main.java
public static void main(String[] args) { final Timer timer; final JProgressBar progressBar = new JProgressBar(); final JButton button = new JButton("Start"); JFrame f = new JFrame(); f.setLayout(new FlowLayout()); f.add(progressBar);/* w w w . ja v a2s .c o m*/ f.add(button); ActionListener updateProBar = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int val = progressBar.getValue(); if (val >= 100) { // timer.stop(); button.setText("End"); return; } progressBar.setValue(++val); } }; timer = new Timer(50, updateProBar); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (timer.isRunning()) { timer.stop(); button.setText("Start"); } else if (button.getText() != "End") { timer.start(); button.setText("Stop"); } } }); f.pack(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setResizable(false); f.setLocationRelativeTo(null); f.setVisible(true); }
From source file:Main.java
public static void main(final String args[]) { final String labels[] = { "A", "B", "C", "D", "E" }; JFrame frame = new JFrame("Selecting JComboBox"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox comboBox = new JComboBox(labels); frame.add(comboBox, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Command: " + actionEvent.getActionCommand()); ItemSelectable is = (ItemSelectable) actionEvent.getSource(); System.out.println(", Selected: " + selectedString(is)); System.out.println(", Selected: " + new Date(actionEvent.getWhen())); }/*from w w w. j a va 2 s .c om*/ }; comboBox.addActionListener(actionListener); frame.setSize(400, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame jframe = new JFrame(); jframe.setSize(500, 200);/*w w w .ja v a2 s. c o m*/ jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextPane jTextPane = new JTextPane(); jTextPane.setEditorKit(new HTMLEditorKit()); JButton btn = new JButton("Print"); btn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { jTextPane.setContentType("text/html"); boolean done = jTextPane.print(); if (done) { System.out.println("Printing is done"); } else { System.out.println("Error while printing"); } } catch (Exception pex) { pex.printStackTrace(); } } }); jframe.add(btn, BorderLayout.SOUTH); jframe.add(jTextPane); jframe.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { final JFrame frame = new JFrame(); frame.setUndecorated(true);/*from w w w . j a va2s .co 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);/*from ww w . j av a2 s.c om*/ 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:Main.java
public static void main(String args[]) { DefaultTableModel model;// w ww. j av a 2 s .c om 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); }