Java Code Examples for javax.swing.JScrollPane#getPreferredSize()

The following examples show how to use javax.swing.JScrollPane#getPreferredSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventThreadJOptionPane.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
private static JScrollPane newScrollPane(final @Nullable String message) {
  final JLabel label = new JLabel(message);
  final JScrollPane scroll = new JScrollPane(label);
  scroll.setBorder(BorderFactory.createEmptyBorder());
  final Dimension screenResolution = Toolkit.getDefaultToolkit().getScreenSize();
  final int availWidth = screenResolution.width - 40;
  final int availHeight = screenResolution.height - 140;
  // add 25 for scrollbars
  final int newWidth =
      (scroll.getPreferredSize().width > availWidth
          ? availWidth
          : (scroll.getPreferredSize().width
              + (scroll.getPreferredSize().height > availHeight ? 25 : 0)));
  final int newHeight =
      (scroll.getPreferredSize().height > availHeight
          ? availHeight
          : (scroll.getPreferredSize().height
              + (scroll.getPreferredSize().width > availWidth ? 25 : 0)));
  scroll.setPreferredSize(new Dimension(newWidth, newHeight));
  return scroll;
}
 
Example 2
Source File: MenuKeyPreferences.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new {@link MenuKeyPreferences}.
 *
 * @param owner The owning {@link PreferencesWindow}.
 */
public MenuKeyPreferences(PreferencesWindow owner) {
    super(I18n.Text("Menu Keys"), owner);
    setLayout(new BorderLayout());
    mPanel = new BandedPanel(I18n.Text("Menu Keys"));
    mPanel.setLayout(new ColumnLayout(2, 5, 0));
    mPanel.setBorder(new EmptyBorder(2, 5, 2, 5));
    mPanel.setOpaque(true);
    mPanel.setBackground(Color.WHITE);
    for (Command cmd : StdMenuBar.getCommands()) {
        JButton button = new JButton(KeyStrokeDisplay.getKeyStrokeDisplay(KeyStroke.getKeyStroke('Z', InputEvent.META_DOWN_MASK | InputEvent.ALT_DOWN_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK)));
        UIUtilities.setToPreferredSizeOnly(button);
        button.setText(getAcceleratorText(cmd));
        mMap.put(button, cmd);
        button.addActionListener(this);
        mPanel.add(button);
        JLabel label = new JLabel(cmd.getTitle());
        mPanel.add(label);
    }
    mPanel.setSize(mPanel.getPreferredSize());
    JScrollPane scroller      = new JScrollPane(mPanel);
    Dimension   preferredSize = scroller.getPreferredSize();
    if (preferredSize.height > 200) {
        preferredSize.height = 200;
    }
    scroller.setPreferredSize(preferredSize);
    add(scroller);
}
 
Example 3
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void adjustScrollPaneSize(JScrollPane sp, JEditorPane editorPane) {
    int height;
    //logger.fine("createSingleLineEditor(): editorPane's margin = "+editorPane.getMargin());
    //logger.fine("createSingleLineEditor(): editorPane's insets = "+editorPane.getInsets());
    Dimension prefSize = sp.getPreferredSize();
    Insets borderInsets = sp.getBorder().getBorderInsets(sp);//sp.getInsets();
    EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(editorPane);
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("createSingleLineEditor(): editor UI = "+eui);
        if (eui != null) {
            logger.fine("createSingleLineEditor(): editor UI's line height = "+eui.getLineHeight());
            logger.fine("createSingleLineEditor(): editor UI's line ascent = "+eui.getLineAscent());
        }
    }
    if (eui != null) {
        height = eui.getLineHeight();
        if (height < eui.getLineAscent()) {
            height = (eui.getLineAscent()*4)/3; // Hack for the case when line height = 1
        }
    } else {
        java.awt.Font font = editorPane.getFont();
        java.awt.FontMetrics fontMetrics = editorPane.getFontMetrics(font);
        height = fontMetrics.getHeight();
        //logger.fine("createSingleLineEditor(): editor's font = "+font+" with metrics = "+fontMetrics+", leading = "+fontMetrics.getLeading());
        //logger.fine("createSingleLineEditor(): font's height = "+height);
    }
    height += getLFHeightAdjustment();
    //height += 2; // 2 for border
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("createSingleLineEditor(): border vertical insets = "+borderInsets.bottom+" + "+borderInsets.top);
        logger.fine("createSingleLineEditor(): computed height = "+height+", prefSize = "+prefSize);
    }
    if (prefSize.height < height) {
        prefSize.height = height;
        sp.setPreferredSize(prefSize);
        sp.setMinimumSize(prefSize);
        sp.setMaximumSize(prefSize);
        java.awt.Container c = sp.getParent();
        logger.fine("createSingleLineEditor(): setting a new height of ScrollPane = "+height);
        if (c instanceof JComponent) {
            ((JComponent) c).revalidate();
        }
    }
}
 
Example 4
Source File: ReferenceLookupPreferences.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a new {@link ReferenceLookupPreferences}.
 *
 * @param owner The owning {@link PreferencesWindow}.
 */
public ReferenceLookupPreferences(PreferencesWindow owner) {
    super(I18n.Text("Page References"), owner);
    setLayout(new BorderLayout());
    mPanel = new BandedPanel(I18n.Text("Page References"));
    mPanel.setLayout(new ColumnLayout(4, 5, 0));
    mPanel.setBorder(new EmptyBorder(2, 5, 2, 5));
    mPanel.setOpaque(true);
    mPanel.setBackground(Color.WHITE);
    Preferences prefs = Preferences.getInstance();
    for (PdfRef ref : prefs.allPdfRefs(false)) {
        JButton button = new JButton(I18n.Text("Remove"));
        UIUtilities.setToPreferredSizeOnly(button);
        button.addActionListener(event -> {
            prefs.removePdfRef(ref);
            Component[] children = mPanel.getComponents();
            int         length   = children.length;
            for (int i = 0; i < length; i++) {
                if (children[i] == button) {
                    for (int j = i + 4; --j >= i; ) {
                        mPanel.remove(j);
                    }
                    mPanel.setSize(mPanel.getPreferredSize());
                    break;
                }
            }
        });
        mPanel.add(button);
        JLabel idLabel = new JLabel(ref.getId(), SwingConstants.CENTER);
        idLabel.setBorder(new CompoundBorder(new LineBorder(), new EmptyBorder(1, 4, 1, 4)));
        idLabel.setOpaque(true);
        idLabel.setBackground(Color.YELLOW);
        mPanel.add(idLabel);
        EditorField field = new EditorField(new DefaultFormatterFactory(new IntegerFormatter(-9999, 9999, true)), event -> ref.setPageToIndexOffset(((Integer) event.getNewValue()).intValue()), SwingConstants.RIGHT, Integer.valueOf(ref.getPageToIndexOffset()), Integer.valueOf(-9999), I18n.Text("If your PDF is opening up to the wrong page when opening page references, enter an offset here to compensate."));
        mPanel.add(field);
        mPanel.add(new JLabel(ref.getPath().normalize().toAbsolutePath().toString()));
    }
    mPanel.setSize(mPanel.getPreferredSize());
    JScrollPane scroller      = new JScrollPane(mPanel);
    Dimension   preferredSize = scroller.getPreferredSize();
    if (preferredSize.height > 200) {
        preferredSize.height = 200;
    }
    scroller.setPreferredSize(preferredSize);
    add(scroller);
}