List of usage examples for javax.swing JButton addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. From source file:TextEditFrame.java
public TextEditFrame() { setTitle("TextEditTest"); setSize(500, 300);/*from ww w. j ava2s .c om*/ addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container contentPane = getContentPane(); JPanel panel = new JPanel(); JButton replaceButton = new JButton("Replace"); panel.add(replaceButton); replaceButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { String from = fromField.getText(); int start = textArea.getText().indexOf(from); if (start >= 0 && from.length() > 0) textArea.replaceRange(toField.getText(), start, start + from.length()); } }); panel.add(fromField); panel.add(new JLabel("with")); panel.add(toField); contentPane.add(panel, "South"); contentPane.add(scrollPane, "Center"); }
From source file:Main.java
public Main() { setLayout(new BorderLayout()); JButton button = new JButton("Print"); button.addActionListener(new PrintListener()); booklist = new JList(books); booklist.setCellRenderer(new BookCellRenderer()); booklist.setVisibleRowCount(4);// w ww .j a v a2 s.co m JScrollPane pane = new JScrollPane(booklist); add(pane, BorderLayout.NORTH); add(button, BorderLayout.SOUTH); }
From source file:Main.java
public Main() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TreeNode root = getNodes();//from w ww . j ava 2 s. c om DefaultTreeModel model = new DefaultTreeModel(root); JTree tree = new JTree(model); tree.setRootVisible(false); JButton add = new JButton("add new"); add.addActionListener(e -> { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode == null) { return; } MyObject obj = (MyObject) selectedNode.getUserObject(); MyObject newChild = new MyObject(obj.name + "-" + (obj.childs.size() + 1)); obj.childs.add(newChild); DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(newChild); model.insertNodeInto(newNode, selectedNode, selectedNode.getChildCount()); TreeNode[] nodes = model.getPathToRoot(newNode); TreePath path = new TreePath(nodes); tree.scrollPathToVisible(path); }); JButton print = new JButton("print childs"); print.addActionListener(e -> { DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (selectedNode == null) { return; } MyObject obj = (MyObject) selectedNode.getUserObject(); System.out.println(obj.childs); }); JPanel btns = new JPanel(); btns.add(add); btns.add(print); add(new JScrollPane(tree)); add(btns, BorderLayout.SOUTH); pack(); setVisible(true); }
From source file:SwingListExample.java
public SwingListExample() { setLayout(new BorderLayout()); JButton button = new JButton("Print"); button.addActionListener(new PrintListener()); booklist = new JList(books); booklist.setCellRenderer(new BookCellRenderer()); booklist.setVisibleRowCount(4);//from w w w. jav a2 s. c o m JScrollPane pane = new JScrollPane(booklist); add(pane, BorderLayout.NORTH); add(button, BorderLayout.SOUTH); }
From source file:TryCardLayout.java
public TryCardLayout() { setLayout(card);/*from w w w .j a v a2 s. c o m*/ JButton button; for (int i = 1; i <= 6; i++) { add(button = new JButton(" Press " + i), "Card" + i); // Add a button button.addActionListener(this); // Add listener for button } }
From source file:DialogTest.java
public AboutDialog(JFrame owner) { super(owner, "About DialogTest", true); // add HTML label to center add(new JLabel("<html><h1><i>Core Java</i></h1><hr>By Cay Horstmann and Gary Cornell</html>"), BorderLayout.CENTER); // Ok button closes the dialog JButton ok = new JButton("Ok"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { setVisible(false);//from w w w .j a v a 2s .co m } }); // add Ok button to southern border JPanel panel = new JPanel(); panel.add(ok); add(panel, BorderLayout.SOUTH); setSize(250, 150); }
From source file:MainClass.java
public MainClass() { JButton launchButton = new JButton("Launch!"); getContentPane().add(launchButton, "South"); getContentPane().add(label, "Center"); launchButton .addActionListener((ActionListener) EventHandler.create(ActionListener.class, this, "actionName")); }
From source file:ExecDemoNS.java
/** Constructor - set up strings and things. */ public ExecDemoNS(String prog) { super("ExecDemo: " + prog); String osname = System.getProperty("os.name"); if (osname == null) throw new IllegalArgumentException("no os.name"); if (prog.equals("netscape")) program = // Windows or UNIX only for now, sorry Mac fans (osname.toLowerCase().indexOf("windows") != -1) ? "c:/program files/netscape/communicator/program/netscape.exe" : "/usr/local/netscape/netscape"; else/*from w w w.j av a2 s . co m*/ program = prog; Container cp = getContentPane(); cp.setLayout(new FlowLayout()); JButton b; cp.add(b = new JButton("Exec")); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { runProg(); } }); cp.add(b = new JButton("Wait")); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { doWait(); } }); cp.add(b = new JButton("Exit")); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.exit(0); } }); pack(); }
From source file:Main.java
public Main() { this.setLayout(new FlowLayout()); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton processButton = new JButton("Start"); JButton helloButton = new JButton("Hello"); processButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { MyTask process = new MyTask(); try { process.execute();/*from w w w. ja v a 2s . c om*/ } catch (Exception e) { e.printStackTrace(); } } }); helloButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(null, "Hello There"); } }); this.getContentPane().add(processButton); this.getContentPane().add(helloButton); this.pack(); setVisible(true); }
From source file:ModelJTable.java
public ModelJTable() { super();//from w ww .ja va2 s.c o m model = new DefaultTableModel(); model.addColumn("First Name"); model.addColumn("Last Name"); model.addColumn("Years"); String[] socrates = { "Socrates", "", "469-399 B.C." }; model.addRow(socrates); String[] plato = { "Plato", "", "428-347 B.C." }; model.addRow(plato); String[] aquinas = { "Thomas", "Aquinas", "1225-1274" }; model.addRow(aquinas); String[] kierkegaard = { "Soren", "Kierkegaard", "1813-1855" }; model.addRow(kierkegaard); String[] kant = { "Immanuel", "Kant", "1724-1804" }; model.addRow(kant); String[] nietzsche = { "Friedrich", "Nietzsche", "1844-1900" }; model.addRow(nietzsche); String[] arendt = { "Hannah", "Arendt", "1906-1975" }; model.addRow(arendt); table = new JTable(model); JButton addButton = new JButton("Add Philosopher"); addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { String[] philosopher = { "", "", "" }; model.addRow(philosopher); } }); JButton removeButton = new JButton("Remove Selected Philosopher"); removeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { model.removeRow(table.getSelectedRow()); } }); JPanel inputPanel = new JPanel(); inputPanel.add(addButton); inputPanel.add(removeButton); Container container = getContentPane(); container.add(new JScrollPane(table), BorderLayout.CENTER); container.add(inputPanel, BorderLayout.NORTH); setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 300); setVisible(true); }