简体   繁体   中英

How to make a JList filling JPanel

I have got a JPanel with a JList added inside. How to make that that JList will got height and weight like my JPanel . It need to fill it totaly.

If i have got 2 items in my JList , i want to 1 of these will be 1/2 of height, if 3 - 1/3 of height etc.

Any solutions?

public class RozkladLotow extends JPanel{
Vector<Samolot> samoloty;
public RozkladLotow(GeneratorSamolotow g){
    super();
    this.setLayout(new BorderLayout());
    Miasto m1 = new Miasto("Warszawa",571,142,"Polska",10,10);//sample objects needed to create a `Samolot`
    Miasto m2 = new Miasto("Pekin",571,142,"Chiny",10,10);//sample objects needed to create a `Samolot`
    samoloty = new Vector<Samolot>();
    samoloty.add(new Samolot(0, m1, m2, 0, 0, 0 , 0));
    samoloty.add(new Samolot(0, m2, m1, 0, 0, 0 , 0));
    JList lista = new JList<Samolot>(samoloty);
    add(lista,BorderLayout.CENTER);     
}
}

Ofc in class Samolot i have got toString() functions returning sample String

jPanel.setLayout(new BorderLayout());
jPanel.add(jList, BorderLayout.CENTER);

try this..

     JList<String> list = new JList<>();
     JPanel panel = new JPanel(new BorderLayout()); 
     panel.add(list,BorderLayout.CENTER);

If you have single component in panel, it is automatically stretch the component.

How to make that that JList will got height and weight like my JPanel. It need to fill it totaly.

Well in this case the solution is adding the list into a JPanel with BorderLayout , as pointed out in other answers.

If i have got 2 items in my JList, i want to 1 of these will be 1/2 of height, if 3 - 1/3 of height etc.

This requirement is a little more tricky: the cell height depends on the list's visible rectangle and the number of elements in the list or the number of elements you want to display , which may be different.

For instance let's say the list has 6 elements but you only want to display a maximum of 5 cells (rows) and then scroll the list. You can do as follows:

For instance:

DefaultListModel model = new DefaultListModel();
model.addElement("Fender");
model.addElement("Gibson");
model.addElement("Ibanez");
model.addElement("Paul Reed Smith");
model.addElement("Jackson");
model.addElement("Godin"); // The model has 6 elements

JList list = new JList(model);
list.setVisibleRowCount(5); // I want to show only 5 elements, then scroll the list

list.addComponentListener(new ComponentAdapter() { // ComponentAdapter implements ComponentListener interface
    @Override
    public void componentResized(ComponentEvent e) {
        JList list = (JList)e.getComponent();
        int divider = Math.min(list.getVisibleRowCount(), list.getModel().getSize());
        list.setFixedCellHeight(list.getVisibleRect().height / divider);
    }
});

JPanel content = new JPanel(new BorderLayout());
content.add(new JScrollPane(list));

Since I'm using Math.min() to determine the minimum divider in order to set the fixed cell height properly, if the list model has only 2 elements they both will fill the 50% each one of the available height.

Note: if you want to display all the elements in the list always, then it's easier:

list.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        JList list = (JList)e.getComponent();
        list.setFixedCellHeight(list.getVisibleRect().height / list.getModel().getSize());
    }
});

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