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:MaxLengthDocument.java

public static void main(String[] args) {
    Document doc = new MaxLengthDocument(5);
    JTextField field = new JTextField(doc, "", 8);

    JPanel flowPanel = new JPanel();
    flowPanel.add(field);
    JFrame frame = new JFrame("MaxLengthDocument demo");
    frame.setContentPane(flowPanel);//  w  w  w. jav  a2s .c  o  m
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(160, 80);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JDialog f = new JDialog();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);//from w  ww .ja  v a 2 s . com
        }
    });

    JButton btOK = new JButton("Press Enter to click me, I am the default.");
    btOK.setToolTipText("Save and exit");
    f.getRootPane().setDefaultButton(btOK);

    JPanel p = new JPanel();
    p.add(btOK);
    p.add(new JButton("I am NOT the default."));
    f.getContentPane().add(p);

    f.pack();
    f.setSize(new Dimension(300, 200));

    f.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(800, 600);//  www  .j  a  v a 2  s .com
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    device.setFullScreenWindow(frame);
    device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
    frame.setVisible(true);

    JButton btn = new JButton();
    btn.setText("Button");
    JPanel panel = new JPanel();

    panel.add(btn);
    frame.add(panel);

    btn.addActionListener(e -> {
        JOptionPane.showMessageDialog(frame.getContentPane(), "JOptionPane");
    });
}

From source file:Main.java

public static void main(String[] args) {
    final JTextPane tp = new JTextPane();
    JButton withFocus = new JButton("Select with focus");
    withFocus.addActionListener(e -> {
        tp.selectAll();// www .  ja  v  a  2  s. co m
        tp.requestFocus();
    });
    JButton withOutFocus = new JButton("Select without focus");
    withFocus.addActionListener(e -> tp.selectAll());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(tp));
    JPanel panel = new JPanel();
    panel.add(withFocus);
    panel.add(withOutFocus);
    frame.add(panel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.setUI(new MetalTabbedPaneUI() {
        @Override/*from  ww  w  .j  a  va2s .  c om*/
        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:MaxLengthDocument.java

public static void main(String[] a) {
    Document doc = new MaxLengthDocument(5); // set maximum length to 5
    JTextField field = new JTextField(doc, "", 8);

    JPanel flowPanel = new JPanel();
    flowPanel.add(field);
    JFrame frame = new JFrame("MaxLengthDocument demo");
    frame.setContentPane(flowPanel);/*  w ww . ja  v a 2  s.  c om*/
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(160, 80);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.add(new JTextField(10));
    panel.add(new JButton("button"));
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);/*from ww  w .  j a  v  a  2s .  c o m*/
    frame.pack();
    frame.setVisible(true);

    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowIconified(WindowEvent wEvt) {
            ((JFrame) wEvt.getSource()).dispose();
        }

        @Override
        public void windowDeactivated(WindowEvent wEvt) {
            ((JFrame) wEvt.getSource()).dispose();
        }
    });
}

From source file:Main.java

public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Border Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel p = new JPanel();

    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));
    p.add(new JTextField(10));

    Set<AWTKeyStroke> set = p.getFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS);
    //set = new HashSet(set);
    KeyStroke up = KeyStroke.getKeyStroke("A");
    set.add(up);/*from   www.  ja v  a 2s  . c  o m*/
    System.out.println(set);
    p.setFocusTraversalKeys(KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, set);
    aWindow.add(p);
    aWindow.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    String[] items = { "A", "B", "C" };
    JComboBox<String> comboBox1 = new MyComboBox1<>(items);
    JPanel p = new JPanel();
    p.add(comboBox1);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(p);/*from   w  w  w. j  a va 2  s  .co  m*/
    f.setSize(320, 240);
    f.setVisible(true);
}

From source file:DefaultButton.java

public static void main(String[] a) {
    JDialog f = new JDialog();
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*from w ww.ja  v a  2  s  .c om*/
        }
    });

    JButton btOK = new JButton("Press Enter to click me, I am the default.");
    btOK.setToolTipText("Save and exit");
    f.getRootPane().setDefaultButton(btOK);

    JPanel p = new JPanel();
    p.add(btOK);
    p.add(new JButton("I am NOT the default."));
    f.getContentPane().add(p);

    f.pack();
    f.setSize(new Dimension(300, 200));

    f.show();

}