我正在制作一个显示数据的应用程序。但它不会。我只是显示了一些奇怪的String
,它显示的是jeremy.ItemsData@33f1ca93
另一个表格单元格显示的是jeremy.ItemsData@427a9389
。这是我的代码。我从某个地方得到了代码,我修改了它,因为我不想让我的应用程序看起来像那样。
public class JTables extends JFrame
{
private static final long serialVersionUID = 7866940189661427857L;
public JScrollPane scroll;
public JTable table;
private List<ItemsData> items;
public JTables(List<ItemsData> i)
{
items = i;
initGUI();
}
private void initGUI()
{
items.add(new ItemsData("JustTesting", null, null, null, null, null, null));
scroll = new JScrollPane();
table = new JTable(new JTablesModel(items));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
table.setDefaultRenderer(ItemsData.class, new JTablesCell());
table.setDefaultEditor(ItemsData.class, new JTablesCell());
table.setRowHeight(292);
scroll.setViewportView(table);
add(scroll);
pack();
}
}
public class JTablesModel extends AbstractTableModel implements TableModel
{
private static final long serialVersionUID = 3752534513457351699L;
private List<ItemsData> item;
public JTablesModel(List<ItemsData> i)
{
this.item = i;
}
@Override
public Class<?> getColumnClass(int columnIndex)
{
return Items.class;
}
@Override
public String getColumnName(int column)
{
return "Items";
}
@Override
public boolean isCellEditable(int rowIndex, int columnIndex)
{
return true;
}
@Override
public int getColumnCount()
{
return 1;
}
@Override
public int getRowCount()
{
return (item==null) ? 0 : item.size();
}
@Override
public Object getValueAt(int arg0, int arg1)
{
return (item==null) ? null : item.get(arg0);
}
}
public class JTablesCell extends AbstractCellEditor implements TableCellEditor, TableCellRenderer
{
private static final long serialVersionUID = -7826925896868335289L;
public JLabel data;
public JLabel icon;
public JLabel id;
public JLabel lvl;
public JLabel name;
public JLabel type;
public JPanel p;
public JTablesCell()
{
initGUI();
}
private void initGUI()
{
p = new javax.swing.JPanel();
icon = new javax.swing.JLabel();
name = new javax.swing.JLabel();
type = new javax.swing.JLabel();
lvl = new javax.swing.JLabel();
data = new javax.swing.JLabel();
id = new javax.swing.JLabel();
name.setFont(new java.awt.Font("Times New Roman", 0, 24)); // NOI18N
name.setText("Name");
type.setText("Type");
lvl.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
lvl.setText("Min Lvl");
data.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
data.setText("data");
id.setForeground(new java.awt.Color(255, 0, 51));
id.setText("Hover to view identification");
javax.swing.GroupLayout pLayout = new javax.swing.GroupLayout(p);
p.setLayout(pLayout);
pLayout.setHorizontalGroup(
pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addGroup(pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(44, 44, 44))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pLayout.createSequentialGroup()
.addContainerGap()
.addComponent(id)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
.addGroup(pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addGroup(pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(type, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lvl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(data, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pLayout.createSequentialGroup()
.addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 127, Short.MAX_VALUE)
.addGap(22, 22, 22))))
);
pLayout.setVerticalGroup(
pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addGroup(pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(pLayout.createSequentialGroup()
.addComponent(name)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(type)
.addGap(18, 18, 18)
.addComponent(lvl))
.addGroup(pLayout.createSequentialGroup()
.addContainerGap()
.addComponent(icon, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(pLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(data)
.addComponent(id)))
);
}
private void update(ItemsData id, boolean isSelected, JTable t)
{
this.name.setText(id.name);
}
@Override
public Object getCellEditorValue()
{
return null;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
ItemsData id = (ItemsData)value;
update(id, true, table);
return p;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
{
ItemsData id = (ItemsData)value;
update(id, isSelected, table);
return p;
}
}
我只添加了1个表格单元格,但它显示了两个。上面还有一些奇怪的文字。谁能改进我的代码,使其更好地工作?由于
解决方案
@Override
public Object getValueAt(int arg0, int arg1) {
return (item == null) ? null : item.get(arg0).name;
}
应该是
item.get(arg0).name;
代替
item.get(arg0); // It returns object to ItemsData class.
您也可以这样做,通过重写ItemsData
类的toString()
方法以及在您现有的代码中使用item.get(arg0);
class ItemsData{
public ItemsData(String string) {
name = string;
}
public String name;
@Override
public String toString(){
return name;
}
}