简体   繁体   中英

java JList size

Hi i want to set JList height as height as screen is

public Locations() {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    screenWidth = screen.width;
    screenHeight = screen.height;
    initComponents();
}

private void initComponents() {
    setLayout(new GroupLayout());
    add(getJScrollPane0(), new Constraints(new Leading(8, 100, 10, 10), new Leading(5, 135, 10, 10)));
    setSize(1366, 768);
}

private JScrollPane getJScrollPane0() {
    if (jScrollPane0 == null) {
        jScrollPane0 = new JScrollPane();
        jScrollPane0.setSize(screenWidth, screenHeight);
        jScrollPane0.setViewportView(getJList0());
    }
    return jScrollPane0;
}

private JList getJList0() {
    if (jList0 == null) {
        jList0 = new JList();
        jList0.setSize(400, screenHeight);
        DefaultListModel listModel = new DefaultListModel();
        listModel.addElement("item0");
        listModel.addElement("item1item1item1item1item1");
        listModel.addElement("item2");
        listModel.addElement("item3");
        jList0.setModel(listModel);
    }
    return jList0;
}

private static void installLnF() {
    try {
        String lnfClassname = PREFERRED_LOOK_AND_FEEL;
        if (lnfClassname == null)
            lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
        UIManager.setLookAndFeel(lnfClassname);
    } catch (Exception e) {
        System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
                + " on this platform:" + e.getMessage());
    }
}

/**
 * Main entry of the class.
 * Note: This class is only created so that you can easily preview the result at runtime.
 * It is not expected to be managed by the designer.
 * You can modify it as you like.
 */
public static void main(String[] args) {
    installLnF();
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Locations");
            Locations content = new Locations();
            content.setPreferredSize(content.getSize());
            frame.add(content, BorderLayout.CENTER);
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    });
}

But list size is like i draw it in editor, how to change it ??

Edit:

I have solved the problem by adding

add(getJScrollPane0(), new Constraints(new Leading(8, 100, 10, 10), new Leading(5, screenHeight - 115, 10, 10)));

Each List is associated with JScrollPane. So First set size of JScrollPane associated with list then set size of list with list.setsize(width,height) method

In my program:

jScrollPane1.setSize(100,500);
list.setSize(100,500);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM