Example usage for javax.swing JPanel getLayout

List of usage examples for javax.swing JPanel getLayout

Introduction

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

Prototype

public LayoutManager getLayout() 

Source Link

Document

Gets the layout manager for this container.

Usage

From source file:Provider.GoogleMapsStatic.TestUI.MySample.java

/**
 * @author Nazmul//from w w  w  .  j  av a 2 s. c  o m
 */
private void initComponents() {
    // JFormDesigner - Component initialization - DO NOT MODIFY
    // //GEN-BEGIN:initComponents
    // Generated using JFormDesigner non-commercial license
    ttfSizeW = new JTextField("512");
    ttfLat = new JTextField("45.5");
    btnGetMap = new JButton("Get Map");
    btnQuit = new JButton("Quit");
    ttfSizeH = new JTextField("512");
    ttfLon = new JTextField("-73.55");
    ttfZoom = new JTextField("14");
    ttaStatus = new JTextArea();
    checkboxRecvStatus = new JCheckBox();
    checkboxSendStatus = new JCheckBox();
    ttfProgressMsg = new JTextField();
    progressBar = new JProgressBar();
    imgLbl = new JLabel();

    /***
     * @author Dhgiang, jpmolinamatute
     * Created a slider, zoom in/out buttons, conbo box (drop down listbox) for city selection,
     * a panel to group the zoom buttons and the slider bar
     */
    slider = new JSlider(0, 19, 14);
    controlPanel = new JPanel(new GridBagLayout()); // controlPanel was created by jpmolinamatute 
    cities = new JComboBox<Object>(new String[] { "Montreal", "Toronto", // the place setting where the combo box is now used to be a text field
            "Vancouver", "New York City", "Caracas", "Hong Kong" }); // for license key, but it was removed to accommodate space for combo box 
    // @author Dhgiang

    JPanel panel1 = new JPanel();
    JPanel contentPanel = new JPanel();
    JPanel btnPanel = new JPanel();
    JPanel dialogPane = new JPanel();

    JLabel label1 = new JLabel("Select City"); // this used to be the label for license key, it was changed to label as 'select city'
    JLabel label2 = new JLabel("Size Width");
    JLabel label3 = new JLabel("Size Height");
    JLabel label4 = new JLabel("Latitude");
    JLabel label5 = new JLabel("Longitude");
    JLabel label6 = new JLabel("Zoom");

    JButton btnZoomIn = new JButton("-");
    JButton btnZoomOut = new JButton("+");

    /***
     * @author jpmolinamatute
     * Created 8 cardinal points: N,NE,NW; S,SE,SW; E, W; 
     */
    JButton btnSE = new JButton("SE");
    JButton btnS = new JButton("S");
    JButton btnSW = new JButton("SW");
    JButton btnE = new JButton("E");
    JButton btnW = new JButton("W");
    JButton btnNE = new JButton("NE");
    JButton btnN = new JButton("N");
    JButton btnNW = new JButton("NW");

    /*** 
     * @author JPMolinaMatute
     * Creaetd a spaceControl GridBagConstraints object to manage 
     * the cardinal points, and add KeyListener so users can use the
     * 8 cardinal keys on the num pad; initialize dimensions
     * 
     */
    GridBagConstraints spaceControl = new GridBagConstraints();
    spaceControl.insets = new Insets(5, 5, 5, 5);
    controlPanel.addKeyListener(this);
    controlPanel.setSize(new Dimension(512, 512));
    // ---- My Combo Boxes for different city coordinates ----//

    // ======== this ========
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    setTitle("Google Static Maps");
    setIconImage(null);
    Container contentPane = getContentPane();
    contentPane.setLayout(new BorderLayout());

    // ======== dialogPane ========
    {
        dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
        dialogPane.setOpaque(false);
        dialogPane.setLayout(new BorderLayout());

        // ======== contentPanel ========
        {
            contentPanel.setOpaque(false);
            contentPanel.setLayout(new TableLayout(
                    new double[][] { { TableLayoutConstants.FILL }, { TableLayoutConstants.PREFERRED,
                            TableLayoutConstants.FILL, TableLayoutConstants.PREFERRED } }));
            ((TableLayout) contentPanel.getLayout()).setHGap(5);
            ((TableLayout) contentPanel.getLayout()).setVGap(5);

            // ======== panel1 ========
            {
                panel1.setOpaque(false);
                panel1.setBorder(new CompoundBorder(
                        new TitledBorder("Configure the inputs to Google Static Maps"), Borders.DLU2_BORDER));
                panel1.setLayout(new TableLayout(
                        new double[][] { { 0.17, 0.17, 0.17, 0.17, 0.05, TableLayoutConstants.FILL },
                                { TableLayoutConstants.PREFERRED, TableLayoutConstants.PREFERRED,
                                        TableLayoutConstants.PREFERRED } }));
                ((TableLayout) panel1.getLayout()).setHGap(5);
                ((TableLayout) panel1.getLayout()).setVGap(5);

                label1.setHorizontalAlignment(SwingConstants.RIGHT);
                panel1.add(label1, new TableLayoutConstraints(0, 2, 0, 2, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));
                // ---- label2 ----
                label2.setHorizontalAlignment(SwingConstants.RIGHT);
                panel1.add(label2, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- ttfSizeW ----

                panel1.add(ttfSizeW, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- label4 ----

                label4.setHorizontalAlignment(SwingConstants.RIGHT);
                panel1.add(label4, new TableLayoutConstraints(2, 0, 2, 0, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- ttfLat ----

                panel1.add(ttfLat, new TableLayoutConstraints(3, 0, 3, 0, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- btnGetMap ----
                btnGetMap.setHorizontalAlignment(SwingConstants.LEFT);
                btnGetMap.setMnemonic('G');
                btnGetMap.setActionCommand("getMap");
                btnGetMap.addActionListener(this);
                panel1.add(btnGetMap, new TableLayoutConstraints(5, 0, 5, 0, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- label3 ----
                label3.setHorizontalAlignment(SwingConstants.RIGHT);
                panel1.add(label3, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- ttfSizeH ----

                panel1.add(ttfSizeH, new TableLayoutConstraints(1, 1, 1, 1, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- label5 ----
                label5.setHorizontalAlignment(SwingConstants.RIGHT);
                panel1.add(label5, new TableLayoutConstraints(2, 1, 2, 1, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- ttfLon ----

                panel1.add(ttfLon, new TableLayoutConstraints(3, 1, 3, 1, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- btnQuit ----
                btnQuit.setMnemonic('Q');
                btnQuit.setHorizontalAlignment(SwingConstants.LEFT);
                btnQuit.setHorizontalTextPosition(SwingConstants.RIGHT);
                btnQuit.setActionCommand("quit");
                btnQuit.addActionListener(this);

                panel1.add(btnQuit, new TableLayoutConstraints(5, 1, 5, 1, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                /***
                 * @author Dhgiang
                 * Added an anonymous inner class for ItemLister and event handling for the combo box
                 * Used the switch case condition handling to set coordinates based on the city selected
                 * Juan helped modified this method by adding the startTaskAction() at the end so 
                 * users don't have to click on Get Map button.
                 */

                cities.setSelectedIndex(0); // initialize the city selection item to 0 (or the first item)      

                cities.addItemListener(new ItemListener() {
                    public void itemStateChanged(ItemEvent e) {
                        Integer z = cities.getSelectedIndex();
                        switch (z) {
                        case 0:
                            ttfLat.setText("45.5");
                            ttfLon.setText("-73.55");
                            break;
                        case 1:
                            ttfLat.setText("43.65");
                            ttfLon.setText("-79.38");
                            break;
                        case 2:
                            ttfLat.setText("49.2505");
                            ttfLon.setText("-123.1119");
                            break;
                        case 3:
                            ttfLat.setText("40.7142");
                            ttfLon.setText("-74.0064");
                            break;
                        case 4:
                            ttfLat.setText("10.4901");
                            ttfLon.setText("-66.9151");
                            break;
                        case 5:
                            ttfLat.setText("22.257");
                            ttfLon.setText("114.2");
                            break;
                        default:
                            break;
                        }
                        startTaskAction();
                    }
                });

                /***
                 * @author Dhgiang
                 * Added the combo box to the panel
                 */
                panel1.add(cities, new TableLayoutConstraints(1, 2, 1, 2, TableLayoutConstraints.FULL,
                        TableLayoutConstraints.FULL));

                // ---- label6 ----
                label6.setHorizontalAlignment(SwingConstants.RIGHT);
                panel1.add(label6, new TableLayoutConstraints(2, 2, 2, 2, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));

                // ---- ttfZoom ----

                panel1.add(ttfZoom, new TableLayoutConstraints(3, 2, 3, 2, TableLayoutConstants.FULL,
                        TableLayoutConstants.FULL));
            }
            {
                btnPanel.setOpaque(false);
                btnPanel.setLayout(new GridLayout(0, 3));

                /****
                 * @author Dhgiang
                 * Initializing the zoom IN / OUT buttons with proper parameters and
                 * adding them to the btnPanel of Panel1 (panel within a panel)
                 */
                // ---- btnZoomIn ----
                btnZoomIn.setHorizontalAlignment(SwingConstants.LEFT);
                btnZoomIn.setHorizontalTextPosition(SwingConstants.RIGHT);
                btnZoomIn.setActionCommand("Zoomin");
                btnZoomIn.addActionListener(this);
                btnPanel.add(btnZoomIn, new TableLayoutConstraints(5, 2, 5, 2, TableLayoutConstraints.FULL,
                        TableLayoutConstraints.FULL));

                // ---- btnZoomOut ----
                btnZoomOut.setHorizontalAlignment(SwingConstants.RIGHT);
                btnZoomOut.setHorizontalTextPosition(SwingConstants.RIGHT);
                btnZoomOut.setActionCommand("Zoomout");
                btnZoomOut.addActionListener(this);

                btnPanel.add(btnZoomOut, new TableLayoutConstraints(5, 2, 5, 2, TableLayoutConstraints.FULL,
                        TableLayoutConstraints.FULL));

                /***
                 * @author Dhgiang
                 * Having created a new JSlider slider object, maximum & minimum values 
                 * are initialized along with incremental values
                 * 
                 */
                // ---- slider -----
                slider.setMaximum(19);
                slider.setMinimum(0);
                slider.setPaintTicks(true);
                slider.setMajorTickSpacing(19);
                slider.setMinorTickSpacing(1);
                slider.setPaintTrack(false);
                slider.createStandardLabels(4, 0);

                /***
                 * @author Dhgiang
                 * Added a ChangeListener to the slider so that 
                 * 1) the slider moves left if (-) button is clicked, slider moves right if (+) is clicked
                 * 2) and zoom values will increase or decrease if slider bar is shifted left or right accordingly
                 * 3) after the user releases the click button, the map will display automatically with the values
                 *    set by the slider bar, without having the user to click on get map button
                 */
                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent arg0) {
                        Integer a = slider.getValue();

                        if (a >= 0 && a <= 19) {
                            ttfZoom.setText(a.toString());
                            startTaskAction();
                        }

                    }

                });
                slider.setBorder(null);

                /***
                 * @author Dhgiang
                 * Added the slider bar to the button panel
                 */
                btnPanel.add(slider, new TableLayoutConstraints(5, 2, 5, 2, TableLayoutConstraints.FULL,
                        TableLayoutConstraints.FULL));
            }

            /***
             * @author Dhgiang
             * Adding the button panel to panel1
             */
            panel1.add(btnPanel, new TableLayoutConstraints(5, 2, 5, 2, TableLayoutConstraints.FULL,
                    TableLayoutConstraints.FULL));
            contentPanel.add(panel1, new TableLayoutConstraints(0, 0, 0, 0, TableLayoutConstants.FULL,
                    TableLayoutConstants.FULL));

            /***
             * @author jpmolinamatute
             * Initializing coordinates for the cardinal points
             * Adding the cardinal points button to the control panel
             */
            // ---- Cardinals points -----
            btnNW.setActionCommand("Northwest");
            btnNW.addActionListener(this);
            spaceControl.gridx = 0;
            spaceControl.gridy = 0;
            controlPanel.add(btnNW, spaceControl);

            btnN.setActionCommand("North");
            btnN.addActionListener(this);
            spaceControl.gridx = 3;
            spaceControl.gridy = 0;
            controlPanel.add(btnN, spaceControl);

            btnNE.setActionCommand("Northeast");
            btnNE.addActionListener(this);
            spaceControl.gridx = 6;
            spaceControl.gridy = 0;
            controlPanel.add(btnNE, spaceControl);

            btnW.setActionCommand("West");
            btnW.addActionListener(this);
            spaceControl.gridx = 0;
            spaceControl.gridy = 3;
            controlPanel.add(btnW, spaceControl);

            spaceControl.gridx = 3;
            spaceControl.gridy = 3;
            controlPanel.add(imgLbl, spaceControl);

            btnE.setActionCommand("East");
            btnE.addActionListener(this);
            spaceControl.gridx = 6;
            spaceControl.gridy = 3;
            controlPanel.add(btnE, spaceControl);

            btnSW.setActionCommand("Southwest");
            btnSW.addActionListener(this);
            spaceControl.gridx = 0;
            spaceControl.gridy = 6;
            controlPanel.add(btnSW, spaceControl);

            btnS.setActionCommand("South");
            btnS.addActionListener(this);
            spaceControl.gridx = 3;
            spaceControl.gridy = 6;
            controlPanel.add(btnS, spaceControl);

            btnSE.setActionCommand("Southeast");
            btnSE.addActionListener(this);
            spaceControl.gridx = 6;
            spaceControl.gridy = 6;
            controlPanel.add(btnSE, spaceControl);

            contentPanel.add(controlPanel, new TableLayoutConstraints(0, 1, 0, 1, TableLayoutConstants.FULL,
                    TableLayoutConstants.FULL));

        }
        dialogPane.add(contentPanel, BorderLayout.CENTER);
    }

    contentPane.add(dialogPane, BorderLayout.CENTER);
    setSize(700, 800);

    setLocationRelativeTo(null);
}