0

我正在制作一个程序,该程序通过 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);
      }

    }

编辑:: 刚刚注意到,当我最小化程序并再次打开它时,我在线程“AWT-EventQueue-0”java.lang.NullPointerException 中得到一个异常,即使我没有重新运行,单元渲染似乎也被再次调用该程序。

关于如何清除它或为什么它导致程序变黑的任何想法?

4

0 回答 0