List of usage examples for javax.swing JButton addActionListener
public void addActionListener(ActionListener l)
ActionListener
to the button. From source file:Main.java
public static void main(final String args[]) { final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("A"); model.addElement("C"); model.addElement("D"); model.addElement("A"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox1 = new JComboBox<String>(model); comboBox1.setMaximumRowCount(5);/*ww w . j av a 2 s . co m*/ comboBox1.setEditable(true); frame.add(comboBox1, BorderLayout.NORTH); JList<String> jlist = new JList<String>(model); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); frame.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("a"); System.out.println(model.getIndexOf("A")); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(final String args[]) { final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("A"); model.addElement("C"); model.addElement("D"); model.addElement("A"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox1 = new JComboBox<String>(model); comboBox1.setMaximumRowCount(5);//from w ww. j av a 2 s . c o m comboBox1.setEditable(true); frame.add(comboBox1, BorderLayout.NORTH); JList<String> jlist = new JList<String>(model); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); frame.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("a"); System.out.println(model.getElementAt(1)); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(final String args[]) { final DefaultComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("A"); model.addElement("C"); model.addElement("D"); model.addElement("A"); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComboBox<String> comboBox1 = new JComboBox<String>(model); comboBox1.setMaximumRowCount(5);//from www . j av a 2 s . co m comboBox1.setEditable(true); frame.add(comboBox1, BorderLayout.NORTH); JList<String> jlist = new JList<String>(model); JScrollPane scrollPane = new JScrollPane(jlist); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); frame.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("a"); System.out.println(model.getSelectedItem()); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:ModifyModelSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Modifying Model"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); // Fill model final DefaultListModel model = new DefaultListModel(); for (int i = 0, n = labels.length; i < n; i++) { model.addElement(labels[i]);//from ww w .j a v a 2 s .c o m } JList jlist = new JList(model); JScrollPane scrollPane1 = new JScrollPane(jlist); contentPane.add(scrollPane1, BorderLayout.WEST); final JTextArea textArea = new JTextArea(); textArea.setEditable(false); JScrollPane scrollPane2 = new JScrollPane(textArea); contentPane.add(scrollPane2, BorderLayout.CENTER); ListDataListener listDataListener = new ListDataListener() { public void contentsChanged(ListDataEvent listDataEvent) { appendEvent(listDataEvent); } public void intervalAdded(ListDataEvent listDataEvent) { appendEvent(listDataEvent); } public void intervalRemoved(ListDataEvent listDataEvent) { appendEvent(listDataEvent); } private void appendEvent(ListDataEvent listDataEvent) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); switch (listDataEvent.getType()) { case ListDataEvent.CONTENTS_CHANGED: pw.print("Type: Contents Changed"); break; case ListDataEvent.INTERVAL_ADDED: pw.print("Type: Interval Added"); break; case ListDataEvent.INTERVAL_REMOVED: pw.print("Type: Interval Removed"); break; } pw.print(", Index0: " + listDataEvent.getIndex0()); pw.print(", Index1: " + listDataEvent.getIndex1()); DefaultListModel theModel = (DefaultListModel) listDataEvent.getSource(); Enumeration elements = theModel.elements(); pw.print(", Elements: "); while (elements.hasMoreElements()) { pw.print(elements.nextElement()); pw.print(","); } pw.println(); textArea.append(sw.toString()); } }; model.addListDataListener(listDataListener); // Setup buttons JPanel jp = new JPanel(new GridLayout(2, 1)); JPanel jp1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1)); JPanel jp2 = new JPanel(new FlowLayout(FlowLayout.CENTER, 1, 1)); jp.add(jp1); jp.add(jp2); JButton jb = new JButton("add F"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.add(0, "First"); } }); jb = new JButton("addElement L"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.addElement("Last"); } }); jb = new JButton("insertElementAt M"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); model.insertElementAt("Middle", size / 2); } }); jb = new JButton("set F"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.set(0, "New First"); } }); jb = new JButton("setElementAt L"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.setElementAt("New Last", size - 1); } }); jb = new JButton("load 10"); jp1.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { for (int i = 0, n = labels.length; i < n; i++) { model.addElement(labels[i]); } } }); jb = new JButton("clear"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.clear(); } }); jb = new JButton("remove F"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.remove(0); } }); jb = new JButton("removeAllElements"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.removeAllElements(); } }); jb = new JButton("removeElement 'Last'"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { model.removeElement("Last"); } }); jb = new JButton("removeElementAt M"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.removeElementAt(size / 2); } }); jb = new JButton("removeRange FM"); jp2.add(jb); jb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int size = model.getSize(); if (size != 0) model.removeRange(0, size / 2); } }); contentPane.add(jp, BorderLayout.SOUTH); frame.setSize(640, 300); frame.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 ww w .ja v a 2 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:FrameKey.java
public static void main(String args[]) { final JFrame frame = new JFrame("Frame Key"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JDialog dialog = new EscapeDialog(frame, "Hey"); JButton button = new JButton("Okay"); ActionListener innerActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Dialog Button Selected"); }//from www .j a v a 2 s.com }; button.addActionListener(innerActionListener); dialog.getContentPane().add(button, BorderLayout.SOUTH); dialog.setSize(200, 200); dialog.show(); } }; JPanel content = (JPanel) frame.getContentPane(); KeyStroke stroke = KeyStroke.getKeyStroke("M"); InputMap inputMap = content.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(stroke, "OPEN"); content.getActionMap().put("OPEN", actionListener); frame.setSize(300, 300); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { final JTextField textField = new JTextField(15); JButton buttonCut = new JButton("Cut"); JButton buttonPaste = new JButton("Paste"); JButton buttonCopy = new JButton("Copy"); JFrame jfrm = new JFrame("Cut, Copy, and Paste"); jfrm.setLayout(new FlowLayout()); jfrm.setSize(230, 150);/* w w w. j a v a 2 s .c o m*/ jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buttonCut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { textField.cut(); } }); buttonPaste.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { textField.paste(); } }); buttonCopy.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent le) { textField.copy(); } }); textField.addCaretListener(new CaretListener() { public void caretUpdate(CaretEvent ce) { System.out.println("All text: " + textField.getText()); if (textField.getSelectedText() != null) System.out.println("Selected text: " + textField.getSelectedText()); else System.out.println("Selected text: "); } }); jfrm.add(textField); jfrm.add(buttonCut); jfrm.add(buttonPaste); jfrm.add(buttonCopy); jfrm.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); JPanel panel = new JPanel(); JTextPane textPane = new JTextPane(); JButton button = new JButton("Test"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); panel.setLayout(new BorderLayout()); panel.setPreferredSize(new Dimension(200, 200)); panel.add(textPane, BorderLayout.CENTER); panel.add(button, BorderLayout.SOUTH); textPane.addStyle("myStyle", null); button.addActionListener(e -> { StyledDocument doc = textPane.getStyledDocument(); int start = textPane.getSelectionStart(); int end = textPane.getSelectionEnd(); if (start == end) { return; }/*from www.j a va 2 s .c o m*/ if (start > end) { int life = start; start = end; end = life; } Style style = textPane.getStyle("myStyle"); if (StyleConstants.isBold(style)) { StyleConstants.setBold(style, false); } else { StyleConstants.setBold(style, true); } doc.setCharacterAttributes(start, end - start, style, false); }); frame.add(panel); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:SharedDataSample.java
public static void main(String args[]) { final String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; final DefaultComboBoxModel model = new DefaultComboBoxModel(labels); JFrame frame = new JFrame("Shared Data"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); JPanel panel = new JPanel(); JComboBox comboBox1 = new JComboBox(model); comboBox1.setMaximumRowCount(5);//from w ww.j av a 2 s .c o m comboBox1.setEditable(true); JComboBox comboBox2 = new JComboBox(model); comboBox2.setMaximumRowCount(5); comboBox2.setEditable(true); panel.add(comboBox1); panel.add(comboBox2); contentPane.add(panel, BorderLayout.NORTH); JList jlist = new JList(model); JScrollPane scrollPane = new JScrollPane(jlist); contentPane.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Add"); contentPane.add(button, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { int index = (int) (Math.random() * labels.length); model.addElement(labels[index]); } }; button.addActionListener(actionListener); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { final Object rows[][] = { { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, { "one", "1" }, { "two", "2" }, { "three", "3" }, { "four", "4" }, };/*from w w w. j a v a 2 s. c o m*/ final Object headers[] = { "English", "#" }; JFrame frame = new JFrame("Table Printing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTable table = new JTable(rows, headers); JScrollPane scrollPane = new JScrollPane(table); frame.add(scrollPane, BorderLayout.CENTER); JButton button = new JButton("Print"); ActionListener printAction = new ActionListener() { public void actionPerformed(ActionEvent e) { try { table.print(); } catch (PrinterException pe) { System.err.println("Error printing: " + pe.getMessage()); } } }; button.addActionListener(printAction); frame.add(button, BorderLayout.SOUTH); frame.setSize(300, 150); frame.setVisible(true); }