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

private static Container makeIt(String title, float alignment) {
    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    container.setLayout(layout);

    for (int i = 0, n = labels.length; i < n; i++) {
        JButton button = new JButton(labels[i]);
        button.setAlignmentX(alignment);
        container.add(button);//from  w  w w.j  a va 2s .  com
    }
    return container;
}

From source file:XAxisAlignY.java

private static Container makeIt(String title, float alignment) {
    String labels[] = { "--", "--", "--" };

    JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS);
    container.setLayout(layout);

    for (int i = 0, n = labels.length; i < n; i++) {
        JButton button = new JButton(labels[i]);
        button.setAlignmentY(alignment);
        container.add(button);// www  . ja  va  2  s .  c  om
    }
    return container;
}

From source file:Main.java

public static boolean showModalDialogOnEDT(JComponent contents, String title, String okButtonMessage,
        String cancelButtonMessage, ModalityType modalityType, final boolean systemExitOnDisposed) {

    class Result {
        boolean OK = false;
    }/*from  ww w. ja v a  2s  . co  m*/
    final Result result = new Result();

    final JDialog dialog = new JDialog((Frame) null, title, true) {
        @Override
        public void dispose() {
            super.dispose();
            if (systemExitOnDisposed) {
                System.exit(0);
            }
        }
    };

    // ensure it doesn't block other dialogs
    if (modalityType != null) {
        dialog.setModalityType(modalityType);
    }

    // See http://stackoverflow.com/questions/1343542/how-do-i-close-a-jdialog-and-have-the-window-event-listeners-be-notified
    dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

    JPanel buttonPane = new JPanel();
    buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
    if (okButtonMessage != null) {
        buttonPane.add(new JButton(new AbstractAction(okButtonMessage) {

            @Override
            public void actionPerformed(ActionEvent e) {
                result.OK = true;
                dialog.dispose();
            }
        }));
    }

    if (cancelButtonMessage != null) {
        buttonPane.add(new JButton(new AbstractAction(cancelButtonMessage) {

            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        }));
    }

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    panel.setLayout(new BorderLayout());
    panel.add(contents, BorderLayout.CENTER);
    panel.add(buttonPane, BorderLayout.SOUTH);

    // startup frame
    dialog.getContentPane().add(panel);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.pack();

    // This hopefully centres the dialog even though the parameter is null 
    // see http://stackoverflow.com/questions/213266/how-do-i-center-a-jdialog-on-screen
    dialog.setLocationRelativeTo(null);
    dialog.setVisible(true);

    return result.OK;
}

From source file:YAxisAlignXButtonMixed.java

private static Container makeIt(String title) {
    JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    container.setLayout(layout);

    AButton button = new AButton("0.0", 0.0f);
    container.add(button);//from w  ww  . j a v  a2s  . c om
    button = new AButton(".25", .25f);
    container.add(button);
    button = new AButton(".50", .50f);
    container.add(button);
    button = new AButton(".75", .75f);
    container.add(button);
    button = new AButton("1.0", 1.0f);
    container.add(button);
    return container;
}

From source file:components.ListDialogRunner.java

public static JPanel createUI() {
    //Create the labels.
    JLabel intro = new JLabel("The chosen name:");
    final JLabel name = new JLabel(names[1]);
    intro.setLabelFor(name);/*  w ww. j  a v  a 2 s. co  m*/

    //Use a wacky font if it exists. If not, this falls
    //back to a font we know exists.
    name.setFont(getAFont());

    //Create the button.
    final JButton button = new JButton("Pick a new name...");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String selectedName = ListDialog.showDialog(frame, button, "Baby names ending in O:",
                    "Name Chooser", names, name.getText(), "Cosmo  ");
            name.setText(selectedName);
        }
    });

    //Create the panel we'll return and set up the layout.
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
    intro.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    name.setAlignmentX(JComponent.CENTER_ALIGNMENT);
    button.setAlignmentX(JComponent.CENTER_ALIGNMENT);

    //Add the labels to the content pane.
    panel.add(intro);
    panel.add(Box.createVerticalStrut(5)); //extra space
    panel.add(name);

    //Add a vertical spacer that also guarantees us a minimum width:
    panel.add(Box.createRigidArea(new Dimension(150, 10)));

    //Add the button.
    panel.add(button);

    return panel;
}

From source file:Main.java

public static JPanel createPainelContainer(String title, LayoutManager layout, Component... components) {
    final JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));

    if (layout != null)
        container.setLayout(layout);

    for (Component c : components) {
        container.add(c);/*from  w ww .  j  a va2  s . co m*/
    }

    return container;

}

From source file:Main.java

/**
 * Initialises the {@link JDialog} for the {@link JComponent}.
 * //from w  ww  .ja  va 2 s. co m
 * @param dialog
 * @param component
 * @param parentComponent
 */
private static void initDialog(final JDialog dialog, final JComponent component,
        final Component parentComponent) {
    dialog.setResizable(true);
    dialog.setComponentOrientation(component.getComponentOrientation());
    Container contentPane = dialog.getContentPane();

    contentPane.setLayout(new BorderLayout());
    contentPane.add(component, BorderLayout.CENTER);

    final int buttonWidth = 75;

    final JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 4, 4));

    buttonPanel.add(Box.createHorizontalGlue());

    @SuppressWarnings("serial")
    final Action closeAction = new AbstractAction("Close") {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.dispose();
        }
    };

    final JButton button = new JButton(closeAction);
    fixWidth(button, buttonWidth);
    buttonPanel.add(button);

    contentPane.add(buttonPanel, BorderLayout.SOUTH);

    if (JDialog.isDefaultLookAndFeelDecorated()) {
        boolean supportsWindowDecorations = UIManager.getLookAndFeel().getSupportsWindowDecorations();
        if (supportsWindowDecorations) {
            dialog.setUndecorated(true);
            component.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        }
    }
    dialog.pack();
    dialog.setLocationRelativeTo(parentComponent);
    WindowAdapter adapter = new WindowAdapter() {
        //         private boolean gotFocus = false;
        public void windowClosing(WindowEvent we) {
            fireAction(we.getSource(), closeAction, "close");
        }
    };
    dialog.addWindowListener(adapter);
    dialog.addWindowFocusListener(adapter);
}

From source file:EdgeLayoutExample.java

public static JPanel createPanel() {
    JPanel outerPanel = new JPanel();
    outerPanel.setLayout(new EdgeLayout());
    outerPanel.add(new JButton("West1"), EdgeLayout.WEST);
    outerPanel.add(new JButton("North1"), EdgeLayout.NORTH);
    outerPanel.add(new JButton("West2"), EdgeLayout.WEST);
    outerPanel.add(new JButton("North2"), EdgeLayout.NORTH);
    outerPanel.add(new JButton("East1"), EdgeLayout.EAST);
    outerPanel.add(new JButton("South1"), EdgeLayout.SOUTH);
    outerPanel.add(new JButton("West3"), EdgeLayout.WEST);
    outerPanel.add(new JButton("West4"), EdgeLayout.WEST);
    outerPanel.add(new JButton("South2"), EdgeLayout.SOUTH);
    outerPanel.add(new JButton("South3"), EdgeLayout.SOUTH);
    outerPanel.add(new JButton("Center1"), EdgeLayout.CENTER);
    return outerPanel;
}

From source file:FindHullWindowLogic.java

static void drawPointsOnChart(JPanel panelWhenInside, ArrayList<Point2D> convexHull) {
    panelWhenInside.removeAll();/*from   www.j a  v a2s .c  o m*/
    panelWhenInside.setLayout(new java.awt.BorderLayout());
    XYSeries seriersAllPoints = new XYSeries("All points");
    addPointsToSeries(seriersAllPoints);

    int pairsNumber = 0;
    if (convexHull != null)
        pairsNumber = convexHull.size() - 1;
    XYSeries covnexHullDivideOnPiars[] = new XYSeries[pairsNumber];

    for (int i = 0; i < covnexHullDivideOnPiars.length; i++) {
        covnexHullDivideOnPiars[i] = new XYSeries("Convex hull pair " + i);
    }

    if (convexHull != null) {
        divideOnPairsAndConvertConvexHullIntoSeries(covnexHullDivideOnPiars, convexHull);
    }

    // Add the seriersAllPoints to your data set
    XYSeriesCollection dataset = new XYSeriesCollection();
    dataset.addSeries(seriersAllPoints);

    for (int i = 0; i < covnexHullDivideOnPiars.length; i++) {
        dataset.addSeries(covnexHullDivideOnPiars[i]);
    }

    // Generate the graph
    JFreeChart chart = ChartFactory.createXYLineChart(null, // Title
            null, // x-axis Label
            null, // y-axis Label
            dataset, // Dataset
            PlotOrientation.VERTICAL, // Plot Orientation
            false, // Show Legend
            false, // Use tooltips
            false // Configure chart to generate URLs?
    );

    final XYPlot plot = chart.getXYPlot();
    ChartPanel chartPanel = new ChartPanel(chart);
    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
    renderer.setSeriesPaint(0, Color.BLACK);
    renderer.setSeriesLinesVisible(0, false);
    renderer.setSeriesShape(0, ShapeUtilities.createDiamond(3));

    for (int i = 1; i <= covnexHullDivideOnPiars.length; i++) {
        renderer.setSeriesPaint(i, Color.red);
        renderer.setSeriesLinesVisible(i, true);
        renderer.setSeriesStroke(i, new BasicStroke(1.0f));
    }

    plot.setRenderer(renderer);

    panelWhenInside.add(chartPanel, BorderLayout.CENTER);
    panelWhenInside.validate();
}

From source file:com.surenpi.autotest.suite.SuiteRunnerLauncher.java

private static void gui(List<URL> urlList) {
    JFrame frame = new JFrame("PhoenixFramework");

    JPanel centerPanel = new JPanel();
    frame.add(centerPanel);//  w w  w.  j  ava2  s  . co  m
    centerPanel.setLayout(new BorderLayout());

    createItemsPanel(centerPanel, urlList);
    createButPanel(centerPanel);

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setSize(600, 400);
}