Java JTable内容是中心对齐的

Java JTable内容是中心对齐的,java,swing,jtable,Java,Swing,Jtable,我使用MS Access数据库作为后端。我有个问题。检索并显示在小程序中后,内容会自动居中对齐,这看起来很尴尬。我不知道是哪部分代码导致了这种情况。因此,请帮助我诊断问题 public void addTable8(String query8) { SimpleDateFormat formatter8 = new SimpleDateFormat("dd/MM/yyyy"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDr

我使用MS Access数据库作为后端。我有个问题。检索并显示在小程序中后,内容会自动居中对齐,这看起来很尴尬。我不知道是哪部分代码导致了这种情况。因此,请帮助我诊断问题

public void addTable8(String query8)
{
   SimpleDateFormat formatter8 = new SimpleDateFormat("dd/MM/yyyy");
   try
   {
     Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
     Connection conn8=DriverManager.getConnection("jdbc:odbc:vasantham","","");
     Statement st8=conn8.createStatement();
     ResultSet rs8=st8.executeQuery(query8);
     ResultSetMetaData md8=rs8.getMetaData();
     int cols8=md8.getColumnCount();
     model8=new DefaultTableModel()
     {
       public Class getColumnClass(int col)
       {
          Object o = getValueAt(0, col);
          if(o == null)
             return Object.class;
          else
             return o.getClass();
       }
     };

model8.addColumn("Purpose");
model8.addColumn("Name");
model8.addColumn("Composition");
model8.addColumn("Expiry");
model8.addColumn("Stock");
model8.addColumn("Cost");
model8.addColumn("Cost/Tab");
model8.addColumn("Type");
model8.addColumn("Supplier");
model8.addColumn("Supplier Number");
model8.addColumn("Rack");

table8=new JTable(model8);
table8.setOpaque(true);
table8.setFillsViewportHeight(true);
table8.setBackground(new Color(255,255,208));
table8.getTableHeader().setBackground(new Color(255,228,181));

String[] tabledata8=new String[cols8];
int i=0;

while(rs8.next())
{
 for(i=0;i<cols8;i++)
 {
  if(i==3)
  {   
   Date intr8=(rs8.getDate(i+1));
   tabledata8[i]=formatter8.format(intr8);
  }
  else
   tabledata8[i]=rs8.getObject(i+1).toString();

 }
 model8.addRow(tabledata8);
}


panel8.removeAll();

table8.setDefaultRenderer(String.class, new CustomRenderer());
table8.setRowSelectionAllowed(true);
table8.setColumnSelectionAllowed(true);
table8.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

JScrollPane scroll8 = new JScrollPane(table8); 
panel8.setLayout(new BorderLayout());

panel8.add(scroll8,BorderLayout.CENTER);
frame8.add(panel8,BorderLayout.CENTER);
conn8.close();


 }
  catch(Exception e8)
  {
  e8.printStackTrace();
  }
}

 class CustomRenderer implements TableCellRenderer
 {
public CustomRenderer()
{
  label = new JLabel();
  label.setHorizontalAlignment(JLabel.CENTER);
  label.setOpaque(true);
  targetRow = -1;
  targetCol = -1;
}

public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column)
{
  if(isSelected)
  {
    label.setBackground(table.getSelectionBackground());
    label.setForeground(table.getSelectionForeground());
  }
  else
  {
    label.setBackground(table.getBackground());
    label.setForeground(table.getForeground());
  }
  if(row == targetRow && column == targetCol)
  {
    label.setBackground(new Color(176,196,222));

    label.setFont(table.getFont().deriveFont(Font.BOLD));
  }
  else
  {
    label.setBorder(null);
    label.setFont(table.getFont());
  } 
  label.setText((String)value);
  return label;
}

public void setTargetCell(int row, int col)
{
  targetRow = row;
  targetCol = col;
}
} 
public void addTable8(字符串查询8)
{
SimpleDataFormat Formatter 8=新的SimpleDataFormat(“dd/MM/yyyy”);
尝试
{
forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection conn8=DriverManager.getConnection(“jdbc:odbc:vasantham”,“”,“”);
语句st8=conn8.createStatement();
结果集rs8=st8.executeQuery(查询8);
ResultSetMetaData md8=rs8.getMetaData();
int cols8=md8.getColumnCount();
model8=新的DefaultTableModel()
{
公共类getColumnClass(int-col)
{
对象o=getValueAt(0,列);
如果(o==null)
返回Object.class;
其他的
返回o.getClass();
}
};
模式8.添加列(“目的”);
模型8.添加列(“名称”);
模型8.添加列(“组成”);
模型8.添加列(“到期”);
模型8.添加列(“股票”);
模型8.添加列(“成本”);
模型8.添加列(“成本/选项卡”);
model8.addColumn(“类型”);
型号8.添加栏(“供应商”);
型号8.添加栏(“供应商编号”);
型号8.添加柱(“支架”);
表8=新JTable(型号8);
表8.setOpaque(真);
表8.SetFillsViewPerthight(真);
表8.挫折背景(新颜色(255255208));
表8.getTableHeader().setBackground(新颜色(255228181));
String[]tabledata8=新字符串[cols8];
int i=0;
while(rs8.next())
{

for(i=0;i可能是
CustomRenderer
构造函数中的此行

label.setHorizontalAlignment(JLabel.CENTER);
  • 您的代码行
    label.setHorizontalAlignment(JLabel.CENTER);
    from
    class CustomRenderer implements TableCellRenderer
    将列的内容居中放置在
    中心

  • 阅读Oracle教程中的
    渲染器和编辑器概念


  • 在事件调度线程上执行DB查询是不好的做法,可能会导致阻塞的UI。考虑在后台线程上执行这些查询。