调整表第一列的大小,即在java中显示列的数字
的示例代码
我包括我的应用程序第一类
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jdesktop.swingx.JXTable;
import javax.swing.table.TableColumn;
public class setcolwidth {
public static void main(String args[]) {
final Object rowData[][] = {
{"kirti", "one", "I"},
{"kkk", "two", "II"},
{"kiki", "three", "III"}};
final String columnNames[] = {"#", "English", "Roman"};
final JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JXTable lineTable = new LineNumberTable(table);
scrollPane.setRowHeaderView(lineTable);
table.getTableHeader().setReorderingAllowed(false);
JFrame frame = new JFrame("Resizing Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
TableColumn column = null;
int i=0;
try{
column = lineTable.getColumnModel().getColumn(i);
column.setPreferredWidth(50);
}catch(Exception ex){
System.out.println("Error for the column index"+i+" Err"+ex.toString());
}
frame.setSize(300, 150);
frame.setVisible(true);
}
}
和第二类
import javax.swing.JTable;
import javax.swing.table.*;
import org.jdesktop.swingx.JXTable;
public class LineNumberTable extends JXTable {
private JTable mainTable;
public LineNumberTable(JTable table) {
super();
mainTable = table;
setAutoCreateColumnsFromModel( false );
setModel( mainTable.getModel() );
setAutoscrolls( false );
addColumn( new TableColumn() );
getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer());
// mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getColumnModel().getColumn(0).setPreferredWidth(40);
setPreferredScrollableViewportSize(getPreferredSize());
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Object getValueAt(int row, int column) {
return Integer.valueOf(row+1);
}
@Override
public int getRowHeight(int row) {
return mainTable.getRowHeight();
}
}
,但是行号列没有调整大小。
I include sample code of my application
1st class
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import org.jdesktop.swingx.JXTable;
import javax.swing.table.TableColumn;
public class setcolwidth {
public static void main(String args[]) {
final Object rowData[][] = {
{"kirti", "one", "I"},
{"kkk", "two", "II"},
{"kiki", "three", "III"}};
final String columnNames[] = {"#", "English", "Roman"};
final JTable table = new JTable(rowData, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
JXTable lineTable = new LineNumberTable(table);
scrollPane.setRowHeaderView(lineTable);
table.getTableHeader().setReorderingAllowed(false);
JFrame frame = new JFrame("Resizing Table");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(scrollPane, BorderLayout.CENTER);
TableColumn column = null;
int i=0;
try{
column = lineTable.getColumnModel().getColumn(i);
column.setPreferredWidth(50);
}catch(Exception ex){
System.out.println("Error for the column index"+i+" Err"+ex.toString());
}
frame.setSize(300, 150);
frame.setVisible(true);
}
}
and 2nd class
import javax.swing.JTable;
import javax.swing.table.*;
import org.jdesktop.swingx.JXTable;
public class LineNumberTable extends JXTable {
private JTable mainTable;
public LineNumberTable(JTable table) {
super();
mainTable = table;
setAutoCreateColumnsFromModel( false );
setModel( mainTable.getModel() );
setAutoscrolls( false );
addColumn( new TableColumn() );
getColumnModel().getColumn(0).setCellRenderer(mainTable.getTableHeader().getDefaultRenderer());
// mainTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
getColumnModel().getColumn(0).setPreferredWidth(40);
setPreferredScrollableViewportSize(getPreferredSize());
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Object getValueAt(int row, int column) {
return Integer.valueOf(row+1);
}
@Override
public int getRowHeight(int row) {
return mainTable.getRowHeight();
}
}
But the row number column is not resizing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为您要在列上设置首选大小,并且需要在行标题的
JViewport
上设置它。在这一行之后:
试试这个:
That is because you are setting the preferred size on the column and need to be setting it on the
JViewport
on the row header instead.After this line:
Try this:
我不使用 JXTable,所以无法发送您的代码。但对于 JTable 来说,你的基本逻辑在我看来没问题。
看看我的行号表,它对我有用。也许差异在于我创建 TableColumn 并分配标题值的方式。
I don't use JXTable so I can't text your code. But your basic logic looks ok to me for a JTable.
Take a look at my Row Number Table which works for me. Maybe the difference is the way I create the TableColumn and assign a header value.