List of usage examples for javax.swing JButton JButton
public JButton(Action a)
Action
supplied. From source file:GridBagWithWeight.java
public static void main(String[] args) { JFrame f = new JFrame("Demonstrates the use of weightx, weighty constraints"); JPanel p = new JPanel(); p.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); p.setLayout(new GridBagLayout()); c = new GridBagConstraints(); c.insets = new Insets(2, 2, 2, 2); c.weighty = 1.0;//from w w w. j a v a2s. c o m c.weightx = 1.0; c.gridx = 0; c.gridy = 0; p.add(new JButton("Java"), c); c.gridx = 1; p.add(new JButton("Source"), c); c.gridx = 0; c.gridy = 1; p.add(new JButton("and"), c); c.gridx = 1; p.add(new JButton("Support."), c); WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; f.addWindowListener(wndCloser); f.getContentPane().add(p); f.setSize(600, 200); f.show(); }
From source file:ManualDisplayPopup.java
public static void main(String args[]) { JFrame frame = new JFrame("NoButton Popup"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Ask"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Component source = (Component) actionEvent.getSource(); JOptionPane optionPane = new JOptionPane("Continue printing?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); JDialog dialog = optionPane.createDialog(source, "Manual Creation"); dialog.show();/* w w w . ja va2 s. c o m*/ int selection = OptionPaneUtils.getSelection(optionPane); System.out.println(selection); } }; button.addActionListener(actionListener); Container contentPane = frame.getContentPane(); contentPane.add(button, BorderLayout.SOUTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:MainClass.java
public static void main(final String args[]) { JFrame frame = new JFrame("Justified Titled Borders"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Border greenBorder = BorderFactory.createLineBorder(Color.GREEN, 2); Border blueBorder = BorderFactory.createLineBorder(Color.BLUE, 2); Border magentaBorder = BorderFactory.createLineBorder(Color.MAGENTA, 2); Border twoColorBorder = new CompoundBorder(magentaBorder, blueBorder); Border threeColorBorder = new CompoundBorder(twoColorBorder, greenBorder); JButton rainbowButton = new JButton("Rainbow"); rainbowButton.setBorder(threeColorBorder); Container contentPane = frame.getContentPane(); contentPane.add(rainbowButton);// w ww . j av a 2 s.c om frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { SpringLayout layout = new SpringLayout(); JPanel p = new JPanel(layout); p.setBorder(BorderFactory.createLineBorder(Color.GREEN, 10)); JLabel l1 = new JLabel("label: width=90%", SwingConstants.CENTER); l1.setBorder(BorderFactory.createLineBorder(Color.RED, 1)); JButton l2 = new JButton("button: width=50%"); Spring panelw = layout.getConstraint(WIDTH, p); SpringLayout.Constraints c1 = layout.getConstraints(l1); c1.setX(Spring.constant(0));/*from w w w . ja v a2 s. c o m*/ c1.setY(Spring.constant(20)); c1.setWidth(Spring.scale(panelw, 0.9f)); p.add(l1); SpringLayout.Constraints c2 = layout.getConstraints(l2); c2.setWidth(Spring.scale(panelw, 0.5f)); layout.putConstraint(SOUTH, l2, -20, SOUTH, p); layout.putConstraint(EAST, l2, -20, EAST, p); p.add(l2); JFrame f = new JFrame(); f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.getContentPane().add(p); f.setSize(320, 240); f.setVisible(true); }
From source file:Main.java
public static final void main(String[] args) { JFrame frame = new JFrame(); JPanel mainPanel = new JPanel(); JPanel buttonsPanel = new JPanel(); frame.add(mainPanel);/*from w w w. j a va 2 s .co m*/ frame.add(buttonsPanel, BorderLayout.SOUTH); String[] options = { "S", "G", "I", "T" }; JComboBox comboBox = new JComboBox(options); comboBox.setRenderer(new MyComboBoxRenderer("COUNTRY")); comboBox.setSelectedIndex(-1); mainPanel.add(comboBox); JButton clearSelectionButton = new JButton("Clear selection"); clearSelectionButton.addActionListener(e -> { comboBox.setSelectedIndex(-1); }); buttonsPanel.add(clearSelectionButton); frame.pack(); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:MouseEnterFocusMover.java
public static void main(String args[]) { JFrame frame = new JFrame("Focus Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MouseListener mouseListener = new MouseEnterFocusMover(); frame.setLayout(new GridLayout(3, 3)); for (int i = 1; i < 10; i++) { JButton button = new JButton(Integer.toString(i)); button.addMouseListener(mouseListener); if ((i % 2) != 0) { button.setFocusable(false);// www .j a va2 s. co m } frame.add(button); } frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel controlPane = new JPanel(); JPanel buttonPane = new JPanel(); controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS)); controlPane.setPreferredSize(new Dimension(200, 200)); controlPane.add(new JScrollPane(new JTextArea())); buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT)); buttonPane.setPreferredSize(new Dimension(100, 40)); buttonPane.add(new JButton("Button1")); buttonPane.add(new JButton("Button2")); frame.add(controlPane, BorderLayout.NORTH); frame.add(buttonPane, BorderLayout.SOUTH); frame.pack();/*from w ww . jav a 2 s. c o m*/ frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JDialog dialog;//from w ww . jav a 2s. c o m JList jlist; ActionListener otherListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("current"); } }; JButton okButton = new JButton("OK"); okButton.addActionListener(e -> close(true)); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(e -> close(false)); jlist = new JList(new String[] { "A", "B", "C", "D", "E", "F", "G" }); jlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); jlist.setVisibleRowCount(5); JScrollPane scroll = new JScrollPane(jlist); JPanel buttonsPanel = new JPanel(new FlowLayout()); buttonsPanel.add(okButton); buttonsPanel.add(cancelButton); JPanel content = new JPanel(new BorderLayout()); content.add(scroll, BorderLayout.CENTER); content.add(buttonsPanel, BorderLayout.SOUTH); dialog = new JDialog((Frame) null, true); dialog.setContentPane(content); dialog.pack(); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().registerKeyboardAction(otherListener, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); dialog.getRootPane().getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "doSomething"); dialog.getRootPane().getActionMap().put("doSomething", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { System.exit(0); } }); dialog.setVisible(true); }
From source file:CreateColorSamplePopup.java
public static void main(String args[]) { JFrame frame = new JFrame("JColorChooser Create Popup Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); final JButton button = new JButton("Pick to Change Background"); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color initialBackground = button.getBackground(); final JColorChooser colorChooser = new JColorChooser(initialBackground); // colorChooser.setPreviewPanel(new JPanel()); final JLabel previewLabel = new JLabel("I Love Swing", JLabel.CENTER); previewLabel.setFont(new Font("Serif", Font.BOLD | Font.ITALIC, 48)); colorChooser.setPreviewPanel(previewLabel); // Bug workaround colorChooser.updateUI();/* ww w . j ava 2 s .com*/ // For okay button selection, change button background to // selected color ActionListener okActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { Color newColor = colorChooser.getColor(); if (newColor.equals(button.getForeground())) { System.out.println("Color change rejected"); } else { button.setBackground(colorChooser.getColor()); } } }; // For cancel button selection, change button background to red ActionListener cancelActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { button.setBackground(Color.red); } }; final JDialog dialog = JColorChooser.createDialog(null, "Change Button Background", true, colorChooser, okActionListener, cancelActionListener); // Wait until current event dispatching completes before showing // dialog Runnable showDialog = new Runnable() { public void run() { dialog.show(); } }; SwingUtilities.invokeLater(showDialog); } }; button.addActionListener(actionListener); contentPane.add(button, BorderLayout.CENTER); frame.setSize(300, 100); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(); buttonPanel.add(new JButton(new AbstractAction("Update") { @Override//from w w w. j a va2 s .c o m public void actionPerformed(ActionEvent ae) { StyledDocument doc = (StyledDocument) textPane.getDocument(); SimpleAttributeSet style = new SimpleAttributeSet(); StyleConstants.setFontFamily(style, "Serif"); StyleConstants.setFontSize(style, size++); try { doc.insertString(doc.getLength(), " one two three", style); Dimension d = textPane.getPreferredSize(); Rectangle r = textPane.modelToView(textPane.getDocument().getLength()); d.height = r.y + r.height; textPane.setPreferredSize(d); } catch (BadLocationException e) { e.printStackTrace(); } frame.pack(); } })); frame.add(buttonPanel, BorderLayout.NORTH); textPane.setText("this is a test."); textPane.setBorder(new LineBorder(Color.BLACK)); frame.add(new JScrollPane(textPane)); frame.pack(); frame.setVisible(true); }