Pregunta

Let's say I have a List of Items(my own class that I created that have 5 fields). I want to somehow inject these items into the JTable.
And in the JTable I want to have some kind of method like public String determineColumnText(Object o, int col) where I can convert the received Object into Item and then based on the col take out a specific value from the Item and return it so that it will be shown.

I have tried searching and saw numerous answers saying create AbstractTableModel however all the tutorials I looked at do not provide anything that I desired. Closest I saw was public Object getValueAt(int rowIndex, int columnIndex) however that would mean that I would have to store all the objects I want to display inside the AbstractTableModel. But what if I want to make it so that the Object is not stored inside AbstractTableModel but at the same time can be send into the AbstractTableModel.

Any suggestion as to how to go about doing this?

¿Fue útil?

Solución

But what if I want to make it so that the Object is not stored inside AbstractTableModel

The Object should always be stored inside the TableModel. You can create the Object externally but then you need to add the Object to the TableModel. Once the Object is part of the model you should only manipulate it through the TableModel methods.

See the Row Table Model for a TableModel that will allow you to store Objects in the model but allow you to retrieve the data as a complete Object if you desire.

You will need to extend the class to implement your own getValueAt() and setValueAt() methods. These methods will access individual properties of your Object. The JButtonTableModel.java example shows how you can do this.

Otros consejos

Option 1:

  • Loop through your list of items.
  • Build arrays of the data for all cells
  • Pass that array to a DefaultTableModel's constructor, which you will use in a JTable

Option 2:

  • Extend DefaultTableModel and override getValueAt(), like you describe
  • Store a reference to the list in your table model

Option 1 can be simple if the data is not changing. Option 2 is the most flexible but you seem to have an object to it that isn't explained.

I will paste you how I've used AbstractTableModel to populate table.

public class InventoryModel extends AbstractTableModel {

private static final String[] columnNames = { "Owner", "Location", "Sample Name", "Form Factor" };
private List<Samples> samplesList;

public InventoryModel(){
    samplesList = SampleQueries.getAvailableSamples();

}

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return columnNames.length;
}

@Override
public int getRowCount() {
    return samplesList.size();
}

@Override
public String getColumnName(int column) {
    return columnNames[column];
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    switch (columnIndex) {
    case 0:
        return samplesList.get(rowIndex).getCurrentOwner();
    case 1:
        return samplesList.get(rowIndex).getSampleLocation();
    case 2:
        return samplesList.get(rowIndex).getSampleName();
    case 3:
        return samplesList.get(rowIndex).getFormFactor();
    }
    return null;
}   

And this is how I call it from the Panel.

    table = new JTable();
    model = new InventoryModel();
    model.fireTableDataChanged();
    panel.setLayout(new BorderLayout(0, 0));
    table.setModel(model);

    JScrollPane scroll = new JScrollPane(table);
    panel.add(scroll);

Hope it will work for you

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top