Results 1 to 6 of 6
  1. #1
    suwandibagus is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Question JTable > how to display the database to automatically based on time

    Dear All,

    I have made a program to display data from a database using netbeans. In the netbeans I created a JFrame Form. Here's a snippet of code that I created:

    ===============
    Java Code:
    public class Back extends javax.swing.JFrame {
    
        public Back() {
            initComponents();
            
            loadDataPrefix();
           
        }
    
    ....................
    
        public void loadDataPrefix(){
            DefaultTableModel dtmPrefix = new DefaultTableModel();
            dtmPrefix.addColumn("Id");
            dtmPrefix.addColumn("Operator");
            dtmPrefix.addColumn("Prefix");
            try {
                String queryPrefix = "SELECT * FROM prefix ORDER BY prefix_name";
                statementPrefix = connection.createStatement();
                resultSetPrefix = statementPrefix.executeQuery(queryPrefix);
                int size = 0;
                while(resultSetPrefix.next()){
                    size++;
                    dtmPrefix.addRow(new Object[]{
                        size,
                        resultSetPrefix.getString(3),
                        resultSetPrefix.getString(2)
                    });
                    jTable4.setModel(dtmPrefix);
                }
            } catch (SQLException preEr) {
                JOptionPane.showMessageDialog(null, preEr,"Peringatan !",JOptionPane.WARNING_MESSAGE);
            }
        }
    ......................
    }
    ===============

    I created a button to update the database in JTable display as follows:
    ===============
    Java Code:
    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
    	loadDataPrefix();
    }
    ===============
    Everything went as desired.

    What I need is to make a checkbox, which when select the checkbox in JTable will display data based on the data that is in my mySQL database automatically.

    For example, when I enter data or edit data to MySQL (via DOS or phpmyadmin or other applications) then changes to a JTable data is displayed automatically.

    And when chexkBox not select it to display the database changes have to click a button.

    Kindly enlighten and sample code completion. Thank you ....

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: JTable > how to display the database to automatically based on time

    You'll need something that polls the database then.

    The simplest would be to reload the table every so often.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: JTable > how to display the database to automatically based on time

    I'll shift this to a better part of the forum.
    Since it's a JTable I'll go for the Swing forum rather than JDBC.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    suwandibagus is offline Member
    Join Date
    Sep 2014
    Posts
    2
    Rep Power
    0

    Default Re: JTable > how to display the database to automatically based on time

    Thank you

    My difficulty is to make the automatic reload on JTable. code to reload I have prepared in the method - loadDataPrefix (); - Which will be displayed in the JTable.

    I did this when designing in Netbeans:

    I created a button that when clicked call the method - loadDataPrefix ();
    When I run, and I click the button, successfully .... the data appear in the JTable.

    ====

    I've made ​​the code used to redial method runable - loadDataPrefix (); - Every 2 minutes or so. And it works when I display the output on the standard output of netbeans. But it does not apply to the display jTabel designed using netbeans.

    Please explanations and examples of simple code to reload chechBox JTable when I select (automatic reload) and automatic reload will stop when I do not select the checkbox.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default Re: JTable > how to display the database to automatically based on time

    "I've made ​​the code used to redial method runable - loadDataPrefix (); - Every 2 minutes or so. And it works when I display the output on the standard output of netbeans. But it does not apply to the display jTabel designed using netbeans."

    So, what does that code look like?
    Is the database monitor in its own thread?
    If not, then that's your problem.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default Re: JTable > how to display the database to automatically based on time

    I created a button that when clicked call the method - loadDataPrefix ();
    When I run, and I click the button, successfully .... the data appear in the JTable.
    So when you automate the process make sure you invoke the loadDataPrefix() method. That method should work because you recreate the TableModel every time. So as long as your code only has a single reference to the jTable4 variable, the code should work. Make sure you are using a Swing Timer to schedule the update because all updates to Swing components should be done on the Event Dispatch Thread.

Similar Threads

  1. Replies: 1
    Last Post: 08-02-2011, 09:12 AM
  2. Replies: 0
    Last Post: 05-25-2011, 06:38 PM
  3. Replies: 26
    Last Post: 02-25-2010, 11:56 AM
  4. Replies: 0
    Last Post: 12-04-2008, 05:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •