List of usage examples for javax.swing JPanel add
public Component add(Component comp)
From source file:Main.java
public static void main(String[] args) { JFrame f = new JFrame(); final ConfirmDialog dialog = new ConfirmDialog(f); final JTree tree = new JTree(); tree.setVisibleRowCount(5);// w ww . j a v a 2 s.c om final JScrollPane treeScroll = new JScrollPane(tree); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b = new JButton("Choose Tree Item"); b.addActionListener(e -> { int result = dialog.showConfirmDialog(treeScroll, "Choose an item"); if (result == ConfirmDialog.OK_OPTION) { System.out.println(tree.getSelectionPath()); } else { System.out.println("User cancelled"); } }); JPanel p = new JPanel(new BorderLayout()); p.add(b); p.setBorder(new EmptyBorder(50, 50, 50, 50)); f.setContentPane(p); f.pack(); f.setLocationByPlatform(true); f.setVisible(true); }
From source file:PassiveTextField1.java
public static void main(String[] args) { JFrame f = new JFrame("Passive Text Field"); f.getContentPane().setLayout(new BoxLayout(f.getContentPane(), BoxLayout.Y_AXIS)); final JTextField ptf = new JTextField(32); JTextField tf = new JTextField(32); JPanel p = new JPanel(); JButton b = new JButton("OK"); p.add(b); f.getContentPane().add(ptf);/* w w w .ja va 2 s. c o m*/ f.getContentPane().add(tf); f.getContentPane().add(p); Keymap map = ptf.getKeymap(); // Gets the shared map KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); map.removeKeyStrokeBinding(key); ActionListener l = new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Action event from a text field"); } }; ptf.addActionListener(l); tf.addActionListener(l); // Make the button the default button f.getRootPane().setDefaultButton(b); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { System.out.println("Content of text field: <" + ptf.getText() + ">"); } }); f.pack(); f.setVisible(true); }
From source file:FtfInputVerifier.java
public static void main(String argv[]) { java.net.URL u = null;/*from w w w .j a v a2s. c om*/ try { u = new java.net.URL("http://www.ora.com/"); } catch (java.net.MalformedURLException ignored) { } // create two identical JFormattedTextFields JFormattedTextField ftf1 = new JFormattedTextField(u); JFormattedTextField ftf2 = new JFormattedTextField(u); // and set an InputVerifier on one of them ftf2.setInputVerifier(new InputVerifier() { public boolean verify(JComponent input) { if (!(input instanceof JFormattedTextField)) return true; // give up focus return ((JFormattedTextField) input).isEditValid(); } }); JPanel p = new JPanel(new java.awt.GridLayout(0, 2, 3, 8)); p.add(new JLabel("plain JFormattedTextField:", JLabel.RIGHT)); p.add(ftf1); p.add(new JLabel("FTF with InputVerifier:", JLabel.RIGHT)); p.add(ftf2); p.add(new JLabel("plain JTextField:", JLabel.RIGHT)); p.add(new JTextField(u.toString())); p.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); JFrame f = new JFrame("FtfInputVerifier"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JLabel("Try to delete the colon in each field.", JLabel.CENTER), java.awt.BorderLayout.NORTH); f.getContentPane().add(p, java.awt.BorderLayout.CENTER); f.pack(); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JLabel label = new JLabel("java2s.com"); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new BorderLayout()); frame.add(label);/*from w w w. ja v a 2 s .c o m*/ JPanel buttons = new JPanel(new GridLayout(0, 1)); for (int index = 0; index < 10; index++) { buttons.add(new JButton(String.valueOf(index))); } JPanel right = new JPanel(new BorderLayout()); right.add(buttons, BorderLayout.NORTH); frame.add(right, BorderLayout.EAST); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { JFrame f = new JFrame(Main.class.getSimpleName()); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BufferedImage bi = ImageIO.read(new URL("http://www.java2s.com/style/download.png")); JPanel panel = new JPanel(new BorderLayout()); JLabel label = new JLabel(new ImageIcon(bi)); panel.add(label); MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() { @Override//w ww.ja v a2 s . c o m public void mouseDragged(MouseEvent e) { Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1); ((JPanel) e.getSource()).scrollRectToVisible(r); } }; panel.addMouseMotionListener(doScrollRectToVisible); panel.setAutoscrolls(true); f.add(new JScrollPane(panel)); f.pack(); f.setSize(f.getWidth() / 2, f.getHeight() / 2); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JDialog dialog;/*from ww w . j av 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:Main.java
public static void main(String args[]) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel panel = (JPanel) frame.getContentPane(); panel.setLayout(null);//from ww w.j a va 2 s . c o m JLabel label = new JLabel("aaa"); panel.add(label); Dimension size = label.getPreferredSize(); label.setBounds(100, 100, size.width, size.height); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { final JProgressBar progressBar = new JProgressBar(0, 10); final CounterTask task = new CounterTask(); task.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("progress".equals(evt.getPropertyName())) { progressBar.setValue((Integer) evt.getNewValue()); }//from w w w.j ava2 s.com } }); JButton startButton = new JButton("Start"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { task.execute(); } }); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { task.cancel(true); } }); JPanel buttonPanel = new JPanel(); buttonPanel.add(startButton); buttonPanel.add(cancelButton); JPanel cp = new JPanel(); LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS); cp.setLayout(layout); cp.add(buttonPanel); cp.add(progressBar); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) { JFrame frame = new JFrame("Action Sample"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); final Action printAction = new PrintHelloAction(); JMenuBar menuBar = new JMenuBar(); JMenu menu = new JMenu("File"); menuBar.add(menu);//from w w w . j av a 2 s . c o m menu.add(new JMenuItem(printAction)); JToolBar toolbar = new JToolBar(); toolbar.add(new JButton(printAction)); JButton enableButton = new JButton("Enable"); ActionListener enableActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { printAction.setEnabled(true); } }; enableButton.addActionListener(enableActionListener); JButton disableButton = new JButton("Disable"); ActionListener disableActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { printAction.setEnabled(false); } }; disableButton.addActionListener(disableActionListener); JButton relabelButton = new JButton("Relabel"); ActionListener relabelActionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { printAction.putValue(Action.NAME, "Hello, World"); } }; relabelButton.addActionListener(relabelActionListener); JPanel buttonPanel = new JPanel(); buttonPanel.add(enableButton); buttonPanel.add(disableButton); buttonPanel.add(relabelButton); frame.setJMenuBar(menuBar); frame.add(toolbar, BorderLayout.SOUTH); frame.add(buttonPanel, BorderLayout.NORTH); frame.setSize(300, 200); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JTextArea textArea = new JTextArea(10, 20); final JProgressBar progressBar = new JProgressBar(0, 10); final CounterTask task = new CounterTask(); task.addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if ("progress".equals(evt.getPropertyName())) { progressBar.setValue((Integer) evt.getNewValue()); }//from ww w . ja v a 2 s . co m } }); JButton startButton = new JButton("Start"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { task.execute(); } }); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { task.cancel(true); } }); JPanel buttonPanel = new JPanel(); buttonPanel.add(startButton); buttonPanel.add(cancelButton); JPanel cp = new JPanel(); LayoutManager layout = new BoxLayout(cp, BoxLayout.Y_AXIS); cp.setLayout(layout); cp.add(buttonPanel); cp.add(new JScrollPane(textArea)); cp.add(progressBar); JFrame frame = new JFrame("SwingWorker Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setContentPane(cp); frame.pack(); frame.setVisible(true); }