Making Table With List Of JPanels

爷,独闯天下 提交于 2019-12-01 10:43:28

问题


I need to a table in Java application. First I used to a Object of class JTable but my table has a lot of features and at now I try to use a list of JPanel components instead a table.

How can I make a table with a list of panels?


回答1:


If you need to create a table composed of JPanels containing JTextArea , start with something like:

JPanel table = new JPanel();
table.setLayout(new BoxLayout(table, BoxLayout.X_AXIS));
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
    table.add(getRow(numberOfColumns));
} 

where getRow is defined by

private Component getRow(int numberOfColumns) {

    JPanel row = new JPanel();
    //use GridLayout if you want equally spaced columns 
    row.setLayout(new BoxLayout(row, BoxLayout.Y_AXIS));
    for (int colIndex = 0; colIndex < numberOfColumns; colIndex++) {
        row.add(getCell());
    }
    return row;
}

and getCell

private Component getCell() {
    JTextArea ta = new JTextArea("Add text");
    ta.setBorder(BorderFactory.createLineBorder(Color.BLACK));
    return ta;
}

However, the recommended way is to use a JTable and attempt to solve the issues you described in a previous post.



来源:https://stackoverflow.com/questions/51797852/making-table-with-list-of-jpanels

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!