List of usage examples for javax.swing JList getPreferredSize
@Transient
public Dimension getPreferredSize()
preferredSize
has been set to a non-null
value just returns it. From source file:kenh.xscript.elements.Debug.java
private void initial(Container c) { c.setLayout(new BorderLayout()); // Add variable list DefaultListModel<String> model = new DefaultListModel(); if (this.getEnvironment() != null) { java.util.Set<String> keys = this.getEnvironment().getVariables().keySet(); for (String key : keys) { model.addElement(key);//from w w w. j ava2s .com } } else { for (int i = 1; i < 10; i++) { model.addElement("Variable " + i); } model.addElement("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); } JList list = new JList(model); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane listPane = new JScrollPane(); listPane.setViewportView(list); listPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); c.add(listPane, BorderLayout.EAST); list.setPreferredSize(new Dimension(150, list.getPreferredSize().height)); // JTextField quote = new JTextField(); quote.requestFocus(); //JButton button = new JButton(">>"); JPanel quotePanel = new JPanel(); quotePanel.setLayout(new BorderLayout()); quotePanel.add(quote, BorderLayout.CENTER); //quotePanel.add(button, BorderLayout.EAST); JTextArea result = new JTextArea(); result.setEditable(false); JScrollPane resultPane = new JScrollPane(); resultPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); resultPane.setViewportView(result); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(quotePanel, BorderLayout.NORTH); panel.add(resultPane, BorderLayout.CENTER); c.add(panel, BorderLayout.CENTER); list.addListSelectionListener(this); //button.addActionListener(this); quote.addKeyListener(this); this.result = result; }
From source file:org.yccheok.jstock.gui.Utils.java
/** * Adjust popup for combo box, so that horizontal scrollbar will not display. * http://forums.oracle.com/forums/thread.jspa?messageID=8037483� * http://www.camick.com/java/source/BoundsPopupMenuListener.java * * Update : According to https://forums.oracle.com/forums/thread.jspa?messageID=9789603#9789603 * , the above techniques is longer workable. * ========================================================================= * 6u25 changed when popupMenuWillBecomeVisible is called: it is now called * before the list is created so you can add items in that method and still * have the list size correctly.//from ww w .j a v a 2s . c om * See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4743225 * So for your workaround: either it isn't needed anymore or you need to add * an extra hierarchy listener to check when the list is actually added. * ========================================================================= * * I use a quick hack from * http://javabyexample.wisdomplug.com/java-concepts/34-core-java/59-tips-and-tricks-for-jtree-jlist-and-jcombobox-part-i.html * * @param comboBox The combo box */ public static void adjustPopupWidth(JComboBox comboBox) { if (comboBox.getItemCount() == 0) return; Object comp = comboBox.getAccessibleContext().getAccessibleChild(0); if (!(comp instanceof BasicComboPopup)) { return; } BasicComboPopup popup = (BasicComboPopup) comp; JList list = popup.getList(); JScrollPane scrollPane = getScrollPane(popup); // Just to be paranoid enough. if (list == null || scrollPane == null) { return; } // Determine the maximimum width to use: // a) determine the popup preferred width // b) ensure width is not less than the scroll pane width int popupWidth = list.getPreferredSize().width + 5 // make sure horizontal scrollbar doesn't appear + getScrollBarWidth(comboBox, scrollPane); Dimension scrollPaneSize = scrollPane.getPreferredSize(); //popupWidth = Math.max(popupWidth, scrollPaneSize.width); // Use comboBox.getSize(), since we realize under Linux's Java 6u25, // After expanding, scrollPane.getPreferredSize() will return expanded // size in the 2nd round, although no expand is required. popupWidth = Math.max(popupWidth, comboBox.getSize().width); // Adjust the width scrollPaneSize.width = popupWidth; scrollPane.setPreferredSize(scrollPaneSize); scrollPane.setMaximumSize(scrollPaneSize); // The above workaround is no longer working. Use the below hack code! if (comboBox instanceof JComboBoxPopupAdjustable) { ((JComboBoxPopupAdjustable) comboBox).setPopupWidth(popupWidth); } }