0

I wrote the following code that I thought would work, I wanted to open a new JFrame on a button click that shows a progress bar that gets updated:

b3.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
    int numOfRows = getRows();//function that returns number of rows
    int numOfColumns = getColumns();//function that returns number of columns

    String myquery = "select * from foo_table";
    rs = null;        
    try {
      rs = st.executeQuery(myquery);
      // extract column information
      ResultSetMetaData rsmd = rs.getMetaData(); 
      int columnCount = rsmd.getColumnCount();
      columnData = new ArrayList<String>(columnCount);
      for (int i = 1; i <= columnCount; i++) {
        columnData.add(rsmd.getColumnName(i));
      }
      // sql result data
      table_ResQues.setModel(new ListTableModel(Collections.<List<Object>>emptyList(), Collections.<String>emptyList())); 
      rowData = new ArrayList<List<Object>>(); 
      final JFrame progFrame = new JFrame("Processing...");
      JPanel mainPPanel = new JPanel(new BorderLayout());
      JProgressBar progBar = new JProgressBar(0,100);
      progFrame.setBounds(850,300,400,100);
      progFrame.setVisible(true);       
      progBar = new JProgressBar(0,100);
      mainPPanel.add(progBar, BorderLayout.NORTH);
      progFrame.add(mainPPanel);
      progFrame.setVisible(true);

      int countRows = 0;
      int area = numOfRows*numOfColumns;
      int totalTime = area % 200000;
      int xPerSec = totalTime/100;

      while (rs.next()) {
        row = new ArrayList<Object>(columnCount);
        for (int i = 0; i < columnCount; i++) {
          row.add(rs.getObject(i + 1));
        }
        rowData.add(row);
        countRows++;
        if(countRows*numOfColumns == 200000){
          progBar.setValue(xPerSec++);
          countRows = 0;
        }
      }     
      table_ResQues.setModel(new ListTableModel(rowData, columnData));

    }catch (SQLException e1) {
      JOptionPane.showMessageDialog(frame, e1.getMessage(), "SQL Exception", JOptionPane.ERROR_MESSAGE);
      e1.printStackTrace();
    }    
  });

But when googling my problem I realized it is a threading issue, and I never had any experience with threading or SwingWorker, can anyone help me implement what I want using SwingWorker? Should my while-loop be the one that's running in the background? should it be a class by itself? Sorry, i'm really confused.

Abdane
  • 137
  • 1
  • 12
  • 1
    Step back for a second. You need to start by separating the UI logic from the database logic. All the database logic needs to go into the `doInBackground` method of the `SwingWorker`. You need to then `publish` the progress, which is used by the `process` method to update the UI and finally, in the `done` method, you can `get` the results returned by the `doInBackground` method – MadProgrammer Oct 23 '17 at 19:36
  • 1
    *"open a new JFrame"* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Oct 24 '17 at 00:11

1 Answers1

0

I have solved it by creating a new class that extends SwingWorker and overriding it's methods. Thanks for the help in the comments.

Abdane
  • 137
  • 1
  • 12