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

public FillViewportHeightDemo() {
    super("Empty Table DnD Demo");

    tableModel = getDefaultTableModel();
    table = new JTable(tableModel);
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    table.setDropMode(DropMode.INSERT_ROWS);

    table.setTransferHandler(new TransferHandler() {

        public boolean canImport(TransferSupport support) {
            // for the demo, we'll only support drops (not clipboard paste)
            if (!support.isDrop()) {
                return false;
            }//  w w w.  j  a  va2s .  co m

            // we only import Strings
            if (!support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                return false;
            }

            return true;
        }

        public boolean importData(TransferSupport support) {
            // if we can't handle the import, say so
            if (!canImport(support)) {
                return false;
            }

            // fetch the drop location
            JTable.DropLocation dl = (JTable.DropLocation) support.getDropLocation();

            int row = dl.getRow();

            // fetch the data and bail if this fails
            String data;
            try {
                data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            } catch (UnsupportedFlavorException e) {
                return false;
            } catch (IOException e) {
                return false;
            }

            String[] rowData = data.split(",");
            tableModel.insertRow(row, rowData);

            Rectangle rect = table.getCellRect(row, 0, false);
            if (rect != null) {
                table.scrollRectToVisible(rect);
            }

            // demo stuff - remove for blog
            model.removeAllElements();
            model.insertElementAt(getNextString(count++), 0);
            // end demo stuff

            return true;
        }
    });

    JList dragFrom = new JList(model);
    dragFrom.setFocusable(false);
    dragFrom.setPrototypeCellValue(getNextString(100));
    model.insertElementAt(getNextString(count++), 0);
    dragFrom.setDragEnabled(true);
    dragFrom.setBorder(BorderFactory.createLoweredBevelBorder());

    dragFrom.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent me) {
            if (SwingUtilities.isLeftMouseButton(me) && me.getClickCount() % 2 == 0) {
                String text = (String) model.getElementAt(0);
                String[] rowData = text.split(",");
                tableModel.insertRow(table.getRowCount(), rowData);
                model.removeAllElements();
                model.insertElementAt(getNextString(count++), 0);
            }
        }
    });

    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    JPanel wrap = new JPanel();
    wrap.add(new JLabel("Drag from here:"));
    wrap.add(dragFrom);
    p.add(Box.createHorizontalStrut(4));
    p.add(Box.createGlue());
    p.add(wrap);
    p.add(Box.createGlue());
    p.add(Box.createHorizontalStrut(4));
    getContentPane().add(p, BorderLayout.NORTH);

    JScrollPane sp = new JScrollPane(table);
    getContentPane().add(sp, BorderLayout.CENTER);
    fillBox = new JCheckBoxMenuItem("Fill Viewport Height");
    fillBox.addActionListener(this);

    JMenuBar mb = new JMenuBar();
    JMenu options = new JMenu("Options");
    mb.add(options);
    setJMenuBar(mb);

    JMenuItem clear = new JMenuItem("Reset");
    clear.addActionListener(this);
    options.add(clear);
    options.add(fillBox);

    getContentPane().setPreferredSize(new Dimension(260, 180));
}

From source file:ac.kaist.ccs.presentation.CCSHubSelectionCoverage.java

public CCSHubSelectionCoverage(final String title, List<Double> coverSourceNum) {

    super(title);
    this.title = title;
    final XYDataset dataset = createDataset(coverSourceNum);
    final JFreeChart chart = createChart(dataset);
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    final ChartPanel chartPanel = new ChartPanel(chart);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BorderLayout());
    JButton buttonExport = new JButton("Export");
    buttonPanel.add("East", buttonExport);
    buttonExport.addActionListener(new ActionListener() {
        ChartPanel chartPanel;//w  w w  .  ja v  a  2  s .c o m

        public ActionListener init(ChartPanel chartPanel) {
            this.chartPanel = chartPanel;
            return this;
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            Dimension size = chartPanel.getSize();

            try {
                //String outPath = textFieldSelectPath.getText();
                //String filename = "chromatography.png";
                //String path = outPath+"/"+filename;
                JFileChooser fileChooser = new JFileChooser();

                fileChooser.setCurrentDirectory(new File("/Users/mac/Desktop"));

                fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JPEG", "jpeg"));

                int returnVal = fileChooser.showDialog(new JFrame(), "Open File Path");

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    String inputPath = fileChooser.getSelectedFile().getAbsolutePath();
                    if (!inputPath.endsWith(".jpeg"))
                        inputPath = inputPath + ".jpeg";

                    OutputStream os = new FileOutputStream(inputPath);
                    System.out.println(inputPath + "///" + size.width + " " + size.height);
                    BufferedImage chartImage = chartPanel.getChart().createBufferedImage(size.width,
                            size.height, null);
                    ImageIO.write(chartImage, "png", os);
                    os.close();
                    JOptionPane.showMessageDialog(null, "Chart image was saved in " + inputPath);

                }

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }.init(chartPanel));

    panel.add("Center", chartPanel);
    panel.add("South", buttonPanel);

    chartPanel.setPreferredSize(new java.awt.Dimension(700, 500));
    setContentPane(panel);

}

From source file:ac.kaist.ccs.presentation.CCSHubSelectionCo2Coverage.java

public CCSHubSelectionCo2Coverage(final String title, List<Double> coverSourceNum) {

    super(title);
    this.title = title;
    final XYDataset dataset = createDataset(coverSourceNum);
    final JFreeChart chart = createChart(dataset);

    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    final ChartPanel chartPanel = new ChartPanel(chart);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new BorderLayout());
    JButton buttonExport = new JButton("Export");
    buttonPanel.add("East", buttonExport);
    buttonExport.addActionListener(new ActionListener() {
        ChartPanel chartPanel;//from   ww  w .  ja  va 2 s.c  om

        public ActionListener init(ChartPanel chartPanel) {
            this.chartPanel = chartPanel;
            return this;
        }

        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
            Dimension size = chartPanel.getSize();

            try {
                //String outPath = textFieldSelectPath.getText();
                //String filename = "chromatography.png";
                //String path = outPath+"/"+filename;
                JFileChooser fileChooser = new JFileChooser();

                fileChooser.setCurrentDirectory(new File("/Users/mac/Desktop"));

                fileChooser.setFileFilter(new javax.swing.filechooser.FileNameExtensionFilter("JPEG", "jpeg"));

                int returnVal = fileChooser.showDialog(new JFrame(), "Open File Path");

                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    String inputPath = fileChooser.getSelectedFile().getAbsolutePath();
                    if (!inputPath.endsWith(".jpeg"))
                        inputPath = inputPath + ".jpeg";

                    OutputStream os = new FileOutputStream(inputPath);
                    System.out.println(inputPath + "///" + size.width + " " + size.height);
                    BufferedImage chartImage = chartPanel.getChart().createBufferedImage(size.width,
                            size.height, null);
                    ImageIO.write(chartImage, "png", os);
                    os.close();
                    JOptionPane.showMessageDialog(null, "Chart image was saved in " + inputPath);

                }

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }.init(chartPanel));

    panel.add("Center", chartPanel);
    panel.add("South", buttonPanel);

    chartPanel.setPreferredSize(new java.awt.Dimension(700, 500));
    setContentPane(panel);

}

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

@Override
protected JPanel createPanel() {

    final JPanel result = new JPanel();

    result.setLayout(new GridBagLayout());

    FixedBooleanTableCellRenderer.attach(blueprintsTable);
    model.setViewFilter(this.byNameFilter.getViewFilter());
    blueprintsTable.setDefaultRenderer(ISKAmount.class, myRenderer);
    blueprintsTable.setModel(model);//from  w w  w  . ja v  a  2 s  .c o  m
    blueprintsTable.setFillsViewportHeight(true);
    blueprintsTable.setRowSorter(model.getRowSorter());

    result.add(byNameFilter, constraints(0, 0).noResizing().end());
    result.add(new JScrollPane(blueprintsTable), constraints(0, 1).resizeBoth().end());
    return result;
}

From source file:ch.unibas.fittingwizard.application.Visualization.java

/**
 * Visualising with JMol a given SMILES string
 *
 * @param smiles/*from  ww w .  ja  va2s  .c  o m*/
 */
public void showSMILES(String smiles) {

    if (jmolWindow == null) {
        logger.info("Creating Jmol window.");
        jmolWindow = new JFrame("Visualization of " + smiles);
        jmolWindow.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                logger.info("Jmol window closing.");
                jmolWindow = null;
                jmolViewer = null;
                currentOpenFile = null;
            }
        });
        jmolWindow.setSize(600, 600);

        Container contentPane = jmolWindow.getContentPane();
        JmolPanel jmolPanel = new JmolPanel();

        // main panel -- Jmol panel on top
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(jmolPanel);
        contentPane.add(panel);
        jmolViewer = jmolPanel.viewer;

        alignWindowPositionToWizard();
        jmolWindow.setVisible(true);
    } else {
        logger.debug("Bringing existing Jmol window to front.");
    }

    jmolViewer.scriptWait("load $" + smiles);

    jmolWindow.toFront();

}

From source file:ch.unibas.fittingwizard.application.Visualization.java

/**
 * Visualising with JMol a molecule using a PubChemId
 *
 * @param pubid//from   w  w w .j a v  a2  s . c  om
 */
public void showPubChem(String pubid) {

    if (jmolWindow == null) {
        logger.info("Creating Jmol window.");
        jmolWindow = new JFrame("Visualization of PubId " + pubid);
        jmolWindow.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                logger.info("Jmol window closing.");
                jmolWindow = null;
                jmolViewer = null;
                currentOpenFile = null;
            }
        });
        jmolWindow.setSize(600, 600);

        Container contentPane = jmolWindow.getContentPane();
        JmolPanel jmolPanel = new JmolPanel();

        // main panel -- Jmol panel on top
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(jmolPanel);
        contentPane.add(panel);
        jmolViewer = jmolPanel.viewer;

        alignWindowPositionToWizard();
        jmolWindow.setVisible(true);
    } else {
        logger.debug("Bringing existing Jmol window to front.");
    }

    jmolViewer.scriptWait("load :" + pubid);

    jmolWindow.toFront();

}

From source file:InternalFrameEventDemo.java

protected void createDisplayWindow() {
    JButton b1 = new JButton("Show internal frame");
    b1.setActionCommand(SHOW);// ww  w. j  av a  2 s  .  c  o m
    b1.addActionListener(this);

    JButton b2 = new JButton("Clear event info");
    b2.setActionCommand(CLEAR);
    b2.addActionListener(this);

    display = new JTextArea(3, 30);
    display.setEditable(false);
    JScrollPane textScroller = new JScrollPane(display);
    // Have to supply a preferred size, or else the scroll
    // area will try to stay as large as the text area.
    textScroller.setPreferredSize(new Dimension(200, 75));
    textScroller.setMinimumSize(new Dimension(10, 10));

    displayWindow = new JInternalFrame("Event Watcher", true, // resizable
            false, // not closable
            false, // not maximizable
            true); // iconifiable
    JPanel contentPane = new JPanel();
    contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
    b1.setAlignmentX(CENTER_ALIGNMENT);
    contentPane.add(b1);
    contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
    contentPane.add(textScroller);
    contentPane.add(Box.createRigidArea(new Dimension(0, 5)));
    b2.setAlignmentX(CENTER_ALIGNMENT);
    contentPane.add(b2);

    displayWindow.setContentPane(contentPane);
    displayWindow.pack();
    displayWindow.setVisible(true);
}

From source file:com.emental.mindraider.ui.dialogs.ConnectUriqaServerJDialog.java

/**
 * Constructor./* w w  w .  j  a  va  2s.co m*/
 */
public ConnectUriqaServerJDialog() {
    super(Messages.getString("ConnectUriqaServerJDialog.title"));
    getContentPane().setLayout(new BorderLayout());

    // north
    JPanel north = new JPanel();
    north.add(new JLabel(Messages.getString("ConnectUriqaServerJDialog.serverUrl")));

    uriqaServerUrl = new JTextField("http://sw.nokia.com/uriqa/", 31);
    uriqaServerUrl.setMinimumSize(new Dimension(TEXT_AREAS_WIDTH, uriqaServerUrl.getPreferredSize().height));
    uriqaServerUrl.addKeyListener(new KeyListener() {

        public void keyPressed(KeyEvent keyEvent) {
            if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
                connect();
            }
        }

        public void keyReleased(KeyEvent keyEvent) {
        }

        public void keyTyped(KeyEvent keyEvent) {
        }
    });
    north.add(uriqaServerUrl);
    getContentPane().add(north, BorderLayout.NORTH);

    // center
    JPanel center = new JPanel();
    center.setLayout(new BorderLayout());

    JPanel centerTop = new JPanel();
    centerTop.add(new JLabel(Messages.getString("ConnectUriqaServerJDialog.resource")));
    String[] knowUriqaUris = new String[] { "http://sw.nokia.com/VOC-1/term", "http://sw.nokia.com/MARS-3/Rank",
            "http://sw.nokia.com/MARS-3", "http://sw.nokia.com/schemas/nokia/MARS-3.1.rdf" };
    resourceUri = new JComboBox(knowUriqaUris);
    resourceUri.setMinimumSize(new Dimension(TEXT_AREAS_WIDTH, resourceUri.getPreferredSize().height));
    resourceUri.setEditable(true);
    centerTop.add(resourceUri);

    JPanel centerCenter = new JPanel();
    centerCenter.setLayout(new FlowLayout(FlowLayout.LEFT));
    centerCenter.setToolTipText(Messages.getString("ConnectUriqaServerJDialog.loadOverToolTip"));
    centerCenter.add(new JLabel(Messages.getString("ConnectUriqaServerJDialog.loadOver")));
    JCheckBox loadOver = new JCheckBox();
    loadOver.setSelected(true);
    loadOver.setEnabled(false);
    centerCenter.add(loadOver);

    JPanel centerBottom = new JPanel();
    centerBottom.setLayout(new FlowLayout(FlowLayout.LEFT));
    centerBottom.setToolTipText(Messages.getString("ConnectUriqaServerJDialog.addInferedToolTip"));
    centerBottom.add(new JLabel(Messages.getString("ConnectUriqaServerJDialog.addInfered")));
    addInfered = new JCheckBox();
    addInfered.setSelected(true);
    centerBottom.add(addInfered);

    center.add(centerTop, BorderLayout.NORTH);
    center.add(centerCenter, BorderLayout.CENTER);
    center.add(centerBottom, BorderLayout.SOUTH);
    getContentPane().add(center, BorderLayout.CENTER);

    // south
    JPanel south = new JPanel();
    JButton uploadButton = new JButton(Messages.getString("ConnectUriqaServerJDialog.connect"));
    uploadButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            connect();
        }
    });
    south.add(uploadButton);

    JButton cancelButton = new JButton(Messages.getString("ConnectUriqaServerJDialog.cancel"));
    cancelButton.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            dispose();
        }
    });
    south.add(cancelButton);
    getContentPane().add(south, BorderLayout.SOUTH);

    // show
    pack();
    Gfx.centerAndShowWindow(this);
}

From source file:medsavant.uhn.cancer.AddNewCommentDialog.java

private JPanel getHeader(String str) {
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
    p.add(new JLabel(str));
    p.add(Box.createHorizontalGlue());
    return p;/*from   ww  w  .  j av  a  2 s  . c  om*/
}

From source file:cool.pandora.modeller.ui.jpanel.base.NewBagFrame.java

/**
 * createComponents.// ww  w .  j a v  a2 s.c  o m
 *
 * @return pageControl
 */
private JPanel createComponents() {
    final TitlePane titlePane = new TitlePane();
    initStandardCommands();
    final JPanel pageControl = new JPanel(new BorderLayout());
    final JPanel titlePaneContainer = new JPanel(new BorderLayout());
    titlePane.setTitle(bagView.getPropertyMessage("NewBagFrame.title"));
    titlePane.setMessage(new DefaultMessage(bagView.getPropertyMessage("NewBagFrame" + ".description")));
    titlePaneContainer.add(titlePane.getControl());
    titlePaneContainer.add(new JSeparator(), BorderLayout.SOUTH);
    pageControl.add(titlePaneContainer, BorderLayout.NORTH);

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

    int row = 0;
    layoutBagVersionSelection(contentPane, row++);
    layoutProfileSelection(contentPane, row++);

    if (getPreferredSize() != null) {
        contentPane.setPreferredSize(getPreferredSize());
    }

    GuiStandardUtils.attachDialogBorder(contentPane);
    pageControl.add(contentPane);
    final JComponent buttonBar = createButtonBar();
    pageControl.add(buttonBar, BorderLayout.SOUTH);

    this.pack();
    return pageControl;
}