Bernheart
Bernheart

Reputation: 637

Returning a JTable from the JPanel class within the table

I have the follower class and I want to get the JTable that is in.

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

Please note that I don't want to see the table in a Frame. I've yet done this: now I need to add a MouseListener to the table, while the class that I have is a JPanel extension. How can I do that?

Upvotes: 0

Views: 451

Answers (1)

kiheru
kiheru

Reputation: 6618

Add a getter method to TimeTable:

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

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

Then you can get the JTable instance if you have a TimeTable reference:

TimeTable timeTable = new TimeTable(42);
...
JTable table = timeTable.getTable();

Upvotes: 3

Related Questions