-1

I am developing an Java JFrame application in this I had

  1. JTextField
  2. JTable

In that the data of text field stored in database and display it via table.

It display the data successfully from database. But the issue is when I minimize the frame the table data is gone every time. Is there any option to display data even user minimize the frame?

Or, is there any Other option to solve the issue?

Code:

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++;
}
Sandeep Chatterjee
  • 3,220
  • 9
  • 31
  • 47
Nirav Dabhi
  • 341
  • 1
  • 7
  • 29
  • Without a runnable example to demonstrate your problem, there's nothing anybody can possibly do... – MadProgrammer Mar 22 '14 at 05:07
  • Ok wait I post the code – Nirav Dabhi Mar 22 '14 at 05:08
  • Note: Uncompilable code snippet != runnable example. For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal **Complete** Tested and Readable Example). Hard code some data to replace the DB. – Andrew Thompson Mar 22 '14 at 05:14
  • `jScrollpane.setBounds(10, 130, 295, 190);` Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 22 '14 at 05:14
  • But I got Complete output means my program is running succesfully just the problem is data is not stable means when i minimise the window of jframe the data is gone from jtable. – Nirav Dabhi Mar 22 '14 at 05:17
  • 1
    `public void TableDataModel()` Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. – Andrew Thompson Mar 22 '14 at 05:24

1 Answers1

1

Based on your example, I would conclude you are using a null layout manager, which would be the reason you're having issues.

Make use of appropriate layout managers and let it take care of it for you

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366