在 Jtable 中显示记录的问题
我正在使用 DefaultTable Model 在 Jtable 中显示我的记录。但是存在如图所示的问题。当我加载 jtable 时,它显示为 LJava.lang.Object 类型。 不在字符串中..我该如何解决这个问题。 这是我的代码;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SwingDenemePU");
EntityManager em = emf.createEntityManager();
Query sorgu = em.createQuery("select p from Personel p where p.personelAdSoyad like :ad");
Object[] kolonAdi = {"AD SOYAD","ÜNVAN ADI"};
sorgu.setParameter("ad", jTextField1.getText()+"%");
personelList = sorgu.getResultList();
Object[][] data = new Object[personelList.size()][kolonAdi.length];
for(int m=0; m<personelList.size(); m++)
for(int n=0; n<kolonAdi.length; n++)
for(Personel o : personelList) {
Personel personel = (Personel)o;
data[m][n] = new Object[][]{
{
personel.getPersonelAdSoyad(),
personel.getUnvanID().getUnvanAdi()
}
};
}
DefaultTableModel def = new DefaultTableModel();
def.setDataVector(data, kolonAdi);
jTable1.setModel(def);
I am using DefaultTable Model for showing my records in Jtable.But there is a problem like in the picture.When i load jtable its showing like LJava.lang.Object type. NOT in string..How can i solve this.
Here is my codes ;
EntityManagerFactory emf = Persistence.createEntityManagerFactory("SwingDenemePU");
EntityManager em = emf.createEntityManager();
Query sorgu = em.createQuery("select p from Personel p where p.personelAdSoyad like :ad");
Object[] kolonAdi = {"AD SOYAD","ÜNVAN ADI"};
sorgu.setParameter("ad", jTextField1.getText()+"%");
personelList = sorgu.getResultList();
Object[][] data = new Object[personelList.size()][kolonAdi.length];
for(int m=0; m<personelList.size(); m++)
for(int n=0; n<kolonAdi.length; n++)
for(Personel o : personelList) {
Personel personel = (Personel)o;
data[m][n] = new Object[][]{
{
personel.getPersonelAdSoyad(),
personel.getUnvanID().getUnvanAdi()
}
};
}
DefaultTableModel def = new DefaultTableModel();
def.setDataVector(data, kolonAdi);
jTable1.setModel(def);
Jtable Screen http://img244.yukle.tc/images/6092jtable_scr.jpg
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您之所以会出现此行为,是因为您将一个二维对象数组放入数据位置 (m, n)
如果您想在此位置有一个二维数组,
要获得您想要的行为,您需要定义一个自定义渲染器。 首先,我建议您将这个原始二维数组封装到您设计用来包含它的类中,然后为该类定义一个渲染器。
定义渲染器后,您可以使用以下命令将其设置在桌子上:
You are getting this behavior because you are placing a two dimensional Object array into your data location at (m, n)
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.
After defining your renderer, you can set it on the table with:
嗯,看起来您正在从 ResultSet 获取数据。 根据您的实际要求,您有几种不同的选择。
如果您确实不需要创建“Personel”对象来保存数据,那么您可以使用通用模型,您可以在 数据库中的表条目。
如果您确实需要“人员”对象,那么您确实需要创建一个自定义模型。 然而,这也可以通过使用“Bean 表模型”以通用方式完成。 您可以通过从上面的链接搜索博客来找到该模型。
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.
继 Mark 的回答之后:
另一种选择是通过调用
或分配一个
javax.swing.table.TableCellRenderer
Following on from Mark's answer:
Another option is to assign a
javax.swing.table.TableCellRenderer
with a call to eitheror
我建议不要使用
DefaultTableModel
而是使用子类AbstractTableModel
。 这样,您仍然可以遵循 Mark 建议的方法来重写对象的toString()
方法,或者您可以重写getColumnClass(int column)
以返回对象的类返回,然后通过调用JTable
的setDefaultRenderer(ClasscolumnClass, TableCellRenderer renderer)
为该类提供特定的渲染器。我从不使用 DefaultTableModel 的主要原因是因为它意味着您的数据有效地存储在两个位置。 相反,如果您对
AbstractTableModel
进行子类化,则可以将其实现为底层数据结构的视图(例如List
上的视图)。I would recommend not using
DefaultTableModel
but instead sub-classAbstractTableModel
. That way you can still follow the approach suggested by Mark of overriding the object'stoString()
method or you can overridegetColumnClass(int column)
to return the class of the object being returned and then provide a specific renderer for the class by callingJTable
'ssetDefaultRenderer(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-classAbstractTableModel
you can implement it as a view onto your underlying data structue (e.g. a view ontoList<MyRecord>
).DefaultTableModel 会将所有值显示为字符串。 您所看到的是表中显示的值的默认 Object.toString() 实现的结果。
最简单的解决方案是重写 toString() 为表中显示的对象返回一个合理的值。 但是,您可能最好实现自己的表模型,该模型更适合您所显示的数据。 有关详细信息,请参阅 Swing 教程如何使用表有关表模型的信息。
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.
它输出 [[Ljava/lang/Object@.... 的原因只是因为您分配给字段的值似乎是“Object[][]”类型
您确定这就是您想要的吗?
我没有投入太多精力,但我的直觉告诉我你真正想做的是:(
我当然没有编译或测试上面的代码)
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?
I have not put too much effort in it but my gut feeling tells me what you really want to do is something like:
(I have of course not compiled or tested the above code)
我不能 100% 确定您的数据布局来自发布的代码。 不过,我可以告诉你你的问题出在哪里。
这是创建一个 Object[][] 对象的二维数组。 或者更确切地说,是一个 mxnx1x2 数组。
如果您正在寻找一个 mx2 数组,您会这样做(这并不是您想要的,只是举例 [加上人员不在范围内,等等]):
[ [Ljava.lang.Object... 文本是 Object[] 上 toString() 的结果。 这就是告诉我你的矩阵尺寸是错误的。
I'm not 100% sure on what your data layout is from the code posted. However, I can tell you where your problem is.
This is creating a 2-dimensional array of Object[][] objects. Or rather, a mxnx1x2 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.]):
The [[Ljava.lang.Object... text is the result of toString() on an Object[]. Which is what tells me your matrix dimensions are wrong.