0

我有追随者类,我想得到JTable它。

public class TimeTable extends JPanel
{
private JTable table;

public TimeTable (int semestre)
{

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 
    table = new JTable(new MyTableModel()); 
    table.setFillsViewportHeight(true);     
    table.setPreferredScrollableViewportSize(new Dimension(510, 350));  //imposta le dimensioni della tabella
    JScrollPane jps = new JScrollPane(table);
    add(jps);
    add(new JScrollPane(table));
    table.setCellSelectionEnabled(true);
    table.setRowHeight(40);
    TableColumn tcol;
    for (int i=0; i<table.getColumnCount(); i++)
    {
        tcol = table.getColumnModel().getColumn(i);
        tcol.setCellRenderer(new CustomTableCellRenderer());

    }
}
public class MyTableModel extends AbstractTableModel {//instructions that customize my table}
public class CustomTableCellRenderer extends DefaultTableCellRenderer{//other instructions that customize my table }
}

请注意,我不想在 Frame 中看到表格。我还没有这样做:现在我需要MouseListener在表中添加一个,而我拥有的类是一个JPanel扩展。我怎样才能做到这一点?

4

1 回答 1

3

将 getter 方法添加到TimeTable

public class TimeTable extends JPanel {
    private JTable table;
    ...

    public JTable getTable() {
        return table;
    }
    ...
}

JTable然后,如果您有TimeTable参考,则可以获取该实例:

TimeTable timeTable = new TimeTable(42);
...
JTable table = timeTable.getTable();
于 2013-09-14T18:52:43.497 回答