List of usage examples for javax.swing JButton JButton
public JButton(Action a)
Action
supplied. From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "--", "----", "--------", "------------" }; JPanel container = new JPanel(); BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS); layout.invalidateLayout(container);//w w w . j av a 2s. co m container.setLayout(layout); for (int i = 0; i < labels.length; i++) { JButton button = new JButton(labels[i]); container.add(button); } frame.add(container, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JPanel panel = new JPanel(new BorderLayout()); String[] values = new String[] { "One", "Two", "Three" }; JComboBox<String> comboBox = new JComboBox<>(values); panel.add(comboBox, BorderLayout.NORTH); JTextArea textArea = new JTextArea(2, 2); panel.add(textArea, BorderLayout.CENTER); JButton button = new JButton("Action"); button.addActionListener(new ActionListener() { @Override/*from w ww. j a v a 2s .co m*/ public void actionPerformed(ActionEvent e) { textArea.setText((String) comboBox.getSelectedItem()); comboBox.setSelectedIndex(0); } }); panel.add(button, BorderLayout.SOUTH); JFrame frame = new JFrame(); frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); }
From source file:KeyStrokeSample.java
public static void main(String args[]) { JFrame frame = new JFrame("KeyStroke Sample"); JButton buttonA = new JButton("<html><center>FOCUSED<br>control alt 7"); JButton buttonB = new JButton("<html><center>FOCUS/RELEASE<br>VK_ENTER"); JButton buttonC = new JButton("<html><center>ANCESTOR<br>VK_F4+SHIFT_MASK"); JButton buttonD = new JButton("<html><center>WINDOW<br>' '"); // Define ActionListener Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JButton source = (JButton) actionEvent.getSource(); System.out.println("Activated: " + source.getText()); }//from ww w .j a v a 2 s.c o m }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); actionMap.put(ACTION_KEY, actionListener); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true); inputMap = buttonB.getInputMap(); inputMap.put(enter, ACTION_KEY); buttonB.setActionMap(actionMap); KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK); inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(shiftF4, ACTION_KEY); buttonC.setActionMap(actionMap); KeyStroke space = KeyStroke.getKeyStroke(' '); inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(space, ACTION_KEY); buttonD.setActionMap(actionMap); Container contentPane = frame.getContentPane(); contentPane.setLayout(new GridLayout(2, 2)); contentPane.add(buttonA); contentPane.add(buttonB); contentPane.add(buttonC); contentPane.add(buttonD); frame.setSize(400, 200); frame.setVisible(true); }
From source file:ElementSample.java
public static void main(String args[]) { JFrame frame = new JFrame("Element Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); JButton button = new JButton("Show Elements"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Document document = textArea.getDocument(); ElementIterator iterator = new ElementIterator(document); Element element = iterator.first(); while (element != null) { System.out.println(element.getStartOffset()); element = iterator.next(); }// w w w .j av a2 s .c o m } }; button.addActionListener(actionListener); frame.add(scrollPane, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { Object[][] rowData = { { "Hello", "World" } }; Object[] columnNames = { "A", "B" }; JTable table;/*from ww w . j a v a 2 s . co m*/ DefaultTableModel model; model = new DefaultTableModel(rowData, columnNames); table = new JTable(); table.setModel(model); JButton add = new JButton("Add"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { model.addRow(rowData[0]); } }); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container c = f.getContentPane(); c.setLayout(new BorderLayout()); c.add(add, BorderLayout.SOUTH); c.add(new JScrollPane(table), BorderLayout.CENTER); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); }
From source file:Main.java
public static void main(String[] a) { JFrame frame = new JFrame("Alignment Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); String labels[] = { "--", "----", "--------", "------------" }; JPanel container = new JPanel(); BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS); System.out.println(layout.getTarget()); container.setLayout(layout);//w ww. j a va 2 s . c om for (int i = 0; i < labels.length; i++) { JButton button = new JButton(labels[i]); container.add(button); } frame.add(container, BorderLayout.CENTER); frame.setSize(300, 200); frame.setVisible(true); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Element Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final JTextArea textArea = new JTextArea(); JScrollPane scrollPane = new JScrollPane(textArea); JButton button = new JButton("Show Elements"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Document document = textArea.getDocument(); ElementIterator iterator = new ElementIterator(document); Element element = iterator.first(); while (element != null) { System.out.println(element.getStartOffset()); element = iterator.next(); }/*from w w w . j a va 2s . co m*/ } }; button.addActionListener(actionListener); frame.add(scrollPane, BorderLayout.CENTER); frame.add(button, BorderLayout.SOUTH); frame.setSize(250, 250); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { String ACTION_KEY = "The Action"; JFrame frame = new JFrame("KeyStroke Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton buttonA = new JButton("FOCUSED (control alt 7)"); JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)"); JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)"); JButton buttonD = new JButton("WINDOW (' ')"); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JButton source = (JButton) actionEvent.getSource(); System.out.println("Activated: " + source.getText()); }/* w w w .jav a 2s . c om*/ }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); Object[] keys = actionMap.allKeys(); actionMap.put(ACTION_KEY, actionListener); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true); inputMap = buttonB.getInputMap(); inputMap.put(enter, ACTION_KEY); buttonB.setActionMap(actionMap); KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK); inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(shiftF4, ACTION_KEY); buttonC.setActionMap(actionMap); KeyStroke space = KeyStroke.getKeyStroke(' '); inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(space, ACTION_KEY); buttonD.setActionMap(actionMap); frame.setLayout(new GridLayout(2, 2)); frame.add(buttonA); frame.add(buttonB); frame.add(buttonC); frame.add(buttonD); frame.setSize(400, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { String ACTION_KEY = "The Action"; JFrame frame = new JFrame("KeyStroke Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton buttonA = new JButton("FOCUSED (control alt 7)"); JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)"); JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)"); JButton buttonD = new JButton("WINDOW (' ')"); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JButton source = (JButton) actionEvent.getSource(); System.out.println("Activated: " + source.getText()); }//from www . ja v a 2s. c om }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); actionMap.clear(); actionMap.put(ACTION_KEY, actionListener); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true); inputMap = buttonB.getInputMap(); inputMap.put(enter, ACTION_KEY); buttonB.setActionMap(actionMap); KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK); inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(shiftF4, ACTION_KEY); buttonC.setActionMap(actionMap); KeyStroke space = KeyStroke.getKeyStroke(' '); inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(space, ACTION_KEY); buttonD.setActionMap(actionMap); frame.setLayout(new GridLayout(2, 2)); frame.add(buttonA); frame.add(buttonB); frame.add(buttonC); frame.add(buttonD); frame.setSize(400, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String args[]) { String ACTION_KEY = "The Action"; JFrame frame = new JFrame("KeyStroke Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton buttonA = new JButton("FOCUSED (control alt 7)"); JButton buttonB = new JButton("FOCUS/RELEASE (VK_ENTER)"); JButton buttonC = new JButton("ANCESTOR (VK_F4+SHIFT_MASK)"); JButton buttonD = new JButton("WINDOW (' ')"); Action actionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { JButton source = (JButton) actionEvent.getSource(); System.out.println("Activated: " + source.getText()); }//from w w w . j a v a 2 s . c om }; KeyStroke controlAlt7 = KeyStroke.getKeyStroke("control alt 7"); InputMap inputMap = buttonA.getInputMap(); inputMap.put(controlAlt7, ACTION_KEY); ActionMap actionMap = buttonA.getActionMap(); actionMap.put(ACTION_KEY, actionListener); KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true); inputMap = buttonB.getInputMap(); inputMap.put(enter, ACTION_KEY); buttonB.setActionMap(actionMap); KeyStroke shiftF4 = KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.SHIFT_MASK); inputMap = buttonC.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); inputMap.put(shiftF4, ACTION_KEY); buttonC.setActionMap(actionMap); KeyStroke space = KeyStroke.getKeyStroke(' '); inputMap = buttonD.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputMap.put(space, ACTION_KEY); buttonD.setActionMap(actionMap); frame.setLayout(new GridLayout(2, 2)); frame.add(buttonA); frame.add(buttonB); frame.add(buttonC); frame.add(buttonD); frame.setSize(400, 200); frame.setVisible(true); }