帧最小化时,JFrame数据消失

时间:2014-03-22 05:02:17

标签: java swing jtable jframe jtextfield

我正在开发一个Java JFrame应用程序

  1. JTextField
  2. JTable
  3. 中存储在数据库中的文本字段数据,并通过表格显示。

    它从数据库成功显示数据。但问题是当我最小化表格数据每次都消失时。即使用户最小化框架,是否还有显示数据的选项?

    或者,有没有其他方法可以解决这个问题?

    代码:

    public void insertTaskDetail(){
        global.dbConnect();
        String insertkeydetail = "{call sp_inserttaskdetail(?,?,?,?)}";
        try{
            CallableStatement callableStatement;
            callableStatement = global.con.prepareCall(insertkeydetail);
            System.out.println("Stored Procedure Insert Key Detail is Called");
            String dt=global.getCurrentDateTime();
            System.out.println(dt);
            callableStatement.setString("p1", jText_Task_Name.getText().toString());
            callableStatement.setString("p2", dt);
            callableStatement.setString("p3", "0");
            callableStatement.setString("p4", L_Id);
            callableStatement.executeUpdate();
            System.out.println("Task Detail is successfully inserted in database");
            global.con.commit();
            showtask();
        }catch (Exception e){
            System.out.println("Sql Exception Add Task Changed:"+e.toString());
        }
    }
    
    public void showtask(){
        global.dbConnect();
        String showtask = "{call sp_showtask(?,?)}";
        try{
            callableStatement = global.con.prepareCall(showtask, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            System.out.println("Stored Procedure Get Task Detail is Called");
            callableStatement.setString("p1", L_Id);
            callableStatement.setString("p2", "false");
            rs=callableStatement.executeQuery();
            System.out.println(rs.toString());
            //Task , Start Time , Finish Time , Completed
            String task, st;
            boolean cmp;
            int i=0;
            while(rs.next()){
                i++;
            }
    
            data=new Object[i][];
            int j=0;
            rs.first();
            if(i>0){
                do{
                    System.out.println(i);
                    task=rs.getString("Task");
                    System.out.println(task);
                    st=rs.getString("Start Time");
                    cmp=rs.getBoolean("Completed");
                    data[j] = new Object[]{task, st, cmp};
                    j++;
                }while(rs.next());
            }
        }catch (SQLException e){
            System.out.println("Sql Exception:"+e.toString());
        }
        jText_Task_Name.setText("");
        TableDataModel();
    }
    
    public void TableDataModel(){
        String[] cols = {"<html>Task<br>Name</html>", "<html>Start<br>Time</html>", "Finished"};
        model = new DefaultTableModel(data, cols){
            @Override
            public Class<?> getColumnClass(int col){
                return col == 2 ? Boolean.class : String.class;
            }
    
            @Override
            public boolean isCellEditable(int row, int column){
                return column==2 ? true : false;
            }
        };
    
        table = new JTable(model){
            @Override
            public boolean isCellEditable(int row, int column) {
                return column==2 ? true : false;
            }
        };
    
        table.setFont(new java.awt.Font("Times New Roman", 0, 14));
        table.getTableHeader().setFont(new java.awt.Font("Times New Roman", 0, 14));
        table.getTableHeader().enable(false);
    
        JCheckBox jcheckBox=new JCheckBox();
        TableColumnModel columnModel = table.getColumnModel();
        System.out.println("Column Model:"+columnModel.toString());
        columnModel.getColumn(2).setCellEditor(new DefaultCellEditor(jcheckBox));
    
        jcheckBox.addChangeListener(new ChangeListener(){
            @Override
            public void stateChanged(ChangeEvent e){
                int rowCount = model.getRowCount();
                for(int i=0; i<rowCount; i++){
                    Boolean selected = (Boolean)model.getValueAt(i, 2);
                    if(selected != null && selected){
                        System.out.println("Removed:"+i);
                        model.removeRow(i);
                        i--;
                    }
                }
            }
        });
    
        JScrollPane jScrollpane=new JScrollPane(table);
        jScrollpane.setViewportView(table);
        this.add(jScrollpane);
        jScrollpane.setFont(new java.awt.Font("Times New Roman", 0, 14));
        jScrollpane.setBounds(10, 130, 295, 190);
    
        call++;
    }
    

1 个答案:

答案 0 :(得分:1)

根据您的示例,我会得出结论,您使用的是null布局管理器,这就是您遇到问题的原因。

使用适当的layout managers并让它为您处理