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[]) {
    Main t = new Main();

    JPanel mainPanel = new JPanel(new BorderLayout());

    JLabel l = new JLabel("hello world");
    l.setOpaque(true);/*from w w w.  j av a2s . c  o  m*/
    l.setBackground(Color.RED);

    JPanel extraPanel = new JPanel(new FlowLayout());
    l.setPreferredSize(new Dimension(100, 100));
    extraPanel.setBackground(Color.GREEN);

    extraPanel.add(l);

    mainPanel.add(extraPanel, BorderLayout.CENTER);

    t.setContentPane(mainPanel);

    t.setDefaultCloseOperation(EXIT_ON_CLOSE);
    t.setSize(400, 200);
    t.setVisible(true);
}

From source file:RigidArea.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBorder(new EmptyBorder(new Insets(40, 60, 40, 60)));

    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));

    JFrame f = new JFrame();
    f.add(panel);//from ww  w  .j a  v  a2 s . co  m
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame jFrame = new JFrame();
    jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    jFrame.setSize(400, 300);//  ww  w.  ja  v a 2 s .c o m
    JPanel panel = new JPanel(new FlowLayout());
    jFrame.setContentPane(panel);

    JEditorPane editor = new JEditorPane();
    new Main().remove_border(editor);
    panel.add(editor);

    jFrame.setVisible(true);
}

From source file:BorderExample.java

public static void main(String[] args) {
    JPanel panel = new JPanel(new BorderLayout());
    JPanel top = new JPanel();

    top.setBackground(Color.gray);
    top.setPreferredSize(new Dimension(250, 150));
    panel.add(top);

    panel.setBorder(new EmptyBorder(new Insets(10, 20, 30, 40)));
    JFrame f = new JFrame();
    f.add(panel);/*ww w  .ja  v a 2  s . c om*/
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    String items[] = { "Item1", "item1" };
    JFrame f = new JFrame();
    JPanel panel = new JPanel();
    JComboBox<String> combo = new JComboBox<>(items);
    combo.setEditable(true);//w ww  .  j ava 2  s.co m

    JTextField txt = new JTextField(10);
    panel.add(combo);
    panel.add(txt);
    f.add(panel);
    combo.addItemListener(new ItemListener() {
        @Override
        public void itemStateChanged(ItemEvent ie) {
            String str = (String) combo.getSelectedItem();
            txt.setText(str);
        }
    });
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(400, 100);
    f.setVisible(true);
}

From source file:MyCheckBoxUI.java

public static void main(String[] argv) {
    JFrame f = new JFrame();
    f.setSize(400, 300);/* ww  w.  ja  v  a  2s . c o m*/
    f.setLayout(new FlowLayout());

    JPanel p = new JPanel();
    JCheckBox bt1 = new JCheckBox("Click Me");
    bt1.setUI(new MyCheckBoxUI());
    p.add(bt1);
    f.add(p);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   w  w  w . ja  va 2s.c o m*/
        bg = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    } catch (Exception ex) {
        System.out.println(ex);
    }

    JPanel tabPanel = new JPanel(new GridBagLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 300);
        }
    };
    JPanel buttons = new JPanel(new GridLayout(4, 1, 15, 15));
    buttons.setOpaque(false);
    for (int i = 0; i < 4; i++) {
        buttons.add(new JButton("Button"));
    }
    tabPanel.add(buttons);

    JTabbedPane tabPane = new JTabbedPane();
    tabPane.add("Panel with Bachground", tabPanel);

    JFrame frame = new JFrame();
    frame.setContentPane(tabPane);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:CombinationFormatter.java

public static void main(String argv[]) {
    // a demo main() to show how CombinationFormatter could be used
    int comb1[] = { 35, 11, 19 };
    int comb2[] = { 10, 20, 30 };

    final JFormattedTextField field1 = new JFormattedTextField(new CombinationFormatter());
    field1.setValue(comb1);/*from w w w.ja  v  a 2s.c o m*/

    final JFormattedTextField field2 = new JFormattedTextField(new CombinationFormatter());
    field2.setValue(comb2);

    JPanel pan = new JPanel();
    pan.add(new JLabel("Change the combination from"));
    pan.add(field1);
    pan.add(new JLabel("to"));
    pan.add(field2);

    JButton b = new JButton("Submit");
    b.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent ae) {
            try {
                field1.commitEdit(); // make sure current edit (if any) gets
                // committed
                field2.commitEdit();
            } catch (java.text.ParseException pe) {
            }
            int oldc[] = (int[]) field1.getValue();
            int newc[] = (int[]) field2.getValue();
            //
            // code to validate oldc[] and change to newc[] goes here
            //
        }
    });
    pan.add(b);

    JFrame f = new JFrame("CombinationFormatter Demo");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(pan);
    f.setSize(360, 100);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame("BoxLayout  Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    JPanel hPanel = new JPanel();
    BoxLayout boxLayout = new BoxLayout(hPanel, BoxLayout.X_AXIS);
    hPanel.setLayout(boxLayout);//  w ww .  j  a va2s .  c o  m

    for (int i = 1; i <= 5; i++) {
        hPanel.add(new JButton("Button  " + i));
    }

    contentPane.add(hPanel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {

    JFrame frame = new JFrame("Clip Image");
    Container contentPane = frame.getContentPane();

    final Clipboard clipboard = frame.getToolkit().getSystemClipboard();

    Icon icon = new ImageIcon("jaeger.jpg");
    final JLabel label = new JLabel(icon);
    label.setTransferHandler(new ImageSelection());

    JScrollPane pane = new JScrollPane(label);
    contentPane.add(pane, BorderLayout.CENTER);

    JButton copy = new JButton("Copy");
    copy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            TransferHandler handler = label.getTransferHandler();
            handler.exportToClipboard(label, clipboard, TransferHandler.COPY);
        }/*from  ww  w .  j  a  v a  2 s.  co  m*/
    });

    JButton clear = new JButton("Clear");
    clear.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            label.setIcon(null);
        }
    });

    JButton paste = new JButton("Paste");
    paste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            Transferable clipData = clipboard.getContents(clipboard);
            if (clipData != null) {
                if (clipData.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                    TransferHandler handler = label.getTransferHandler();
                    handler.importData(label, clipData);
                }
            }
        }
    });

    JPanel p = new JPanel();
    p.add(copy);
    p.add(clear);
    p.add(paste);
    contentPane.add(p, BorderLayout.SOUTH);
    frame.setSize(300, 300);
    frame.show();
}