Alireza Mazochi
Alireza Mazochi

Reputation: 977

Making Table With List Of JPanels

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?

Upvotes: 1

Views: 446

Answers (1)

c0der
c0der

Reputation: 18792

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.

Upvotes: 1

Related Questions