tags:

views:

335

answers:

7
A: 

The DefaultTableModel will display all values as Strings. What you are seeing is the result of the default Object.toString() implementation for the values being shown in your table.

The simplest solution would be to override toString() to return a sensible value for the object your are showing in the table. However, you may be better implementing your own table model that is better suited to the data you are showing. See the Swing tutorial How To Use Tables for more information on table models.

Mark
+1  A: 

I'm not 100% sure on what your data layout is from the code posted. However, I can tell you where your problem is.

data[m][n] = new Object[][]{{personel.getPersonelAdSoyad(),personel.getUnvanID().getUnvanAdi()}}

This is creating a 2-dimensional array of Object[][] objects. Or rather, a m**x**n**x**1**x**2 array.

If you're looking for an mx2 array you'd do (notthat this is what you're going for, just by way of example [plus personel isn't in scope, etc. etc.]):

data[m] = new Object[]{personel.getPersonelAdSoyad(),personel.getUnvanID().getUnvanAdi()};

The [[Ljava.lang.Object... text is the result of toString() on an Object[]. Which is what tells me your matrix dimensions are wrong.

Kevin Montrose
thanx ur solution is same as Fredrik's.
Ibrahim AKGUN
+2  A: 

The reason it outputs [[Ljava/lang/Object@.... is simply because the values you assign to the fields seems to be of type "Object[][]"

Are you sure this is what you want?

data[m][n] = new Object[][] {}?

I have not put too much effort in it but my gut feeling tells me what you really want to do is something like:

for(int m=0; m<personelList.size(); m++) {
    Personel personel = personelList.get(m);
    data[m] = new Object[]{
              personel.getPersonelAdSoyad(),
              personel.getUnvanID().getUnvanAdi()
    };
}

(I have of course not compiled or tested the above code)

Fredrik
thanx man. u r solved my problem
Ibrahim AKGUN
A: 

Following on from Mark's answer:

Another option is to assign a javax.swing.table.TableCellRenderer with a call to either

javax.swing.table.JTable.setDefaultRenderer(Class, TableCellRenderer)

or

javax.swing.table.TableColumn.setRenderer(TableCellRenderer)
Nick Holt
A: 

I would recommend not using DefaultTableModel but instead sub-class AbstractTableModel. That way you can still follow the approach suggested by Mark of overriding the object's toString() method or you can override getColumnClass(int column) to return the class of the object being returned and then provide a specific renderer for the class by calling JTable's setDefaultRenderer(Class<?> columnClass, TableCellRenderer renderer).

The main reason I never use DefaultTableModel is because it means your data is effectively stored in two places. In contrast, if you sub-class AbstractTableModel you can implement it as a view onto your underlying data structue (e.g. a view onto List<MyRecord>).

Adamski
That's agreed - DefaultTableModel is evil and you should specialize AbstractTableModel.
Nick Holt
A: 

Well, it looks like you are getting the data from a ResultSet. You have a couple of different options depending on you actual requirements.

If you don't really need to create a "Personel" object to hold the data then you can use a generic model which you can find in the Table From Database entry.

If you do need the "Personel" object, then you do need to create a custom model. However, this can also be done in a generic way by using the "Bean Table Model". You can find this model by searching the blog from the above link.

camickr
A: 

You are getting this behavior because you are placing a two dimensional Object array into your data location at (m, n)

data[m][n] = new Object[][]{
    {
        personel.getPersonelAdSoyad(), 
        personel.getUnvanID().getUnvanAdi()
    }
};

If you want to have a two dimensional array at this location, to get the behavior you want, you will need to define a custom renderer. First I would recommend that you encapsulate this raw 2d array into a class you have designed to contain it, and then define a renderer for that class.

import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class CustomTableCellRenderer implements TableCellRenderer {

    private static final TableCellRenderer defaultRenderer = new DefaultTableCellRenderer();
    public Component getTableCellRendererComponent(JTable table, Object value,
  boolean isSelected, boolean hasFocus, int row, int column) {
     Component renderedObject = null;

     if(value instanceof CustomClass){
      renderedObject = new JLabel();
      // put your customized rendering code here.
     } else {
      renderedObject = defaultRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
     }
 return renderedObject;
    }

}

After defining your renderer, you can set it on the table with:

jTable1.setDefaultRenderer(CustomClass.class, customRenderer);
hasalottajava