List of usage examples for java.awt.event ActionListener ActionListener
ActionListener
From source file:TextSlider.java
public static void main(String args[]) { JFrame f = new JFrame("Text Slider"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final TextSlider ts = new TextSlider(); ts.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Text: " + ts.getText()); }// w w w . ja v a 2 s . c o m }); Container c = f.getContentPane(); c.add(ts, BorderLayout.NORTH); f.setSize(300, 200); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel entreePanel = new JPanel(); final ButtonGroup entreeGroup = new ButtonGroup(); JRadioButton radioButton;//from w w w . ja v a 2 s . c o m entreePanel.add(radioButton = new JRadioButton("A")); radioButton.setActionCommand("A"); entreeGroup.add(radioButton); entreePanel.add(radioButton = new JRadioButton("B")); radioButton.setActionCommand("B"); entreeGroup.add(radioButton); entreePanel.add(radioButton = new JRadioButton("C", true)); radioButton.setActionCommand("C"); entreeGroup.add(radioButton); final JPanel condimentsPanel = new JPanel(); condimentsPanel.add(new JCheckBox("Ketchup")); condimentsPanel.add(new JCheckBox("Mustard")); condimentsPanel.add(new JCheckBox("Pickles")); JPanel orderPanel = new JPanel(); JButton orderButton = new JButton("Place Order"); orderPanel.add(orderButton); frame.setLayout(new GridLayout(3, 1)); frame.add(entreePanel); frame.add(condimentsPanel); frame.add(orderPanel); orderButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { String entree = entreeGroup.getSelection().getActionCommand(); System.out.println(entree + " sandwich"); Component[] components = condimentsPanel.getComponents(); for (Component c : components) { JCheckBox cb = (JCheckBox) c; if (cb.isSelected()) System.out.println("With " + cb.getText()); } } }); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300, 150); frame.setVisible(true); }
From source file:EditabilityExample.java
public static void main(String[] args) { try {//from ww w. j a va 2 s . c o m UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception evt) { } JFrame f = new JFrame("Editability Example"); f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS)); f.getContentPane().add(firstField); JTextField tf = new JTextField("A read-only text field", 20); tf.setEditable(false); f.getContentPane().add(tf); JTextArea ta = new JTextArea("An editable\ntext area", 2, 20); ta.setBorder(BorderFactory.createLoweredBevelBorder()); f.getContentPane().add(ta); ta = new JTextArea("A read-only\ntext area", 2, 20); ta.setBorder(BorderFactory.createLoweredBevelBorder()); ta.setEditable(false); f.getContentPane().add(ta); f.pack(); f.show(); if (args.length == 1 && args[0].equals("disable")) { // Toggle the enabled state of the first // text field every 10 seconds Timer t = new Timer(10000, new ActionListener() { public void actionPerformed(ActionEvent evt) { firstField.setEnabled(!firstField.isEnabled()); firstField.setText(firstFieldText + (firstField.isEnabled() ? "" : " (disabled)")); } }); t.start(); } }
From source file:FilterSample.java
public static void main(String args[]) { JFrame frame = new JFrame("JFileChooser Filter Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); final JLabel directoryLabel = new JLabel(); contentPane.add(directoryLabel, BorderLayout.NORTH); final JLabel filenameLabel = new JLabel(); contentPane.add(filenameLabel, BorderLayout.SOUTH); final JButton button = new JButton("Open FileChooser"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component parent = (Component) actionEvent.getSource(); JFileChooser fileChooser = new JFileChooser("."); fileChooser.setAccessory(new LabelAccessory(fileChooser)); FileFilter filter1 = new ExtensionFileFilter(null, new String[] { "JPG", "JPEG" }); // fileChooser.setFileFilter(filter1); fileChooser.addChoosableFileFilter(filter1); FileFilter filter2 = new ExtensionFileFilter("gif", new String[] { "gif" }); fileChooser.addChoosableFileFilter(filter2); fileChooser.setFileView(new JavaFileView()); int status = fileChooser.showOpenDialog(parent); if (status == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); directoryLabel.setText(selectedFile.getParent()); filenameLabel.setText(selectedFile.getName()); } else if (status == JFileChooser.CANCEL_OPTION) { directoryLabel.setText(" "); filenameLabel.setText(" "); }/*from w w w .ja v a 2 s.c o m*/ } }; button.addActionListener(actionListener); contentPane.add(button, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:AboutDialog.java
public static void main(String[] args) { JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); JMenu help = new JMenu("Help"); help.setMnemonic(KeyEvent.VK_H); JMenuItem about = new JMenuItem("About"); help.add(about);//from w w w . j a v a 2 s .co m about.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { AboutDialog ad = new AboutDialog(); ad.setVisible(true); } }); menubar.add(file); menubar.add(help); JFrame f = new JFrame(); f.setJMenuBar(menubar); f.setSize(300, 200); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel gui = new JPanel(new BorderLayout(5, 5)); JPanel plafComponents = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3, 3)); plafComponents.setBorder(new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)")); UIManager.LookAndFeelInfo[] plafInfos = UIManager.getInstalledLookAndFeels(); String[] plafNames = new String[plafInfos.length]; for (int ii = 0; ii < plafInfos.length; ii++) { plafNames[ii] = plafInfos[ii].getName(); }/* w w w . j a va 2 s. c o m*/ JComboBox plafChooser = new JComboBox(plafNames); plafComponents.add(plafChooser); JCheckBox pack = new JCheckBox("Pack on PLAF change", true); plafComponents.add(pack); plafChooser.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { int index = plafChooser.getSelectedIndex(); try { UIManager.setLookAndFeel(plafInfos[index].getClassName()); SwingUtilities.updateComponentTreeUI(frame); if (pack.isSelected()) { frame.pack(); frame.setMinimumSize(frame.getSize()); } } catch (Exception e) { e.printStackTrace(); } } }); gui.add(plafComponents, BorderLayout.NORTH); JPanel dynamicLabels = new JPanel(new BorderLayout(4, 4)); dynamicLabels.setBorder(new TitledBorder("BorderLayout(4,4)")); gui.add(dynamicLabels, BorderLayout.WEST); final JPanel labels = new JPanel(new GridLayout(0, 2, 3, 3)); labels.setBorder(new TitledBorder("GridLayout(0,2,3,3)")); JButton addNew = new JButton("Add Another Label"); dynamicLabels.add(addNew, BorderLayout.NORTH); addNew.addActionListener(new ActionListener() { private int labelCount = 0; public void actionPerformed(ActionEvent ae) { labels.add(new JLabel("Label " + ++labelCount)); frame.validate(); } }); dynamicLabels.add(new JScrollPane(labels), BorderLayout.CENTER); String[] header = { "Name", "Value" }; String[] a = new String[0]; String[] names = System.getProperties().stringPropertyNames().toArray(a); String[][] data = new String[names.length][2]; for (int ii = 0; ii < names.length; ii++) { data[ii][0] = names[ii]; data[ii][1] = System.getProperty(names[ii]); } DefaultTableModel model = new DefaultTableModel(data, header); JTable table = new JTable(model); JScrollPane tableScroll = new JScrollPane(table); Dimension tablePreferred = tableScroll.getPreferredSize(); tableScroll.setPreferredSize(new Dimension(tablePreferred.width, tablePreferred.height / 3)); JPanel imagePanel = new JPanel(new GridBagLayout()); JLabel imageLabel = new JLabel("test"); imagePanel.add(imageLabel, null); JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableScroll, new JScrollPane(imagePanel)); gui.add(splitPane, BorderLayout.CENTER); frame.setContentPane(gui); frame.pack(); frame.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);/*from ww w. ja 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:Submenu.java
public static void main(String[] args) { JFrame f = new JFrame(); JMenuBar menubar = new JMenuBar(); ImageIcon iconNew = new ImageIcon("new.png"); ImageIcon iconOpen = new ImageIcon("open.png"); ImageIcon iconSave = new ImageIcon("save.png"); ImageIcon iconClose = new ImageIcon("exit.png"); JMenu file = new JMenu("File"); JMenu imp = new JMenu("Import"); JMenuItem fileNew = new JMenuItem("New", iconNew); JMenuItem fileOpen = new JMenuItem("Open", iconOpen); JMenuItem fileSave = new JMenuItem("Save", iconSave); JMenuItem fileClose = new JMenuItem("Close", iconClose); file.setMnemonic(KeyEvent.VK_F); imp.setMnemonic(KeyEvent.VK_M); JMenuItem newsf = new JMenuItem("Import newsfeed list..."); JMenuItem bookm = new JMenuItem("Import bookmarks..."); JMenuItem mail = new JMenuItem("Import mail..."); imp.add(newsf);/* w w w .j a v a 2s .c o m*/ imp.add(bookm); imp.add(mail); fileNew.setMnemonic(KeyEvent.VK_N); fileNew.setMnemonic(KeyEvent.VK_O); fileSave.setMnemonic(KeyEvent.VK_S); fileClose.setMnemonic(KeyEvent.VK_C); fileClose.setToolTipText("Exit application"); fileClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_W, ActionEvent.CTRL_MASK)); fileClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { System.exit(0); } }); file.add(fileNew); file.add(fileOpen); file.add(fileSave); file.addSeparator(); file.add(imp); file.addSeparator(); file.add(fileClose); menubar.add(file); f.setJMenuBar(menubar); f.setSize(360, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:ProgressBarStep.java
public static void main(String args[]) { // Initialize final JProgressBar aJProgressBar = new JProgressBar(0, 50); aJProgressBar.setStringPainted(true); final JButton aJButton = new JButton("Start"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { aJButton.setEnabled(false);/*from w w w. j a v a2 s. co m*/ Thread stepper = new BarThread(aJProgressBar); stepper.start(); } }; aJButton.addActionListener(actionListener); String title = (args.length == 0 ? "Stepping Progress" : args[0]); JFrame theFrame = new JFrame(title); theFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = theFrame.getContentPane(); contentPane.add(aJProgressBar, BorderLayout.NORTH); contentPane.add(aJButton, BorderLayout.SOUTH); theFrame.setSize(300, 200); theFrame.setVisible(true); }
From source file:SystemTrayTest.java
public static void main(String[] args) { final TrayIcon trayIcon; if (!SystemTray.isSupported()) { System.err.println("System tray is not supported."); return;//from w w w . j ava2s . c om } SystemTray tray = SystemTray.getSystemTray(); Image image = Toolkit.getDefaultToolkit().getImage("cookie.png"); PopupMenu popup = new PopupMenu(); MenuItem exitItem = new MenuItem("Exit"); exitItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); popup.add(exitItem); trayIcon = new TrayIcon(image, "Your Fortune", popup); trayIcon.setImageAutoSize(true); trayIcon.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { trayIcon.displayMessage("How do I turn this off?", "Right-click on the fortune cookie and select Exit.", TrayIcon.MessageType.INFO); } }); try { tray.add(trayIcon); } catch (AWTException e) { System.err.println("TrayIcon could not be added."); return; } final List<String> fortunes = readFortunes(); Timer timer = new Timer(10000, new ActionListener() { public void actionPerformed(ActionEvent e) { int index = (int) (fortunes.size() * Math.random()); trayIcon.displayMessage("Your Fortune", fortunes.get(index), TrayIcon.MessageType.INFO); } }); timer.start(); }