0

I want to implement a JTable in my program which has mulitple type different rows.

Here is an example:

enter image description here

So basically costs are calculated like that:

Sale * Production * Production % = Costs

What I am totally unsure is: How to give each row in the JTable model a new "type" of column. At the moment I am using the JTable model like that:

public Object getValueAt(int row, int col) {
        Customer cd = customerList.get(row);
        switch (col) {
        case 0:
            return cd.getName();
        case 1:
            return cd.getAge(); 
        case 2:
            return cd.getPhone(); 
        default:
            break;
        }

        return null;
    }

Any recommendations how to implement this use case?

I appreciate your answer!

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • 1
    What do you mean with "type of column"? A different data type? Different semantics? – Thomas Nov 10 '14 at 16:34
  • 1
    The question is unclear: "How to give each row in the JTable model a new "type" of column". What does giving to a row a column type mean? – DSquare Nov 10 '14 at 16:38
  • TableCellRenderer/Editor has paramater for int columnIndex and int rowIndex, use those both coodinates – mKorbel Nov 10 '14 at 17:01
  • `Sale * Production * Production % = Costs` can be done (the best of the possible ways) by overide setValueAt, why you posted a screenshot from MsExcel – mKorbel Nov 10 '14 at 17:03
  • for better help sooner post an SSCCE/MCVE short, runnable, comilable with hardcoded value in local variable, – mKorbel Nov 10 '14 at 17:03
  • @Thomas Thx for your answer! Have a look at my excel table. What I exactly mean by different type is, that I can have percentage or integer in some rows, whereas in others some calculated value. – Carol.Kar Nov 10 '14 at 17:10
  • @DSquare Thx for your answer! By different type I mean that a row can have different values inside, which depend on the row type. – Carol.Kar Nov 10 '14 at 17:12

1 Answers1

1

If you want the cells in a row have a special formatting or data type just do what you did for Customer and just switch column and row, i.e. I assume one entry in your table is represented by a single column.

Thus just do something like this:

Report report = reports.get(col - 1); //made those names up but you should get the idea
switch (row) {
  ...
  case 5:
    double p = report.getPercentage(); //assuming 10% is stored as 0.1
    return String.format("%.0f%%", p * 100); //%.0f means a floating point number with 0 fraction digits
  ...
}

Alternatively (esp. if your model needs to be more flexible or you just have a bunch of values, e.g. as a 2D array) store the type (i.e. how to display the value) in a map with either row, column or cell (row and column) index as key.

Update:

As mKorbel said in a comment, you'd better let the renderer do the formatting. The problem, however, is that you can't just register a renderer per row, so you'd have to come up with another solution.

Two of them pop into my head right now:

  1. Subclass JTable in order to override getCellRenderer(row, col) in order to provide a PercentageCellRenderer for the row that needs it. You'd have to configure which row that is, e.g. in the table model.

  2. Provide a standard cell renderer which checks the row and column in getTableCellRendererComponent() and applies the formatting accordingly.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • `return String.format("%.0f%%", p * 100);` ???, `getValueAt` is used for `TableCellRenderer` isn't easier to override `getColumnClass` with `Double.Class` and to format `Double` to `pct form` in by override `XxxRenderer`, I'd be to delete this answer as *** – mKorbel Nov 10 '14 at 17:29
  • without an SSCCE/MCVE is everything about shots into dark – mKorbel Nov 10 '14 at 17:29
  • @mKorbel well, setting the column class to `Double.class` might not be sufficient since f.e. `costs` might need a different formatting. You're right, however, in letting the renderer do the formatting but since the OP seems to be confused by even simple things I tried to give as simple an answer as I could come up at that time. – Thomas Nov 10 '14 at 17:40
  • @mKorbel another problem would be that the column has no definite class, i.e. it might contain integers as well as doubles. At least that's how I understand the OP. – Thomas Nov 10 '14 at 17:44
  • @Thomas Yep that is entirely the problem! – Carol.Kar Nov 10 '14 at 21:14
  • 2
    A complete example using the default renderers is examined [here](http://stackoverflow.com/a/22142805/230513). – trashgod Nov 10 '14 at 21:24