我正在创建一个程序,通过jcombo框从数据库中选择类,然后使用类时间填充时间表。
我使用了一个自定义的cellrenderer,它扩展了DefaultTableCellRenderer,以文本包装信息并更改单元格的背景。
它可以工作,但是当我最小化程序时,它会全黑,如果我双击,我会在时间表上打开一个新的JTextArea。
任何人都知道为什么会这样吗?
这是我使用的代码,感谢您的帮助。
public class TimesTableCellRenderer extends DefaultTableCellRenderer {
private int minHeight = -1;
private final JLabel label = new JLabel();
private final JTextArea textArea = new JTextArea();
public TimesTableCellRenderer(){
super();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setBackground(Color.cyan);
}
public Component getTableCellRendererComponent(
JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column) {
Component c = null;
if(value.toString()!=null){
textArea.setText(value.toString());
c = textArea;
setSize(table.getColumnModel().getColumn(column).getWidth(),130);
setVisible(true);
}else{
System.out.println("error");
}
return c;
}
}
以及他们如何进入时间表就是这样
timeTable.getColumnModel().getColumn(1).setCellRenderer(new TimesTableCellRenderer());
这是一个代码实例的例子,虽然有些东西已被更改,但更容易表示,因为它是更大程序的一部分。
public class NewTimeTable {
JFrame s;
public NewTimeTable(JFrame s){
this.s=s;
timetable();
s.setVisible(true);
}
private JTable table;
public JComboBox ModuleComboBox;
public JTextArea DemonstratorTextArea;
// public JTextPane DemonNamesTextPane;
public JList demonNamesJList;
public JPanel DemonNamesJPanel;
JTable timeTable;
JPanel frmMain;
Container pane;
static DefaultTableModel defaultTimeTableModule; // Table model
JScrollPane scrollbarTT; // The scrollpane
JPanel TimeTablePanel; // The panel
public void timetable() {
// creates the table container and where it is in the program
defaultTimeTableModule= new DefaultTableModel();
timeTable = new JTable(defaultTimeTableModule);
scrollbarTT = new JScrollPane(timeTable);
TimeTablePanel = new JPanel(null);
TimeTablePanel.setBounds(554, 89, 715, 552);
TimeTablePanel
.setBorder(BorderFactory.createTitledBorder("Time Table"));
TimeTablePanel.setBackground(Color.white);
pane = s.getContentPane();
pane.add(TimeTablePanel);
TimeTablePanel.add(scrollbarTT);
scrollbarTT.setBounds(22, 44, 668, 467);
// gets the headers and stops them being reordered
// allows for column and cell selections
timeTable.getTableHeader().setReorderingAllowed(false);
timeTable.setColumnSelectionAllowed(false);
timeTable.setRowSelectionAllowed(false);
timeTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// Add headers
String[] headers = { "Time", "Mon", "Tue", "Wed", "Thu", "Fri " };
for (int i = 0; i < 6; i++) {
defaultTimeTableModule.addColumn(headers[i]);
}
// Set background to white and draws in the grid
timeTable.getParent().setBackground(timeTable.getBackground());
timeTable.setRowHeight(35);
defaultTimeTableModule.setColumnCount(6);
defaultTimeTableModule.setRowCount(14);
timeTable.setSize(135, 35);
timeTable.setRowHeight(65);
// add numbers to time column
Integer count = 8;
for (int i = 0; i < 14; i++) {
count++;
String time=null;
if(count>12){
time=count+".00 pm";
}else{
time=count+".00 am";
}
defaultTimeTableModule.setValueAt(time, i, 0);
}
timeTable.setDropMode(DropMode.ON_OR_INSERT);
}
public void addClassesToTimeTable(ArrayList<String> moduleInfo){
int startRow = 0;
int finishRow = 0;
int column = 0;
int c2 = 0;
String startTime=moduleInfo.get(0);
System.out.println(startTime);
String finishTime=moduleInfo.get(1);
System.out.println(finishTime);
String day=moduleInfo.get(2);
System.out.println(day);
double startTimeCell= Double.parseDouble(startTime);
double finishTimeCell= Double.parseDouble(finishTime);
switch (day) {
case "Monday":
case "monday":
column=1;
break;
case "Tuesday":
case "tuesday":
column=2;
break;
case "Wednesday":
case "wednesday":
column=3;
break;
case "Thursday":
case "thursday":
column=4;
break;
case "Friday":
case "friday":
column=5;
break;
default:
//turn into pop up message
System.out.println("error in database on day");
break;
}
//count that tells you what row to place info on in start time
int count = 0;
int count2=0;
for (int loop = 9; loop <= startTimeCell; loop++) {
startRow=count;
count++;
}
for (int loop = 9; loop <= finishTimeCell; loop++) {
finishRow=count2;
count2++;
}
String name = moduleInfo.get(3);
defaultTimeTableModule.setValueAt(name, startRow, column);
timeTable.isCellEditable(finishRow, column);
timeTable.getColumnModel().getColumn(column).setCellRenderer(new TimesTableCellRenderer());
timeTable.setAutoResizeMode(112);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
frame.setBounds(0, 0, screen.width, screen.height - 30);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.white);
NewTimeTable s=new NewTimeTable(frame);
ArrayList<String> a = new ArrayList<>();
a.add("9.00");
a.add("10.00");
a.add("Monday");
a.add("name");
s.addClassesToTimeTable(a);
}
}
编辑::刚注意到当我最小化程序并再次打开它时我在线程中得到一个异常&#34; AWT-EventQueue-0&#34; java.lang.NullPointerException和单元格渲染似乎再次被调用,即使我没有重新运行该程序。
有关如何清除此问题或为何导致程序变黑的任何想法?