0

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)));
skaffman
  • 398,947
  • 96
  • 818
  • 769
kskaradzinski
  • 4,954
  • 10
  • 48
  • 70
  • Where do you set the size? Is it this line `setSize(1366, 768);` or do you mean this line `jList0.setSize(400, screenHeight);`? And which editor do you use? – Thomas May 09 '11 at 09:08
  • `jList0.setSize(400, screenHeight);` this is the line for JList, and Im using Eclipse with SVE (Swing Visual Editor) – kskaradzinski May 09 '11 at 09:10
  • Also note that the layout manager might change the size of the components later (depending on the layout manager and settings used). – Thomas May 09 '11 at 09:10
  • So how can i change this ??, becouse when i run the program frame is 100% width and height of screen. – kskaradzinski May 09 '11 at 09:12

2 Answers2

1

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);
Baz
  • 36,440
  • 11
  • 68
  • 94
  • no, never do _any_ manual sizing/locating, that's the exclusive task of a suitable LayoutManager – kleopatra Oct 01 '12 at 11:11
  • First I tried to resize list with setsize(w,h) method but I was unsuccessful. Then I first set size of scrollpane and then set size of list. – Ajinkya Jagtap Oct 02 '12 at 05:53
1

here Setting the maximum size of a JDialog?

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319