List of usage examples for java.awt.event ActionListener ActionListener
ActionListener
From source file:RevalidateExample.java
public RevalidateExample() { super("Revalidation Demo"); setSize(300, 150);// w ww. j av a 2 s . c o m setDefaultCloseOperation(EXIT_ON_CLOSE); Font font = new Font("Dialog", Font.PLAIN, 10); final JButton b = new JButton("Add"); b.setFont(font); Container c = getContentPane(); c.setLayout(new FlowLayout()); c.add(b); b.addActionListener(new ActionListener() { // Increase the size of the button's font each time it's clicked int size = 20; public void actionPerformed(ActionEvent ev) { b.setFont(new Font("Dialog", Font.PLAIN, ++size)); b.revalidate(); // invalidates the button & validates its root pane } }); }
From source file:DualListener.java
public Main() { MyObject pair = new MyObject("This is 'a'", "This is 'b'"); ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("al: Action " + e); }// w w w .j a va2 s . c o m }; DualListener dual = new DualListener(); pair.addActionListener(al); pair.addChangeListener(dual); pair.setA("new A"); pair.setB("new B"); pair.addActionListener(dual); pair.setA("another A"); pair.removeActionListener(dual); pair.setB("another B"); pair.setA("final A"); }
From source file:HTMLButton.java
public void init() { b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { getContentPane().add(new JLabel("<html>" + "<i><font size=+4>Kapow!")); // Force a re-layout to include the new label: validate();/*from w w w . j a v a 2 s.com*/ } }); Container cp = getContentPane(); cp.setLayout(new FlowLayout()); cp.add(b); }
From source file:ToolbarFrame1.java
public ToolbarFrame1() { super("Toolbar Example (AWT)"); setSize(450, 250);/*from w w w .j a va2 s .c o m*/ addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); ActionListener printListener = new ActionListener() { public void actionPerformed(ActionEvent ae) { System.out.println(ae.getActionCommand()); } }; Panel toolbar = new Panel(); toolbar.setLayout(new FlowLayout(FlowLayout.LEFT)); cutButton = new Button("Cut"); cutButton.addActionListener(printListener); toolbar.add(cutButton); copyButton = new Button("Copy"); copyButton.addActionListener(printListener); toolbar.add(copyButton); pasteButton = new Button("Paste"); pasteButton.addActionListener(printListener); toolbar.add(pasteButton); add(toolbar, BorderLayout.NORTH); }
From source file:Main.java
public Main() { menuBar = new JMenuBar(); JMenu justifyMenu = new JMenu("Justify"); ActionListener actionPrinter = new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Action [" + e.getActionCommand() + "] performed!\n"); }// w ww. j a va 2s. c om }; JCheckBoxMenuItem leftJustify = new JCheckBoxMenuItem("Left", new ImageIcon("1.gif")); leftJustify.setHorizontalTextPosition(JMenuItem.RIGHT); leftJustify .setAccelerator(KeyStroke.getKeyStroke('L', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); leftJustify.addActionListener(actionPrinter); JCheckBoxMenuItem rightJustify = new JCheckBoxMenuItem("Right", new ImageIcon("2.gif")); rightJustify.setHorizontalTextPosition(JMenuItem.RIGHT); rightJustify .setAccelerator(KeyStroke.getKeyStroke('R', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); rightJustify.addActionListener(actionPrinter); JCheckBoxMenuItem centerJustify = new JCheckBoxMenuItem("Center", new ImageIcon("3.gif")); centerJustify.setHorizontalTextPosition(JMenuItem.RIGHT); centerJustify .setAccelerator(KeyStroke.getKeyStroke('M', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); centerJustify.addActionListener(actionPrinter); JCheckBoxMenuItem fullJustify = new JCheckBoxMenuItem("Full", new ImageIcon("4.gif")); fullJustify.setHorizontalTextPosition(JMenuItem.RIGHT); fullJustify .setAccelerator(KeyStroke.getKeyStroke('F', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); fullJustify.addActionListener(actionPrinter); justifyMenu.add(leftJustify); justifyMenu.add(rightJustify); justifyMenu.add(centerJustify); justifyMenu.add(fullJustify); menuBar.add(justifyMenu); menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); }
From source file:Main.java
public Main() { super();//from w ww . j a v a 2 s.com setSize(300, 100); progressBar.setMinimum(minValue); progressBar.setMaximum(maxValue); progressBar.setStringPainted(true); JButton start = new JButton("Start"); start.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Thread runner = new Thread() { public void run() { counter = minValue; while (counter <= maxValue) { Runnable runme = new Runnable() { public void run() { progressBar.setValue(counter); } }; SwingUtilities.invokeLater(runme); counter++; try { Thread.sleep(100); } catch (Exception ex) { } } } }; runner.start(); } }); getContentPane().add(progressBar, BorderLayout.CENTER); getContentPane().add(start, BorderLayout.WEST); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); }
From source file:Main.java
public Main() throws Exception { URL imageURL = getClass().getClassLoader().getResource("images/k.jpeg"); buttonIcon = new ImageIcon(imageURL); button = new JButton("Click to Buh!", buttonIcon); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (buhClip != null) { buhClip.setFramePosition(0); buhClip.start();//www .j a va2s. c o m } else System.out.println("Couldn't load sound"); } }); getContentPane().add(button); URL soundURL = getClass().getClassLoader().getResource("sounds/b.aiff"); Line.Info linfo = new Line.Info(Clip.class); Line line = AudioSystem.getLine(linfo); buhClip = (Clip) line; AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL); buhClip.open(ais); }
From source file:Main.java
public Main() { made_list.setModel(new DefaultListModel()); for (String element : elements) { ((DefaultListModel) made_list.getModel()).addElement(element); }/*from w w w . j a va2s .c o m*/ JButton removeItemBtn = new JButton("Remove Item"); removeItemBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { removeActionPerformed(e); } }); JPanel panel = new JPanel(); panel.add(new JScrollPane(made_list)); panel.add(removeItemBtn); JOptionPane.showMessageDialog(null, panel); }
From source file:ColorChooserDemo.java
protected void createUI() { setSize(400, 400);/*from w w w. j a v a2 s . c om*/ getContentPane().setLayout(new GridBagLayout()); JButton colorButton = new JButton("Choose a color..."); getContentPane().add(colorButton); colorButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { Color c = JColorChooser.showDialog(ColorChooserDemo.this, "Choose a color...", getBackground()); if (c != null) getContentPane().setBackground(c); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { System.exit(0); } }); }
From source file:Main.java
public Main() { JPanel inputPanel = new JPanel(new BorderLayout(3, 3)); go.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { URL pageURL = new URL("http://www.google.com"); HttpURLConnection urlConnection = (HttpURLConnection) pageURL.openConnection(); int respCode = urlConnection.getResponseCode(); String response = urlConnection.getResponseMessage(); codeArea.setText("HTTP/1.x " + respCode + " " + response + "\n"); int count = 1; while (true) { String header = urlConnection.getHeaderField(count); String key = urlConnection.getHeaderFieldKey(count); if (header == null || key == null) { break; }/*from w w w . j a va2 s.c om*/ codeArea.append(urlConnection.getHeaderFieldKey(count) + ": " + header + "\n"); count++; } InputStream in = new BufferedInputStream(urlConnection.getInputStream()); Reader r = new InputStreamReader(in); int c; while ((c = r.read()) != -1) { codeArea.append(String.valueOf((char) c)); } codeArea.setCaretPosition(1); } catch (Exception ee) { } } }); inputPanel.add(BorderLayout.EAST, go); JScrollPane codeScroller = new JScrollPane(codeArea); add(BorderLayout.NORTH, inputPanel); add(BorderLayout.CENTER, codeScroller); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(700, 500); this.setVisible(true); }