Example usage for javax.swing JPanel setPreferredSize

List of usage examples for javax.swing JPanel setPreferredSize

Introduction

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

Prototype

@BeanProperty(preferred = true, description = "The preferred size of the component.")
public void setPreferredSize(Dimension preferredSize) 

Source Link

Document

Sets the preferred size of this component.

Usage

From source file:Main.java

public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setPreferredSize(new Dimension(800, 600));

    String test[] = { "alpha", "bravo", "charlie", "delta", "echo", "omega", "zeta" };
    JList<String> list = new JList<>(test);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setLayoutOrientation(JList.VERTICAL);
    list.setVisibleRowCount(5);/*from   w ww.  ja v a 2  s  .c  o  m*/
    list.setBounds(50, 150, 75, 90);

    JScrollPane jScrollPane1 = new JScrollPane();
    jScrollPane1.setViewportView(list);

    panel.add(jScrollPane1);

    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setResizable(false);
    f.setFocusable(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);//from   ww w  .j  av  a 2  s  .c  om

    panel.setBorder(new EmptyBorder(new Insets(10, 20, 30, 40)));
    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);

}

From source file:Main.java

public static void main(String args[]) {
    JPanel container = new ScrollablePanel();
    container.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
    for (int i = 0; i < 20; ++i) {
        JPanel p = new JPanel();
        p.setPreferredSize(new Dimension(50, 50));
        p.add(new JLabel("" + i));
        container.add(p);//from  ww w .ja v a2s  . com
    }

    JScrollPane scroll = new JScrollPane(container);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.getContentPane().add(scroll);
    f.pack();
    f.setSize(250, 300);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel gui = new JPanel(new BorderLayout());
    gui.setPreferredSize(new Dimension(400, 100));

    JDesktopPane dtp = new JDesktopPane();
    gui.add(dtp, BorderLayout.CENTER);

    JButton newFrame = new JButton("Add Frame");

    ActionListener listener = new ActionListener() {
        private int disp = 10;

        @Override//www  . ja v  a  2s  .  c o  m
        public void actionPerformed(ActionEvent e) {
            JInternalFrame jif = new JInternalFrame();
            dtp.add(jif);
            jif.setLocation(disp, disp);
            jif.setSize(100, 100);
            disp += 10;
            jif.setVisible(true);
        }
    };
    newFrame.addActionListener(listener);

    gui.add(newFrame, BorderLayout.PAGE_START);

    JFrame f = new JFrame();
    f.add(gui);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    f.setLocationByPlatform(true);

    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

    JPanel a = new JPanel();
    a.setAlignmentX(Component.CENTER_ALIGNMENT);
    a.setPreferredSize(new Dimension(100, 100));
    a.setMaximumSize(new Dimension(100, 100)); // set max = pref
    a.setBorder(BorderFactory.createTitledBorder("aa"));
    JPanel b = new JPanel();
    b.setAlignmentX(Component.CENTER_ALIGNMENT);
    b.setPreferredSize(new Dimension(50, 50));
    b.setMaximumSize(new Dimension(50, 50)); // set max = pref
    b.setBorder(BorderFactory.createTitledBorder("bb"));

    frame.getContentPane().add(a);/*from  w  w w .j a va2 s. c o m*/
    frame.getContentPane().add(b);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JPanel topPanel = new JPanel();
    topPanel.setPreferredSize(new Dimension(200, 200));
    topPanel.setBackground(Color.WHITE);

    JTextArea chatArea = new JTextArea();
    JScrollPane scrollPane = new JScrollPane(chatArea);

    JPanel mainPanel = new JPanel(new BorderLayout(5, 5));
    mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    mainPanel.add(topPanel, BorderLayout.CENTER);
    mainPanel.add(scrollPane, BorderLayout.SOUTH);

    chatArea.getDocument().addDocumentListener(new DocumentListener() {

        @Override/*from   ww w .j a v  a 2s.co  m*/
        public void insertUpdate(DocumentEvent e) {
            updateLineCount();
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            updateLineCount();
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            updateLineCount();
        }

        private void updateLineCount() {
            int lineCount = chatArea.getLineCount();
            if (lineCount <= 4) {
                chatArea.setRows(lineCount);
                mainPanel.revalidate();
            }
        }
    });

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

From source file:Main.java

public static void main(String[] args) {
    JLabel label = new JLabel("Hello");
    label.setOpaque(true);//from  ww  w .  ja v  a  2 s  .co  m
    label.setBackground(Color.red);

    JPanel bottomPanel = new JPanel(new BorderLayout());
    bottomPanel.add(label, BorderLayout.LINE_END);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
    mainPanel.setPreferredSize(new Dimension(400, 400));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(mainPanel);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JButton btnA = new JButton("A");
    JButton btnB = new JButton("B");

    btnA.setPreferredSize(new Dimension(50, 25));
    btnB.setPreferredSize(new Dimension(100, 25));

    JPanel btnAPanel = new JPanel();
    JPanel btnBPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    btnAPanel.add(btnA);//from  w  w w  . jav a2 s .co m
    btnBPanel.add(btnB);

    JPanel topPanel = new JPanel(new GridLayout(1, 0));
    topPanel.add(new JLabel("hi"));
    topPanel.add(btnAPanel);
    topPanel.add(btnBPanel);

    JPanel mainPanel = new JPanel(new BorderLayout());
    mainPanel.add(topPanel, BorderLayout.NORTH);

    mainPanel.setPreferredSize(new Dimension(400, 300));

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(mainPanel);
    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++;//from   w ww  . ja  v a 2s  .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) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel controlPane = new JPanel();
    JPanel buttonPane = new JPanel();

    controlPane.setLayout(new BoxLayout(controlPane, BoxLayout.PAGE_AXIS));
    controlPane.setPreferredSize(new Dimension(200, 200));
    controlPane.add(new JScrollPane(new JTextArea()));

    buttonPane.setLayout(new FlowLayout(FlowLayout.LEFT));
    buttonPane.setPreferredSize(new Dimension(100, 40));
    buttonPane.add(new JButton("Button1"));
    buttonPane.add(new JButton("Button2"));

    frame.add(controlPane, BorderLayout.NORTH);
    frame.add(buttonPane, BorderLayout.SOUTH);

    frame.pack();/*from   w w w. j a v a  2 s . c om*/
    frame.setVisible(true);
}