Example usage for javax.swing JPanel setLayout

List of usage examples for javax.swing JPanel setLayout

Introduction

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

Prototype

public void setLayout(LayoutManager mgr) 

Source Link

Document

Sets the layout manager for this container.

Usage

From source file:de.codesourcery.eve.skills.ui.components.AbstractEditorComponent.java

@Override
protected final JPanel createPanel() {

    final JPanel result = new JPanel();
    result.setLayout(new BorderLayout());

    result.add(createPanelHook(), BorderLayout.NORTH);

    final JPanel buttonPanel = new JPanel();
    populateButtonPanel(buttonPanel);/*www.java  2  s .  com*/
    result.add(buttonPanel, BorderLayout.SOUTH);
    return result;
}

From source file:EditorPaneExample4.java

public EditorPaneExample4() {
    super("JEditorPane Example 4");

    pane = new JEditorPane();
    pane.setEditable(false); // Read-only
    getContentPane().add(new JScrollPane(pane), "Center");

    // Build the panel of controls
    JPanel panel = new JPanel();

    panel.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();
    c.gridwidth = 1;// ww w.j av  a  2 s . c  om
    c.gridheight = 1;
    c.anchor = GridBagConstraints.EAST;
    c.fill = GridBagConstraints.NONE;
    c.weightx = 0.0;
    c.weighty = 0.0;

    JLabel urlLabel = new JLabel("URL: ", JLabel.RIGHT);
    panel.add(urlLabel, c);
    JLabel loadingLabel = new JLabel("State: ", JLabel.RIGHT);
    c.gridy = 1;
    panel.add(loadingLabel, c);
    JLabel typeLabel = new JLabel("Type: ", JLabel.RIGHT);
    c.gridy = 2;
    panel.add(typeLabel, c);

    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 1;
    c.weightx = 1.0;
    c.fill = GridBagConstraints.HORIZONTAL;

    textField = new JTextField(32);
    panel.add(textField, c);
    loadingState = new JLabel(spaces, JLabel.LEFT);
    loadingState.setForeground(Color.black);
    c.gridy = 1;
    panel.add(loadingState, c);
    loadedType = new JLabel(spaces, JLabel.LEFT);
    loadedType.setForeground(Color.black);
    c.gridy = 2;
    panel.add(loadedType, c);

    getContentPane().add(panel, "South");

    // Change page based on text field
    textField.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            String url = textField.getText();

            try {
                // Check if the new page and the old
                // page are the same.
                URL newURL = new URL(url);
                URL loadedURL = pane.getPage();
                if (loadedURL != null && loadedURL.sameFile(newURL)) {
                    return;
                }

                // Try to display the page
                textField.setEnabled(false); // Disable input
                textField.paintImmediately(0, 0, textField.getSize().width, textField.getSize().height);
                setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
                // Busy cursor
                loadingState.setText("Loading...");
                loadingState.paintImmediately(0, 0, loadingState.getSize().width,
                        loadingState.getSize().height);
                loadedType.setText("");
                loadedType.paintImmediately(0, 0, loadedType.getSize().width, loadedType.getSize().height);
                pane.setPage(url);

                loadedType.setText(pane.getContentType());
            } catch (Exception e) {
                System.out.println(e);
                JOptionPane.showMessageDialog(pane, new String[] { "Unable to open file", url },
                        "File Open Error", JOptionPane.ERROR_MESSAGE);
                loadingState.setText("Failed");
                textField.setEnabled(true);
                setCursor(Cursor.getDefaultCursor());
            }
        }
    });

    // Listen for page load to complete
    pane.addPropertyChangeListener(new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("page")) {
                loadingState.setText("Page loaded.");
                textField.setEnabled(true); // Allow entry of new URL
                setCursor(Cursor.getDefaultCursor());
            }
        }
    });
}

From source file:events.TableListSelectionDemo.java

public TableListSelectionDemo() {
    super(new BorderLayout());

    String[] columnNames = { "French", "Spanish", "Italian" };
    String[][] tableData = { { "un", "uno", "uno" }, { "deux", "dos", "due" }, { "trois", "tres", "tre" },
            { "quatre", "cuatro", "quattro" }, { "cinq", "cinco", "cinque" }, { "six", "seis", "sei" },
            { "sept", "siete", "sette" } };

    table = new JTable(tableData, columnNames);
    listSelectionModel = table.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    table.setSelectionModel(listSelectionModel);
    JScrollPane tablePane = new JScrollPane(table);

    //Build control area (use default FlowLayout).
    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);/* w  ww. j  ava2  s. c o m*/
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    //Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    //Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    JPanel tableContainer = new JPanel(new GridLayout(1, 1));
    tableContainer.setBorder(BorderFactory.createTitledBorder("Table"));
    tableContainer.add(tablePane);
    tablePane.setPreferredSize(new Dimension(420, 130));
    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(250, 50));
    topHalf.setPreferredSize(new Dimension(200, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    //XXX: next line needed if bottomHalf is a scroll pane:
    //bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 110));
    splitPane.add(bottomHalf);
}

From source file:de.codesourcery.eve.skills.ui.components.impl.PriceInfoComponent.java

@Override
protected JPanel createPanelHook() {

    final JPanel panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    final String kind;

    switch (priceType) {
    case BUY:/* w  w  w. j  av a 2  s.c  o  m*/
        kind = "buy";
        break;
    case SELL:
    default:
        throw new RuntimeException(
                "Invalid data in switch/case: PriceInfo.Type.ALL cannot be queried from user");
    }

    minPrice.addActionListener(listener);
    maxPrice.addActionListener(listener);
    avgPrice.addActionListener(listener);

    final TextArea desc = new TextArea();
    desc.setText(message);
    desc.setEditable(false);

    panel.add(desc, constraints(0, 0).width(2).resizeBoth().end());

    panel.add(new JLabel("Minimum " + kind + " price"), constraints(0, 1).useRelativeWidth().end());
    panel.add(minPrice, constraints(1, 1).useRemainingWidth().end());

    panel.add(new JLabel("Average " + kind + " price"), constraints(0, 2).useRelativeWidth().end());
    panel.add(avgPrice, constraints(1, 2).useRemainingWidth().end());

    panel.add(new JLabel("Maximum " + kind + " price"), constraints(0, 3).useRelativeWidth().end());
    panel.add(maxPrice, constraints(1, 3).useRemainingWidth().end());

    return panel;
}

From source file:ListSelectionDemo.java

public ListSelectionDemo() {
    super(new BorderLayout());

    String[] listData = { "one", "two", "three", "four", "five", "six", "seven" };
    String[] columnNames = { "French", "Spanish", "Italian" };
    list = new JList(listData);

    listSelectionModel = list.getSelectionModel();
    listSelectionModel.addListSelectionListener(new SharedListSelectionHandler());
    JScrollPane listPane = new JScrollPane(list);

    JPanel controlPane = new JPanel();
    String[] modes = { "SINGLE_SELECTION", "SINGLE_INTERVAL_SELECTION", "MULTIPLE_INTERVAL_SELECTION" };

    final JComboBox comboBox = new JComboBox(modes);
    comboBox.setSelectedIndex(2);/*ww  w  .j  ava 2 s.  co m*/
    comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String newMode = (String) comboBox.getSelectedItem();
            if (newMode.equals("SINGLE_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            } else if (newMode.equals("SINGLE_INTERVAL_SELECTION")) {
                listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
            } else {
                listSelectionModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            }
            output.append("----------" + "Mode: " + newMode + "----------" + newline);
        }
    });
    controlPane.add(new JLabel("Selection mode:"));
    controlPane.add(comboBox);

    // Build output area.
    output = new JTextArea(1, 10);
    output.setEditable(false);
    JScrollPane outputPane = new JScrollPane(output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    // Do the layout.
    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    add(splitPane, BorderLayout.CENTER);

    JPanel topHalf = new JPanel();
    topHalf.setLayout(new BoxLayout(topHalf, BoxLayout.LINE_AXIS));
    JPanel listContainer = new JPanel(new GridLayout(1, 1));
    listContainer.setBorder(BorderFactory.createTitledBorder("List"));
    listContainer.add(listPane);

    topHalf.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
    topHalf.add(listContainer);
    // topHalf.add(tableContainer);

    topHalf.setMinimumSize(new Dimension(100, 50));
    topHalf.setPreferredSize(new Dimension(100, 110));
    splitPane.add(topHalf);

    JPanel bottomHalf = new JPanel(new BorderLayout());
    bottomHalf.add(controlPane, BorderLayout.PAGE_START);
    bottomHalf.add(outputPane, BorderLayout.CENTER);
    // XXX: next line needed if bottomHalf is a scroll pane:
    // bottomHalf.setMinimumSize(new Dimension(400, 50));
    bottomHalf.setPreferredSize(new Dimension(450, 135));
    splitPane.add(bottomHalf);
}

From source file:ArcApp.java

public ArcApp() {
    super(new BorderLayout());
    canvas = new MyCanvas();
    int width = 600;
    int height = 55;
    sliderX = setSlider(0, width, width / 4, width / 2, width / 4);
    sliderY = setSlider(0, height, height / 4, height / 2, height / 4);
    sliderWidth = setSlider(0, width, width / 2, width / 2, width / 4);
    sliderHeight = setSlider(0, height, height / 2, height / 2, height / 4);
    sliderT0 = setSlider(0, 360, 45, 180, 45);
    sliderT = setSlider(0, 360, 135, 180, 45);

    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayout(3, 3));
    panel1.add(new JLabel("Location (x,y): ", JLabel.RIGHT));
    panel1.add(sliderX);/*from ww  w. ja v a  2s  .c o m*/
    panel1.add(sliderY);
    panel1.add(new JLabel("Size (w,h): ", JLabel.RIGHT));
    panel1.add(sliderWidth);
    panel1.add(sliderHeight);
    panel1.add(new JLabel("Angles (Th0, Th): ", JLabel.RIGHT));
    panel1.add(sliderT0);
    panel1.add(sliderT);

    add(panel1, BorderLayout.NORTH);

    arcBox = new JComboBox(arcLabels);
    arcBox.setSelectedIndex(0);
    arcBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    arcBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            canvas.arcType = arcTypes[cb.getSelectedIndex()];
            canvas.repaint();
        }
    });

    fillBox = new JComboBox(colorLabels);
    fillBox.setSelectedIndex(0);
    fillBox.setAlignmentX(Component.LEFT_ALIGNMENT);
    fillBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JComboBox cb = (JComboBox) e.getSource();
            canvas.fillColor = colors[cb.getSelectedIndex()];
            canvas.repaint();
        }
    });

    JPanel panel2 = new JPanel();
    panel2.setLayout(new GridLayout(1, 4));
    panel2.add(new JLabel("Arc Type: ", JLabel.RIGHT));
    panel2.add(arcBox);
    panel2.add(new JLabel("Fill Type: ", JLabel.RIGHT));
    panel2.add(fillBox);

    add(panel2, BorderLayout.SOUTH);
    add(canvas, BorderLayout.CENTER);
}

From source file:org.cds06.speleograph.graph.DateAxisEditor.java

public DateAxisEditor(DateAxis dateAxis) {
    super(SpeleoGraphApp.getInstance(), true);
    this.axis = dateAxis;
    this.setTitle(I18nSupport.translate("graph.dateAxisEditor"));
    JPanel panel = new JPanel();
    panel.setLayout(
            new FormLayout("r:p,4dlu,p:grow,4dlu", "p:grow,p,4dlu:grow,p,4dlu:grow,p,4dlu:grow,p,p:grow"));
    CellConstraints cc = new CellConstraints();
    panel.add(new JLabel("Format :"), cc.xy(1, 2));
    panel.add(dateSelector, cc.xy(3, 2));
    panel.add(new JLabel("Date dbut :"), cc.xy(1, 4));
    panel.add(minDate, cc.xy(3, 4));/*w  w  w  . j a  va2s. c o  m*/
    panel.add(new JLabel("Date fin :"), cc.xy(1, 6));
    panel.add(maxDate, cc.xy(3, 6));

    ButtonBarBuilder barBuilder = new ButtonBarBuilder();
    barBuilder.addGlue();

    //Cancel button
    barBuilder.addButton(new AbstractAction() {

        {
            putValue(NAME, I18nSupport.translate("cancel"));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            setVisible(false);
        }
    });

    //Ok button
    barBuilder.addButton(new AbstractAction() {

        {
            putValue(NAME, I18nSupport.translate("ok"));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            axis.setDateFormatOverride((DateFormat) dateSelector.getSelectedItem());
            axis.setMinimumDate(minDate.getDate());
            axis.setMaximumDate(maxDate.getDate());
            setVisible(false);
        }
    });

    panel.add(barBuilder.build(), cc.xyw(1, 8, 3));

    minDate.setDate(dateAxis.getMinimumDate());
    maxDate.setDate(dateAxis.getMaximumDate());
    if (dateAxis.getDateFormatOverride() != null
            && dateAxis.getDateFormatOverride() instanceof HumanSimpleDateFormat) {
        dateSelector.setSelectedItem(dateAxis.getDateFormatOverride());
    }

    setContentPane(panel);
    panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

    setSize(panel.getPreferredSize().width + 100, panel.getPreferredSize().height + 100);

}

From source file:de.huxhorn.lilith.swing.LicenseAgreementDialog.java

private void initUI() {
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

    JTextPane licenseTextPane = new JTextPane();
    HTMLEditorKit htmlEditorKit = new HTMLEditorKit();

    String licenseText = null;//from ww  w  . j  av  a 2  s .  co m
    InputStream licenseStream = LicenseAgreementDialog.class.getResourceAsStream("/licenses/license.html");
    if (licenseStream != null) {
        try {
            licenseText = IOUtils.toString(licenseStream, StandardCharsets.UTF_8);
        } catch (IOException ex) {
            if (logger.isErrorEnabled())
                logger.error("Exception while loading license!", ex);
            IOUtilities.interruptIfNecessary(ex);
        } finally {
            IOUtilities.closeQuietly(licenseStream);
        }
    }
    if (licenseText == null) {
        if (logger.isErrorEnabled())
            logger.error("Couldn't find license text! Exiting!");
        System.exit(-1);
    }

    licenseTextPane.setEditorKit(htmlEditorKit);
    licenseTextPane.setEditable(false);
    licenseTextPane.setText(licenseText);
    JScrollPane licenseScrollPane = new JScrollPane(licenseTextPane);
    licenseScrollPane.setPreferredSize(new Dimension(640, 480));

    content.add(licenseScrollPane, BorderLayout.CENTER);
    content.add(buttonPanel, BorderLayout.SOUTH);

    AcceptAction acceptAction = new AcceptAction();
    DeclineAction declineAction = new DeclineAction();

    JButton acceptButton = new JButton(acceptAction);
    JButton declineButton = new JButton(declineAction);

    buttonPanel.add(acceptButton);
    buttonPanel.add(declineButton);
    setContentPane(content);
    licenseTextPane.setCaretPosition(0);
    declineButton.requestFocusInWindow();
}

From source file:BorderDemo.java

void addCompForBorder(Border border, String description, Container container) {
    JPanel comp = new JPanel(false);
    JLabel label = new JLabel(description, JLabel.CENTER);
    comp.setLayout(new GridLayout(1, 1));
    comp.add(label);/*from  w  ww . j a v a 2 s.  co  m*/
    comp.setBorder(border);

    container.add(Box.createRigidArea(new Dimension(0, 10)));
    container.add(comp);
}

From source file:QandE.Test1.java

public Component createComponents() {
    final JLabel label = new JLabel(labelText);

    /*//from  w  w w . j  av a2  s . com
     * An easy way to put space between a top-level container
     * and its contents is to put the contents in a JPanel
     * that has an "empty" border.
     */
    JPanel pane = new JPanel();
    pane.setBorder(BorderFactory.createEmptyBorder(30, //top
            30, //left
            10, //bottom
            30) //right
    );
    pane.setLayout(new GridLayout(0, 1));
    pane.add(label);

    return pane;
}