简体   繁体   English

如何使JList填充JPanel

[英]How to make a JList filling JPanel

I have got a JPanel with a JList added inside. 我有一个内置JListJPanel How to make that that JList will got height and weight like my JPanel . 如何让JList像我的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. 如果我的JList有2个项目,我希望其中1个是高度的1/2,如果是3 - 1/3的高度等。

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 Samolot类中的Samolot我有toString()函数返回样本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. 如何让JList像我的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. 那么在这种情况下,解决方案是将列表添加到具有BorderLayoutJPanel ,如其他答案中所指出的。

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. 如果我的JList中有2个项目,我希望其中1个是高度的1/2,如果是3 - 1/3的高度等。

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. 例如,假设列表有6个元素,但您只想显示最多5个单元格(行),然后滚动列表。 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. 由于我正在使用Math.min()来确定最小分隔线以便正确设置固定单元高度,如果列表模型只有2个元素,它们都将填充每个可用高度的50%。

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());
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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