Java JComboBox滚动到所选项目

时间:2023-01-19 10:08:56

I have a grid with data and a JComboBox with a lot of users in it, of which one is selected. I would like to scroll to the selected item to the area where user can't see the rest of the data(the bottom of the grid) so that my JScrollPane will jump to this area automatically.

我有一个包含数据的网格和一个包含大量用户的JComboBox,其中一个被选中。我想滚动到所选项目到用户无法看到其余数据(网格底部)的区域,以便我的JScrollPane将自动跳转到此区域。

How can I do that?

我怎样才能做到这一点?

I think this has something to do with scrollRectToVisible() method.

我认为这与scrollRectToVisible()方法有关。

1 个解决方案

#1


1  

A JComboBox needs no JScrollPane.

JComboBox不需要JScrollPane。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class OneLineCombo {

    private JComponent ui = null;

    OneLineCombo() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,20,4,20));

        String[] fontFamily = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        JComboBox fontCombo = new JComboBox(fontFamily);
        fontCombo.setMaximumRowCount(1);
        ui.add(fontCombo, BorderLayout.PAGE_START);
        ui.add(new JLabel("Type some letters of the font name to select it"), 
                BorderLayout.PAGE_END);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                OneLineCombo o = new OneLineCombo();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

#1


1  

A JComboBox needs no JScrollPane.

JComboBox不需要JScrollPane。

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class OneLineCombo {

    private JComponent ui = null;

    OneLineCombo() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        ui = new JPanel(new BorderLayout(4,4));
        ui.setBorder(new EmptyBorder(4,20,4,20));

        String[] fontFamily = GraphicsEnvironment.
                getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        JComboBox fontCombo = new JComboBox(fontFamily);
        fontCombo.setMaximumRowCount(1);
        ui.add(fontCombo, BorderLayout.PAGE_START);
        ui.add(new JLabel("Type some letters of the font name to select it"), 
                BorderLayout.PAGE_END);
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                OneLineCombo o = new OneLineCombo();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}