Example usage for javax.swing JScrollPane setPreferredSize

List of usage examples for javax.swing JScrollPane setPreferredSize

Introduction

In this page you can find the example usage for javax.swing JScrollPane 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:PrintUIWindow.java

public static void main(String args[]) {
    UIManager.put("swing.boldMetal", Boolean.FALSE);
    JFrame f = new JFrame("Print UI Example");
    f.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);/*from ww w .  java2s .c o  m*/
        }
    });
    JTextArea text = new JTextArea(50, 20);
    for (int i = 1; i <= 50; i++) {
        text.append("Line " + i + "\n");
    }
    JScrollPane pane = new JScrollPane(text);
    pane.setPreferredSize(new Dimension(250, 200));
    f.add("Center", pane);
    JButton printButton = new JButton("Print This Window");
    printButton.addActionListener(new PrintUIWindow(f));
    f.add("South", printButton);
    f.pack();
    f.setVisible(true);
}

From source file:Main.java

public static void main(String args[]) {
    JFrame f = new JFrame();
    f.setLayout(new FlowLayout());
    f.setSize(240, 250);/*from ww w.j a  va 2 s. c om*/
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JScrollPane jscrlpLabel = new JScrollPane(
            new JLabel("<html>A<br>B<br>C<br>D<br>E<br>F<br>G<br>H<br></html>."));

    jscrlpLabel.setPreferredSize(new Dimension(200, 100));

    JLabel label = new JLabel("Option");
    JCheckBox a = new JCheckBox("A");
    JCheckBox b = new JCheckBox("B");
    JCheckBox c = new JCheckBox("C");
    JCheckBox d = new JCheckBox("D");
    JCheckBox e = new JCheckBox("E");

    Box box = Box.createVerticalBox();

    box.add(label);
    box.add(a);
    box.add(b);
    box.add(c);
    box.add(d);
    box.add(e);

    JScrollPane jscrlpBox = new JScrollPane(box);
    jscrlpBox.setPreferredSize(new Dimension(140, 90));
    f.add(jscrlpLabel);
    f.add(jscrlpBox);

    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();

    JTextPane codearea = new JTextPane();
    JScrollPane scroll;
    scroll = new JScrollPane(codearea, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.setPreferredSize(new Dimension(300, 300));

    JPanel panel = new JPanel(new BorderLayout());
    panel.add(scroll, BorderLayout.CENTER);
    JButton comp = new JButton("Print text");
    comp.addActionListener(new ActionListener() {
        @Override//from  w  w w .j  a  va2 s. c o m
        public void actionPerformed(ActionEvent e) {
            System.out.println(codearea.getText());
        }
    });
    panel.add(comp, BorderLayout.SOUTH);

    frame.getContentPane().add(panel);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);

}

From source file:SimpleClient.java

public static void main(String argv[]) {
    boolean usage = false;

    for (int optind = 0; optind < argv.length; optind++) {
        if (argv[optind].equals("-L")) {
            url.addElement(argv[++optind]);
        } else if (argv[optind].startsWith("-")) {
            usage = true;//from  www. j  a v  a2 s.c o  m
            break;
        } else {
            usage = true;
            break;
        }
    }

    if (usage || url.size() == 0) {
        System.out.println("Usage: SimpleClient -L url");
        System.out.println("   where url is protocol://username:password@hostname/");
        System.exit(1);
    }

    try {
        // Set up our Mailcap entries.  This will allow the JAF
        // to locate our viewers.
        File capfile = new File("simple.mailcap");
        if (!capfile.isFile()) {
            System.out.println("Cannot locate the \"simple.mailcap\" file.");
            System.exit(1);
        }

        CommandMap.setDefaultCommandMap(new MailcapCommandMap(new FileInputStream(capfile)));

        JFrame frame = new JFrame("Simple JavaMail Client");
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        // Get a Store object
        SimpleAuthenticator auth = new SimpleAuthenticator(frame);
        Session session = Session.getDefaultInstance(System.getProperties(), auth);
        //session.setDebug(true);

        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

        // create a node for each store we have
        for (Enumeration e = url.elements(); e.hasMoreElements();) {
            String urlstring = (String) e.nextElement();
            URLName urln = new URLName(urlstring);
            Store store = session.getStore(urln);

            StoreTreeNode storenode = new StoreTreeNode(store);
            root.add(storenode);
        }

        DefaultTreeModel treeModel = new DefaultTreeModel(root);
        JTree tree = new JTree(treeModel);
        tree.addTreeSelectionListener(new TreePress());

        /* Put the Tree in a scroller. */
        JScrollPane sp = new JScrollPane();
        sp.setPreferredSize(new Dimension(250, 300));
        sp.getViewport().add(tree);

        /* Create a double buffered JPanel */
        JPanel sv = new JPanel(new BorderLayout());
        sv.add("Center", sp);

        fv = new FolderViewer(null);

        JSplitPane jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sv, fv);
        jsp.setOneTouchExpandable(true);
        mv = new MessageViewer();
        JSplitPane jsp2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, jsp, mv);
        jsp2.setOneTouchExpandable(true);

        frame.getContentPane().add(jsp2);
        frame.pack();
        frame.show();

    } catch (Exception ex) {
        System.out.println("SimpletClient caught exception");
        ex.printStackTrace();
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel gui = new JPanel(new BorderLayout(5, 5));

    JPanel plafComponents = new JPanel(new FlowLayout(FlowLayout.RIGHT, 3, 3));
    plafComponents.setBorder(new TitledBorder("FlowLayout(FlowLayout.RIGHT, 3,3)"));

    UIManager.LookAndFeelInfo[] plafInfos = UIManager.getInstalledLookAndFeels();
    String[] plafNames = new String[plafInfos.length];
    for (int ii = 0; ii < plafInfos.length; ii++) {
        plafNames[ii] = plafInfos[ii].getName();
    }//from  w w  w. j a va 2 s . c  o m
    JComboBox plafChooser = new JComboBox(plafNames);
    plafComponents.add(plafChooser);

    JCheckBox pack = new JCheckBox("Pack on PLAF change", true);
    plafComponents.add(pack);

    plafChooser.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            int index = plafChooser.getSelectedIndex();
            try {
                UIManager.setLookAndFeel(plafInfos[index].getClassName());
                SwingUtilities.updateComponentTreeUI(frame);
                if (pack.isSelected()) {
                    frame.pack();
                    frame.setMinimumSize(frame.getSize());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

    gui.add(plafComponents, BorderLayout.NORTH);

    JPanel dynamicLabels = new JPanel(new BorderLayout(4, 4));
    dynamicLabels.setBorder(new TitledBorder("BorderLayout(4,4)"));
    gui.add(dynamicLabels, BorderLayout.WEST);

    final JPanel labels = new JPanel(new GridLayout(0, 2, 3, 3));
    labels.setBorder(new TitledBorder("GridLayout(0,2,3,3)"));

    JButton addNew = new JButton("Add Another Label");
    dynamicLabels.add(addNew, BorderLayout.NORTH);
    addNew.addActionListener(new ActionListener() {

        private int labelCount = 0;

        public void actionPerformed(ActionEvent ae) {
            labels.add(new JLabel("Label " + ++labelCount));
            frame.validate();
        }
    });

    dynamicLabels.add(new JScrollPane(labels), BorderLayout.CENTER);

    String[] header = { "Name", "Value" };
    String[] a = new String[0];
    String[] names = System.getProperties().stringPropertyNames().toArray(a);
    String[][] data = new String[names.length][2];
    for (int ii = 0; ii < names.length; ii++) {
        data[ii][0] = names[ii];
        data[ii][1] = System.getProperty(names[ii]);
    }
    DefaultTableModel model = new DefaultTableModel(data, header);
    JTable table = new JTable(model);

    JScrollPane tableScroll = new JScrollPane(table);
    Dimension tablePreferred = tableScroll.getPreferredSize();
    tableScroll.setPreferredSize(new Dimension(tablePreferred.width, tablePreferred.height / 3));

    JPanel imagePanel = new JPanel(new GridBagLayout());
    JLabel imageLabel = new JLabel("test");
    imagePanel.add(imageLabel, null);

    JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, tableScroll, new JScrollPane(imagePanel));
    gui.add(splitPane, BorderLayout.CENTER);

    frame.setContentPane(gui);
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * @param title/*from ww w .  java 2s. c om*/
 * @param comp 
 * @return jpanel
 */
public static JPanel createTitledScrollComponent(String title, Component comp) {
    JScrollPane scroll = new JScrollPane(comp);
    scroll.setWheelScrollingEnabled(true);
    scroll.setPreferredSize(new Dimension(1, 1));
    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createTitledBorder(title));
    panel.setLayout(new BorderLayout());
    panel.add(scroll, BorderLayout.CENTER);
    return panel;
}

From source file:fr.irit.smac.libs.tooling.plot.server.AgentPlotChart.java

private static JFrame getFrame() {
    if (frame == null) {
        frame = new JFrame(frameName);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JScrollPane jScrollPane = new JScrollPane(getChartContainer());
        jScrollPane.setPreferredSize(new Dimension(800, 600));
        frame.getContentPane().add(jScrollPane, BorderLayout.CENTER);
        frame.pack();// w ww . j a  v a2s .co m
        frame.setVisible(true);
    }
    return frame;
}

From source file:edu.mbl.jif.datasetconvert.CheckboxTreeDimensions.java

public static void test() {
    JSONObject sumMD = SumMetadata.newSummaryMetadata("", "", new ImageAttributes(100, 100),
            new String[] { "Aniso", "Orient", "Sample0", "Sample1" }, 5, 10, 1, "source", "comment");
    final CheckboxTreeDimensions tree = new CheckboxTreeDimensions(sumMD);
    QuickFrame f = new QuickFrame("Test");

    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 600);/*from   ww  w.j a v  a  2s .c  o  m*/
    Container jContentPane = new JPanel();
    jContentPane.setLayout(new BorderLayout());
    JScrollPane cbt = tree.getCheckboxTree();
    cbt.setPreferredSize(new Dimension(200, 400));
    jContentPane.add(cbt, BorderLayout.CENTER);

    JButton buttonTest = new JButton("test");
    buttonTest.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            tree.getSelectedIndices();
        }
    });
    jContentPane.add(buttonTest, BorderLayout.SOUTH);
    f.setContentPane(jContentPane);
    f.setTitle("CheckboxTree");
    f.pack();
    f.setVisible(true);

}

From source file:eu.cassandra.csn.gui.Stats.java

/**
 * //from   w ww  .  j a v a  2  s . co m
 * @param frame
 */
public static JPanel setNetworkStats(JFrame frame) {
    String[][] data = new String[][] { { "Virtual Days:", "" }, { "Number of nodes:", "" },
            { "Number of edges:", "" }, { "Graph diameter:", "" }, { "Vertex Betweenness Centrality:", "" },
            { "Edge Betweenness Centrality:", "" }, { "Total consumption:", "" },
            { "Average consumption:", "" }, { "Peak consumption:", "" }, { "Unconnected vertices:", "" },
            { "Clusters:", "" } };
    String[] columnName = new String[] { "Metric", "Value" };

    tableModel = new MyDefaultTableModel(data, columnName);
    JTable table = new JTable(tableModel);
    JScrollPane scrollPane = new JScrollPane(table);
    table.setFillsViewportHeight(true);

    String[][] dataSelected = new String[5][];
    String[] columnNameSelected = new String[] { "Name", "Type", "Total Consumption", "Peak Comsumption",
            "Avg Consumption" };
    tableModelSelected = new MyDefaultTableModel(dataSelected, columnNameSelected);
    JTable tableSelected = new JTable(tableModelSelected);
    JScrollPane scrollPaneSelected = new JScrollPane(tableSelected);
    tableSelected.setFillsViewportHeight(true);
    tableSelected.setPreferredSize(new Dimension(1600, 100));
    scrollPaneSelected.setPreferredSize(new Dimension(1600, 100));

    JPanel statsPanel = new JPanel(new BorderLayout());
    statsPanel.add(scrollPane, BorderLayout.CENTER);
    chartPanel = Charts.createGraph("Power Consumption", "Hours", "Power (W)", new Double[0]);

    statsPanel.add(chartPanel, BorderLayout.PAGE_END);

    //frame.add(statsPanel,BorderLayout.EAST);
    frame.add(scrollPaneSelected, BorderLayout.PAGE_END);
    return statsPanel;
}

From source file:com.clank.launcher.swing.SwingHelper.java

/**
 * Show a message dialog using/*from   w ww.  ja va  2s.c  om*/
 * {@link javax.swing.JOptionPane#showMessageDialog(java.awt.Component, Object, String, int)}.
 *
 * <p>The dialog will be shown from the Event Dispatch Thread, regardless of the
 * thread it is called from. In either case, the method will block until the
 * user has closed the dialog (or dialog creation fails for whatever reason).</p>
 *
 * @param parentComponent the frame from which the dialog is displayed, otherwise
 *                        null to use the default frame
 * @param message the message to display
 * @param title the title string for the dialog
 * @param messageType see {@link javax.swing.JOptionPane#showMessageDialog(java.awt.Component, Object, String, int)}
 *                    for available message types
 */
public static void showMessageDialog(final Component parentComponent, @NonNull final String message,
        @NonNull final String title, final String detailsText, final int messageType) {

    if (SwingUtilities.isEventDispatchThread()) {
        // To force the label to wrap, convert the message to broken HTML
        String htmlMessage = "<html><div style=\"width: 250px\">" + htmlEscape(message);

        JPanel panel = new JPanel(new BorderLayout(0, detailsText != null ? 20 : 0));

        // Add the main message
        panel.add(new JLabel(htmlMessage), BorderLayout.NORTH);

        // Add the extra details
        if (detailsText != null) {
            JTextArea textArea = new JTextArea(_("errors.reportErrorPreface") + detailsText);
            JLabel tempLabel = new JLabel();
            textArea.setFont(tempLabel.getFont());
            textArea.setBackground(tempLabel.getBackground());
            textArea.setTabSize(2);
            textArea.setEditable(false);
            textArea.setComponentPopupMenu(TextFieldPopupMenu.INSTANCE);

            JScrollPane scrollPane = new JScrollPane(textArea);
            scrollPane.setPreferredSize(new Dimension(350, 120));
            panel.add(scrollPane, BorderLayout.CENTER);
        }

        JOptionPane.showMessageDialog(parentComponent, panel, title, messageType);
    } else {
        // Call method again from the Event Dispatch Thread
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                @Override
                public void run() {
                    showMessageDialog(parentComponent, message, title, detailsText, messageType);
                }
            });
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}