gpt4 book ai didi

java - DefaultTableModel 突出显示单击的单元格

转载 作者:行者123 更新时间:2023-12-01 11:35:50 25 4
gpt4 key购买 nike

我正在制作一个 GUI 简单的日历程序。我使用 DefaultTableModel 和 DefaultTableCellRenderer 显示日历的月 View 。我的日历设置为突出显示今天的日期。我希望它还可以用其他颜色突出显示用户点击的日期。我已将鼠标监听器附加到每个单元格,并且能够检索这些单元格的日期,但我不知道如何让 tblCalendarRenderer 更新(至少不正确)。我让它开始突出显示一些用相同颜色单击的日期,但它只是像其他所有单击一样突出显示我单击的前一个日期,而不是当前日期。它还将开始突出显示多个,然后在某个时刻刷新。有人可以告诉我如何正确完成它吗?

这是一些代码:

public static void monthView(final int month, int year){
//print month calendar

int nod, som; //Number Of Days, Start Of Month

//Clear table
for (int i = 0; i < 6; i++){
for (int j = 0; j < 7; j++){
MyCalendarTester.mtblCalendar.setValueAt(null, i, j);
}
}

if(initial){ // to make sure that the mouse listeners are only created once.
initial = false;
MyCalendarTester.tblCalendar.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent event) {
// TODO Auto-generated method stub
int rowEvent = MyCalendarTester.tblCalendar.getSelectedRow();
int colEvent = MyCalendarTester.tblCalendar.getSelectedColumn();

System.out.print(arrayOfMonths[month].toString() + " ");
MyCalendarTester.realDay = Integer.parseInt(MyCalendarTester.tblCalendar.getValueAt(MyCalendarTester.tblCalendar.convertRowIndexToModel(rowEvent),
MyCalendarTester.tblCalendar.convertColumnIndexToModel(colEvent)).toString());

System.out.print(MyCalendarTester.tblCalendar.getValueAt(MyCalendarTester.tblCalendar.convertRowIndexToModel(rowEvent),
MyCalendarTester.tblCalendar.convertColumnIndexToModel(colEvent)) + " ");
System.out.println(MyCalendarTester.currentYear);


//Apply renderers
MyCalendarTester.tblCalendar.setDefaultRenderer(MyCalendarTester.tblCalendar.getColumnClass(0), new tblCalendarRenderer());
MyCalendarTester.tblCalendar.getCellRenderer(MyCalendarTester.tblCalendar.convertRowIndexToModel(rowEvent), MyCalendarTester.tblCalendar.convertColumnIndexToModel(colEvent));
}

});
//Draw calendar
for (int i=1; i<=nod; i++){
int row = new Integer((i+som-2)/7);
int column = (i+som-2)%7;
MyCalendarTester.mtblCalendar.setValueAt(i, row, column);

}
//Apply renderers
MyCalendarTester.tblCalendar.setDefaultRenderer(MyCalendarTester.tblCalendar.getColumnClass(0), new tblCalendarRenderer());

}

static class tblCalendarRenderer extends DefaultTableCellRenderer{
public Component getTableCellRendererComponent (JTable table, Object value, boolean selected, boolean focused, int row, int column){
super.getTableCellRendererComponent(table, value, selected, focused, row, column);
if (column == 0 || column == 6){ //Week-end
setBackground(new Color(255, 220, 220));
}
else{ //Week
setBackground(new Color(255, 255, 255));
}
if (value != null){
if (Integer.parseInt(value.toString()) == MyCalendarTester.realDay &&
MyCalendarTester.currentMonth == MyCalendarTester.realMonth && MyCalendarTester.currentYear == MyCalendarTester.realYear){ //Today
setBackground(new Color(220, 220, 255));
}
}
setBorder(null);
setForeground(Color.black);
return this;
}
}

最佳答案

感谢 HovercraftFullOfEels 解决了这个问题!我刚刚添加:

        if(selected && value != null){
setBackground(new Color(100,200,50));
}

到渲染器。

关于java - DefaultTableModel 突出显示单击的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30007622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com