如何在JTable单元格中显示Jtable

时间:2017-05-03 08:40:22

标签: java swing jtable

我想要在TableCell中获取一个Table。 我需要一个像Example这样的表格。 我尝试了以下方法:

import window.TableWindow;

public class JTableStart {
    public static void main(String[] args) {
        new TableWindow( "My table window" );
    } //End main
} //End class

TableWindow:

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import MyTableModel;

@SuppressWarnings("serial")
public class TableWindow extends JFrame {
    private MyTableModel dataModel  = new MyTableModel();
    private final JTable table = new JTable(dataModel);

    public TableWindow ( String pTitle ) {
        super( pTitle );
        this.setSize( 700, 800);
        this.setDefaultCloseOperation( EXIT_ON_CLOSE );
        getContentPane().setLayout( new GridLayout( 1, 1 ) );
        table.setRowHeight(85);
        JScrollPane scrollPane = new JScrollPane(table);
        this.add(scrollPane);
        this.setVisible( true );
    }
}

MyTableModel:

import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class MyTableModel extends AbstractTableModel {
    Object[] [] data = {
        {"TableCell1-1", "TableCell1-2", "TableCell1-3", new TableCellData("TableCell 1-4")},
        {"TableCell2-1", "TableCell2-2", "TableCell2-3", new TableCellData("TableCell 2-4")},
        {"TableCell3-1", "TableCell3-2", "TableCell3-3", new TableCellData("TableCell 3-4")},
    };

    @Override
    public int getRowCount() {return 3;}

    @Override
    public int getColumnCount() {return 4;}

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }
}

TableCellData:

import javax.swing.table.AbstractTableModel;

@SuppressWarnings("serial")
public class TableCellData extends AbstractTableModel {
    Object[] [] data = { {"empty0"}, {"empty1"}, {"empty2"}, {"empty3"}};

    public TableCellData(String s) {
        this.data[0][0] = s + " TableCell1-1";
        this.data[1][0] = s + " TableCell2-1";
        this.data[2][0] = s + " TableCell3-1";
        this.data[3][0] = s + " TableCell4-1";}

    @Override
    public int getRowCount() {return 4;}

    @Override
    public int getColumnCount() {return 1;}

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return data[rowIndex][columnIndex];
    }
}

但结果看起来像this。我知道.toString但是我不知道如何告诉渲染器?像桌子一样显示它。

0 个答案:

没有答案