当前位置: 代码迷 >> J2SE >> 怎么调整JTable的宽度?
  详细解决方案

怎么调整JTable的宽度?

热度:182   发布时间:2016-04-24 13:16:10.0
如何调整JTable的宽度????
如何调整JTable的宽度????

------解决方案--------------------
bookTableModel = new BookTableModel();
bookTable = new JTable(bookTableModel);
//调整table的列宽度
int widths[] = {30, 150, 40, 30, 160};
for(int i=0; i<bookTable.getColumnCount(); i++) {
TableColumn tc = bookTable.getColumnModel().getColumn(i);
tc.setMinWidth(widths[i]);
tc.setPreferredWidth(widths[i]);
}

------解决方案--------------------
Java code
protected  int autoResizeMode           确定表是否自动调整列的宽度以占用表的整个宽度,以及如何进行调整。
------解决方案--------------------
用布局管理器,不知道是不是这个意思

import javax.swing.*;
import java.awt.*;

public class Frame1 extends JFrame 
{
JScrollPane jScrollPane1 = new JScrollPane();
JTable table1 = new JTable();
GridBagLayout gridBagLayout1 = new GridBagLayout();
Object[] tableheader =new Object[]{"column1","column1","column1","column1","column1"};
javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel();
public Frame1() 
{
try 
{
jbInit();
model.setColumnIdentifiers(tableheader);
table1.setModel(model);
}
catch(Exception ex) {
ex.printStackTrace();
}
}

void jbInit() throws Exception 
{
this.getContentPane().setLayout(gridBagLayout1);
this.setLocale(java.util.Locale.getDefault());
this.getContentPane().add(jScrollPane1, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0
,GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(35, 57, 138, 79), -67, -264));
jScrollPane1.getViewport().add(table1, null);
}
public static void main(String arg[])
{
Frame1 frame = new Frame1();
frame.setSize(100,100);
frame.show();
}
}
  相关解决方案