Example usage for javax.swing JPanel JPanel

List of usage examples for javax.swing JPanel JPanel

Introduction

In this page you can find the example usage for javax.swing JPanel JPanel.

Prototype

public JPanel() 

Source Link

Document

Creates a new JPanel with a double buffer and a flow layout.

Usage

From source file:Main.java

public static void main(String[] args) {
    int ROW_HEIGHT = 40;
    String[] TABLE_COLUMNS = { "Foo", "Bar" };
    DefaultTableModel tableModel = new DefaultTableModel(TABLE_COLUMNS, 2);
    JTable table = new JTable(tableModel);
    table.setRowHeight(ROW_HEIGHT);//from  w w w.  j  a v  a2s .c o m
    JScrollPane scrollpane = new JScrollPane(table);
    JButton addRowBtn = new JButton(new AbstractAction("Add Row") {
        @Override
        public void actionPerformed(ActionEvent arg0) {
            tableModel.addRow(new String[] { "", "" });
        }
    });
    JPanel btnPanel = new JPanel();
    btnPanel.add(addRowBtn);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scrollpane, BorderLayout.CENTER);
    f.getContentPane().add(btnPanel, BorderLayout.PAGE_END);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextField firstField = new JTextField(10);
    JTextField middleField = new JTextField(10);
    JTextField lastField = new JTextField(10);

    JLabel firstLabel = new JLabel("First Name", JLabel.RIGHT);
    firstLabel.setDisplayedMnemonic('F');
    firstLabel.setLabelFor(firstField);//from   w  ww  . java2 s .co  m

    JLabel middleLabel = new JLabel("Middle Initial", JLabel.RIGHT);
    middleLabel.setDisplayedMnemonic('I');
    middleLabel.setDisplayedMnemonicIndex(7);
    middleLabel.setLabelFor(middleField);

    JLabel lastLabel = new JLabel("Last Name", JLabel.RIGHT);
    lastLabel.setDisplayedMnemonic('L');
    lastLabel.setLabelFor(lastField);

    JPanel p = new JPanel();
    p.setLayout(new GridLayout(3, 2, 5, 5));
    p.add(firstLabel);
    p.add(firstField);
    p.add(middleLabel);
    p.add(middleField);
    p.add(lastLabel);
    p.add(lastField);

    JFrame f = new JFrame("MnemonicLabels");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(p);
    f.pack();
    f.setVisible(true);
}

From source file:BoxSample.java

public static void main(String args[]) {
    JButton button;//ww  w. ja  va2  s .co m
    Vector buttons = new Vector();
    Dimension dim;
    JFrame frame = new JFrame("Box Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
    JPanel topLeft = new JPanel();
    topLeft.setLayout(new BoxLayout(topLeft, BoxLayout.X_AXIS));
    topLeft.add(button = new JButton("One"));
    buttons.add(button);
    changeBoth(button);
    topLeft.add(button = new JButton("Two"));
    buttons.add(button);
    changeBoth(button);
    changeWidth(topLeft);
    JPanel bottomLeft = new JPanel();
    bottomLeft.setLayout(new BoxLayout(bottomLeft, BoxLayout.X_AXIS));
    bottomLeft.add(button = new JButton("Six"));
    buttons.add(button);
    changeBoth(button);
    bottomLeft.add(button = new JButton("Seven"));
    buttons.add(button);
    changeBoth(button);
    changeWidth(bottomLeft);
    JPanel left = new JPanel();
    left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
    left.add(topLeft);
    left.add(button = new JButton("Four"));
    buttons.add(button);
    changeBoth(button);
    left.add(bottomLeft);
    changeBoth(left);

    JPanel right = new JPanel();
    right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
    right.add(button = new JButton("Three"));
    buttons.add(button);
    changeWidth(button);
    right.add(button = new JButton("Five"));
    buttons.add(button);
    changeBoth(button);
    changeBoth(right);
    contentPane.add(left);
    contentPane.add(right);
    tweak(buttons);
    frame.pack();
    frame.setVisible(true);
}

From source file:MaskInputSample.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Mask Input");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label;/*from w w w .j  av  a 2s .  c om*/
    JFormattedTextField input;
    JPanel panel;
    MaskFormatter formatter;

    BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
    frame.setLayout(layout);

    try {
        label = new JLabel("SSN");
        formatter = new MaskFormatter("###'-##'-####");
        input = new JFormattedTextField(formatter);
        input.setValue("123-45-6789");
        input.setColumns(20);
        panel = new JPanel();
        panel.add(label);
        panel.add(input);
        frame.add(panel);
    } catch (ParseException e) {
        System.err.println("Unable to add SSN");
    }
    frame.pack();
    frame.setVisible(true);
}

From source file:DefaultSample.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Default Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = frame.getContentPane();

    JTextField textField = new JTextField();
    content.add(textField, BorderLayout.NORTH);

    ActionListener actionListener = new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println(actionEvent.getActionCommand() + " selected");
        }/*from  w  w  w.jav  a 2s  .c om*/
    };

    JPanel panel = new JPanel();
    JButton defaultButton = new JButton("Default Button");
    defaultButton.addActionListener(actionListener);
    panel.add(defaultButton);

    JButton otherButton = new JButton("Other Button");
    otherButton.addActionListener(actionListener);
    panel.add(otherButton);

    content.add(panel, BorderLayout.SOUTH);

    Keymap keymap = textField.getKeymap();
    KeyStroke keystroke = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
    keymap.removeKeyStrokeBinding(keystroke);

    frame.getRootPane().setDefaultButton(defaultButton);

    frame.setSize(250, 150);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setUI(new MetalTabbedPaneUI() {
        @Override/* ww w .j  ava2  s.  c  o  m*/
        protected int calculateTabWidth(int tabPlacement, int tabIndex, FontMetrics metrics) {
            int width = super.calculateTabWidth(tabPlacement, tabIndex, metrics);
            int extra = tabIndex * 50;
            return width + extra;
        }
    });
    tabbedPane.addTab("JTable", new JScrollPane(new JTable(5, 5)));
    tabbedPane.addTab("JTree", new JScrollPane(new JTree()));
    tabbedPane.addTab("JSplitPane", new JSplitPane());

    JPanel p = new JPanel();
    p.add(tabbedPane);

    JFrame frame = new JFrame();
    frame.setContentPane(p);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    int TIMER_DELAY = 2000;
    String[] data = { "A", "B", "C", "D" };

    DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(data);

    JComboBox<String> combobox = new JComboBox<>(model);
    JList<String> jlist = new JList<>(model);

    new Timer(TIMER_DELAY, new ActionListener() {
        private int count = 0;

        public void actionPerformed(ActionEvent e) {
            model.addElement("count: " + count);
            count++;//w w  w. java 2  s. c o  m
        }
    }).start();

    JPanel comboPanel = new JPanel();
    comboPanel.add(combobox);

    JPanel listPanel = new JPanel();
    listPanel.add(new JScrollPane(jlist));

    JPanel panel = new JPanel(new GridLayout(1, 0));
    panel.add(comboPanel);
    panel.add(listPanel);
    panel.setPreferredSize(new Dimension(400, 200));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(panel);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    BufferedImage original = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gd.getDefaultConfiguration();

    BufferedImage rotated1 = create(original, -Math.PI / 2, gc);
    BufferedImage rotated2 = create(original, +Math.PI / 4, gc);
    BufferedImage rotated3 = create(original, Math.PI, gc);

    JPanel cp = new JPanel();
    cp.add(new JLabel(new ImageIcon(original)));
    cp.add(new JLabel(new ImageIcon(rotated1)));
    cp.add(new JLabel(new ImageIcon(rotated2)));
    cp.add(new JLabel(new ImageIcon(rotated3)));

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(cp);//  w  ww  . j  a v  a 2s.com
    f.pack();
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    JTextPane textPane = new JTextPane();
    JTextField tf = new JTextField("is");
    String word = "";
    Highlighter highlighter = new UnderlineHighlighter(null);

    textPane.setHighlighter(highlighter);
    textPane.setText("This is a test");
    panel.setLayout(new BorderLayout());
    panel.add(new JLabel("Enter word, then press ENTER key: "), "West");
    panel.add(tf, "Center");

    final WordSearcher searcher = new WordSearcher(textPane);
    tf.addActionListener(e -> {//from  w  w w  . ja va  2 s  . c o  m
        String w = tf.getText().trim();
        int offset = searcher.search(w);
        if (offset == -1) {
            return;
        }
        try {
            textPane.scrollRectToVisible(textPane.modelToView(offset));
        } catch (BadLocationException ex) {
        }

    });
    textPane.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void removeUpdate(DocumentEvent evt) {
            searcher.search(word);
        }

        @Override
        public void changedUpdate(DocumentEvent evt) {
        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(panel, "South");
    f.add(new JScrollPane(textPane), "Center");
    f.setSize(400, 400);
    f.setVisible(true);
}

From source file:GTKLookAndFeelDemo.java

public static void main(String[] args) {
    try {/*from w w w  . j  av a2 s.c o m*/
        UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
    } catch (Exception e) {
        e.printStackTrace();
    }
    JLabel label = new JLabel("Label");
    JTextField field = new JTextField("www.java2s.com!");
    JList list = new JList(new String[] { "A", "B", "C" });
    JScrollPane listPane = new JScrollPane(list);
    listPane.setPreferredSize(new Dimension(250, 100));

    JScrollPane treePane = new JScrollPane(new JTree());
    treePane.setPreferredSize(new Dimension(250, 100));
    JButton button = new JButton("Click me");

    JPanel cp = new JPanel();
    cp.add(label);
    cp.add(field);
    cp.add(listPane);
    cp.add(treePane);
    cp.add(button);

    JFrame frame = new JFrame();
    frame.setTitle("Windows Look and Feel Demo");
    frame.setPreferredSize(new Dimension(280, 300));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(cp);
    frame.pack();
    frame.setVisible(true);

}