我的项目涉及JTable和GUI。用户通过添加"事务"来填充JTable。 (表示为一行)然后可以复制或删除这些"交易"随意。
我已决定要在表格中添加更多颜色。特别是我希望每个其他标题和列都有一个带有白色文本的黑色背景,以便于查看。
我已经能够编写完成列的代码了。我也能够改变所有列标题的颜色。我似乎遇到的问题是动态更改单个列标题(就像我做列一样)。我在这里提到了这个问题:JTable header background color但我很困惑。发布的解决方案非常复杂,我无法将其转换为我的个人代码。以下是我到目前为止的情况:
Seq[Map[String, Any]]
如你所见,我使用"标志"布尔值以确保每个其他列都已更改。以下是更改列的代码:
val random = new Random
val someone = session(random.nextInt(session.length))
http(someone("age")) ...
在试验过程中,我可以设置所有标题:
tbl = new JTable();
String header[] = new String[]{
"Notification Reference Number", "Amount","Credit/Debit Indicator","Initiating Party Id",
"Initiating Party Scheme Name", "Debtor Name","Debtor Zip Code","Debtor Town",
"Debtor Country Sub-Division","Debtor Address","Debtor Agent Clearing System Id","Debtor Agent Name",
"Creditor Agent Clearing System Id",
"Creditor Agent Name",
"Return/Reject Code","Return/Reject Additional Info",
"Payment Instrument","Type of Payment","Pass-Thru Data","Clearing Account","Transaction Type",
"Return Occurence","Retired Indicator","Representment Method",
"Check-Split Indicator","Check Split Amount","Corporate Check Indicator","Source Batch Identifier"
};
dtm.setColumnIdentifiers(header);
tbl.setModel(dtm);
tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scroll = new JScrollPane(tbl);
scroll.getViewport().setBackground(Color.gray);
boolean flag = false;
for(int i = 0; i < header.length; i++){
tbl.getColumnModel().getColumn(i).setPreferredWidth(200);
if(flag==false){
TableColumn tm = tbl.getColumnModel().getColumn(i);
tm.setCellRenderer(new ColorColumnRenderer(Color.black,Color.white));
flag=true;
}else if(flag==true){
TableColumn tm = tbl.getColumnModel().getColumn(i);
tm.setCellRenderer(new ColorColumnRenderer(Color.white, Color.black));
flag=false;
}
}
我尝试过很多编码特定标题的方法,但都不成功。我确信解决方案很简单,我无法弄清楚。
我应该注意,我为没有发布完整的代码示例而道歉。这段代码实际上非常复杂,所以我希望有人能够在创建新的JTable之前轻松确定我需要做什么,然后尝试隔离这个例子 - 尽管我会在必要时这样做。