Example usage for javax.swing JPanel add

List of usage examples for javax.swing JPanel add

Introduction

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

Prototype

public Component add(Component comp) 

Source Link

Document

Appends the specified component to the end of this container.

Usage

From source file:Main.java

public static void main(String[] args) {
    JTextField ipField = new JTextField(10);
    JTextField portField = new JTextField(10);
    JPanel panel = new JPanel();
    panel.add(new JLabel("IP:"));
    panel.add(ipField);/*from  w  ww  .j  av a  2s. c o m*/

    panel.add(Box.createHorizontalStrut(15));
    panel.add(new JLabel("Port:"));
    panel.add(portField);

    int result = JOptionPane.showConfirmDialog(null, panel, "Enter Information", JOptionPane.OK_CANCEL_OPTION);

    if (result == JOptionPane.OK_OPTION) {
        System.out.println("IP: " + ipField.getText());
        System.out.println("Port: " + portField.getText());
    }
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new GridLayout(0, 5));
    panel.add(new BasicArrowButton(BasicArrowButton.EAST));
    panel.add(new BasicArrowButton(BasicArrowButton.NORTH));
    panel.add(new BasicArrowButton(BasicArrowButton.SOUTH));
    panel.add(new BasicArrowButton(BasicArrowButton.WEST));
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    JButton component1 = new JButton();
    JButton component2 = new JButton();
    int align = FlowLayout.CENTER; // or LEFT, RIGHT
    JPanel panel = new JPanel(new FlowLayout(align));
    panel.add(component1);
    panel.add(component2);/* w  w w . java2s  . c o  m*/
}

From source file:Main.java

public static void main(String[] args) {
    JScrollPane sPane = new JScrollPane();
    sPane.setPreferredSize(new Dimension(200, 150));
    JButton button = new JButton(new AbstractAction("Create Table") {
        public void actionPerformed(ActionEvent arg0) {
            DefaultTableModel model = new DefaultTableModel(new Integer[][] { { 1, 2 }, { 3, 4 } },
                    new String[] { "A", "B" });
            JTable table = new JTable(model);
            sPane.getViewport().add(table);
        }/*from   www  . j ava2s.  co m*/
    });
    JPanel panel = new JPanel();
    panel.add(sPane);
    panel.add(button);
    JOptionPane.showMessageDialog(null, panel);

}

From source file:Main.java

public static void main(String[] args) {
    JList<String> list = new JList<String>(new String[] { "one", "two", "three", "four", "five" });
    JScrollPane scrollPane = new JScrollPane(list);
    JButton btn = new JButton(new AbstractAction() {
        {/*from w ww.  ja  va  2 s. c o m*/
            putValue(NAME, "Select");
        }

        @Override
        public void actionPerformed(ActionEvent evt) {
            list.setSelectedIndex(0);
        }
    });
    JPanel panel = new JPanel();
    panel.add(scrollPane);
    panel.add(btn);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static void main(String[] args) {
    JTextField tf = new JTextField("mm");
    tf.setPreferredSize(tf.getPreferredSize());
    tf.setText("");

    JPanel pHacked = new JPanel();
    pHacked.add(tf);

    JPanel pStock = new JPanel();
    pStock.add(new JTextField(2));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new java.awt.GridLayout(0, 1));
    frame.add(pHacked);/*from   w  w w. j  a v  a 2 s. c  o  m*/
    frame.add(pStock);
    frame.setSize(150, 150);
    frame.setVisible(true);
    tf.requestFocus();
}

From source file:Main.java

public static void main(String[] args) {
    JTextField xField = new JTextField(5);
    JTextField yField = new JTextField(5);

    JPanel myPanel = new JPanel();
    myPanel.add(new JLabel("x:"));
    myPanel.add(xField);/*from w w  w. j  a v  a 2s  . co  m*/
    myPanel.add(Box.createHorizontalStrut(15)); // a spacer
    myPanel.add(new JLabel("y:"));
    myPanel.add(yField);

    int result = JOptionPane.showConfirmDialog(null, myPanel, "Please Enter X and Y Values",
            JOptionPane.OK_CANCEL_OPTION);
    if (result == JOptionPane.OK_OPTION) {
        System.out.println("x value: " + xField.getText());
        System.out.println("y value: " + yField.getText());
    }

}

From source file:Main.java

public static void main(String[] args) {

    JFrame frame = new JFrame();

    JButton btn = new JButton("Test");

    JPanel panel = new JPanel();
    panel.add(btn);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.add("Tab1", panel);

    frame.add(tabbedPane, BorderLayout.CENTER);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();//from  w  w  w. j av  a  2  s  .  co  m
    frame.setMinimumSize(new Dimension(300, 300));
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTextField smallField = new JTextField(5);
    JTextField largeField = new JTextField(20);
    JPanel gui = new JPanel(new FlowLayout());
    gui.add(smallField);
    gui.add(largeField);//w  ww  .  jav a 2  s  .  com
    JFrame f = new JFrame("Text Field Size");
    f.add(gui);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.pack();
    f.setLocationByPlatform(true);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String[] names = { "John Smith", "this is a test", "this is a test. this is a test. " };
    MyCellRenderer cellRenderer = new MyCellRenderer(80);
    JList<String> list = new JList<>(names);
    list.setCellRenderer(cellRenderer);/*from  w ww .  j  a v a  2s  .co  m*/
    JScrollPane sPane = new JScrollPane(list);
    JPanel panel = new JPanel();
    panel.add(sPane);
    JOptionPane.showMessageDialog(null, panel);

}