[英]Setting JTable Column width from another class
public TableModel getTableData() {
TableModel model=null;
try {
String sql = "SELECT eventID as 'Event ID', date as 'Date',eventName as 'Name', time as 'Start Time' FROM Event";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
model = (DbUtils.resultSetToTableModel(rs));
//CODE TO SET WIDTH OF JTABLE IN VIEWS
}
catch(SQLException e){
e.printStackTrace();
}
finally {
try {rs.close(); pst.close();}
catch(SQLException e){} }
return model;
}
Above is a method of the Model class, responsible for setting up the table model for the table I have in Views called tableEvent
. 上面是Model类的方法,负责为我在Views中名为
tableEvent
的表设置表模型。
The Controller class uses the above method to set change the state of the tableEvent
of Views
Class. Controller类使用上述方法来设置
Views
类的tableEvent
的更改状态。 Please note tableEvent
in Views is empty/nothing there. 请注意,Views中的
tableEvent
为空/无。 Model passes values of rs
to Controller
which will then change the display of the table and populate values. 模型将
rs
值传递给Controller
, Controller
随后将更改表的显示并填充值。
Heres what I mean: 这是我的意思:
view.tableEvent.setModel(model.getTableData());
However, I am trying to set the width of the columns in Model
rather than in Views
through tableEvent.getColumnModel().getColumn(1).setPreferredWidth(60);
但是,我试图通过
tableEvent.getColumnModel().getColumn(1).setPreferredWidth(60);
在Model
而不是在Views
设置列的宽度tableEvent.getColumnModel().getColumn(1).setPreferredWidth(60);
. 。 if I pass the table itself to the method
getTableData()
in order to set the width of the column, it doesnt really work. 如果我将表本身传递给
getTableData()
方法以设置列的宽度,则它实际上不起作用。 Is there a way I can specify my desired width of the first 4 column of the table in Model class in geTableData() method somehow so that it is consistent at all times. 有没有一种方法可以某种方式在geTableData()方法中的Model类中指定表的前4列的所需宽度,以使其始终保持一致。 Right now I have this at part of the constructor of Views class which obviously means its only to my desired width only at initialisation.
现在,我在Views类的构造函数中拥有此函数,这显然意味着仅在初始化时才达到我期望的宽度。
If the JTable
or TableModel
are empty and have no column information, then you need to supply that information yourself. 如果
JTable
或TableModel
为空并且没有列信息,那么您需要自己提供该信息。
You could either create a new TableColumnModel
, add the required TableColumns
to it, providing the width information you want/need and apply that model to the JTable
. 您可以创建一个新的
TableColumnModel
,将所需的TableColumns
添加到其中,提供所需的宽度信息,然后将该模型应用于JTable
。
You would then need to fill in the TableModel
and supply that to the JTable
as well. 然后,您需要填写
TableModel
并将其提供给JTable
。
I would, personally, extract the meta data from the ResultSet
BEFORE I tried retrieving the data from it. 我个人将尝试从
ResultSet
提取元数据,然后再尝试从中获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.