制作表与JPanels列表 [英] Making Table With List Of JPanels

查看:77
本文介绍了制作表与JPanels列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要Java应用程序中的表。首先我习惯了类 JTable 的对象,但我的表有很多功能,现在我尝试使用 JPanel 组件而不是表。

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?

推荐答案

如果您需要创建一个由 JPanel 组成的表格 JTextArea ,从以下内容开始:

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

其中 getRow 是由

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

getCell

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

但是,推荐的方法是使用 JTable 并尝试解决之前中描述的问题发布

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

这篇关于制作表与JPanels列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆