0

I am creating an application in Java which retrieves some information from a MySQL database and then displays it in a JTable on the screen.

The code I am using to get and put the information into a DefaultListModel<> is shown below. The DefaultListModel<> is then fed to the JTable which displays the information onto the screen.

SwingWorker<Void, Void> worker = new SwingWorker< Void, Void>() {

        @Override
        public Void doInBackground() {

            String SQL = "SELECT * FROM AddABill";

            try {
                Statement stmt = GlobalVars.conn.createStatement();

                ResultSet rs = stmt.executeQuery(SQL);

                menuOptions.clear();

                while(rs.next()){

                    menuOptions.addElement(rs.getString(1) + " - " + rs.getString(2));
                }

                rs.close();
                stmt.close();
            }

            catch (SQLException e) {
                System.out.println("An Error Was Detected! :/");
            }

            return null;
        }

        @Override
        public void done(){
            loadingGif.setVisible(false);
        }

    };

    worker.execute();

The issue I am having is that sometimes the information is displayed onto the screen and sometimes it isn't. Since there is no change in the query used I am assuming it is something to do with the MySQL connection.

The JTable and MenuOptions are setup like this

menuOptions = new DefaultListModel<>();
menuOptions.addElement("");

menuList = new JList<>(menuOptions);
menuList.setFont(new Font("Myriad Hebrew", Font.PLAIN, 32));
menuList.setBackground(Color.decode("#005952"));
menuList.setForeground(Color.decode("#BEC423"));
menuList.setSelectionBackground(Color.decode("#660033"));
menuList.setSelectionForeground(Color.decode("#FFFFFF"));

So the question I am wondering is how can I make sure that the information is always displayed on the screen. A picture of when it is displayed and not displayed is shown below.

Displayed:

Displayed

Not Displayed:

Not Displayed

On a final note to get the information to show I have to keep on re-running the code until it is eventually shown.

Any help is much appreciated.

Andrew Thompson
  • 166,747
  • 40
  • 210
  • 420
thomasjcf21
  • 73
  • 1
  • 8
  • 2
    1. everything inside `doInBackground()` is out of `EDT`, you have to notify GUI inside `publish()` ---> `process()`, `setProcess()` or `done()`, 2. `JTable` != `DefaultListModel` 3. to reset `DefaultListModel`, run `SwingWorker`, where inside `publish() ---> process()`, `setProcess()` or `done()` to add a new `Item` to `DefaultListModel`, nothing esle (clear for me) to your code or description – mKorbel Jul 21 '16 at 16:28
  • @mKorbel 1) For the rest of the application changes to the GUI from `doInBackground()` have worked. 2) I know the `DefaultListMenu` is what supplies the `JTable` with the information. If the problem is because I'm changing the code in the `doInBackground()` function then I'm confused as to why sometimes it works and sometimes it doesn't. – thomasjcf21 Jul 21 '16 at 16:38
  • 1
    It may work _sometimes_, but it is not reliable; more [here](http://stackoverflow.com/a/7158505/230513) and [here](http://stackoverflow.com/a/38499763/230513). – trashgod Jul 21 '16 at 18:37

1 Answers1

0

@trashgod @mKorbel

Thanks for the help guys, I have solved the issue by adding the information into the table from the done() function. The code is below

SwingWorker<Void, Void> worker = new SwingWorker< Void, Void>() {

        String[] billItems = null;

        @Override
        public Void doInBackground() {

            String SQL = "SELECT * FROM AddABill";

            try {
                Statement stmt = GlobalVars.conn.createStatement();

                ResultSet rs = stmt.executeQuery(SQL);

                //Set The Cursor To Last Position
                rs.last();

                int totalRows = rs.getRow();

                billItems = new String[totalRows];

                //Set The Cursor Back To The Start
                rs.beforeFirst();

                while(rs.next()){
                    int rowNum = rs.getRow() - 1;
                    billItems[rowNum] = rs.getString(1) + " - " + rs.getString(2);
                }

                rs.close();
                stmt.close();
            }

            catch (SQLException e) {
                System.out.println("An Error Was Detected! :/");
            }

            return null;
        }

        @Override
        public void done(){
            menuOptions.clear();

            for(int i = 0; i < billItems.length; i++){
                menuOptions.addElement(billItems[i]);
            }

            loadingGif.setVisible(false);
        }

    };

Thanks,

Thomas.

thomasjcf21
  • 73
  • 1
  • 8