Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By coderX
  • 1 Post By Lionlev

Thread: Dynamic jTextArea

  1. #1
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Dynamic jTextArea

    I'm trying to retrieve data from mysql table into a jTextArea. When I insert a data into the the table I want it to appear in the textarea without having to use a refresh button,so that it gets updated automatically while the program is running.
    Here is what I tried:
    Java Code:
             private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
               int i,a;
               i=1;
               a=100;
               while(i<=a){ // <--- Loop for repeating the function
                   try {
                conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root" ,"db");
                String SQL = "SELECT * FROM table  " ;
                st = conn.createStatement();
          	    rset = st.executeQuery(SQL);
               
               
               while (rset.next()){
                   
                    jTextArea1.append(rset.getString("Info"));
                  
               
               }
              
            }
            catch(Exception e) {
                System.out.println("Error");
            }
               } 
    }
    But when I use a loop,the program freezes and the UI becomes unresponsive.
    Can someone help me in making the above function(retrieving data from database) repeat itself in regular intervals without a loop,so that the UI doesnt go unresponsive?

  2. #2
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Dynamic jTextArea

    You should probably use threads
    implements Runnable
    and add run() method, inside of the run create a while loop that will do the same as refresh button.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  3. #3
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    Quote Originally Posted by Lionlev View Post
    You should probably use threads
    implements Runnable
    and add run() method, inside of the run create a while loop that will do the same as refresh button.

    I dont know what threads,implements runnable are,but does it go like this?
    Java Code:
    class NewThread implements Runnable {
             public void run() {
                 //code for refresh button
             }
         }

  4. #4
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Dynamic jTextArea

    Yup, that's how it works.

  5. #5
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    Since I'm using NetBeans I have no idea where to put thread and implements runnable,can someone tell me where to put it?here is the whole code:

    Java Code:
    
    import java.sql.*;
    import javax.swing.JOptionPane;
    
    
    
    
    public class Main extends javax.swing.JFrame {
        Connection conn = null;
        Statement st,s = null;
        
        ResultSet rset = null;
        ResultSet rs =null;
        /**
         * Creates new form Main
         */
       
         public  MainGUI() {
            initComponents();
            
            
        }
        
        
              
            @SuppressWarnings("unchecked")
        
        private void initComponents() {
    
            jPanel2 = new javax.swing.JPanel();
            jComboBox1 = new javax.swing.JComboBox();
            jScrollPane2 = new javax.swing.JScrollPane();
            jTextArea2 = new javax.swing.JTextArea();
            jScrollPane3 = new javax.swing.JScrollPane();
            jList1 = new javax.swing.JList();
            jButton1 = new javax.swing.JButton();
            jLabel1 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            jLabel3 = new javax.swing.JLabel();
            jCheckBox1 = new javax.swing.JCheckBox();
            jScrollPane1 = new javax.swing.JScrollPane();
            jTextArea1 = new javax.swing.JTextArea();
            jButton2 = new javax.swing.JButton();
    
                    pack();
        }
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
            
                     
    
          
        }
     
        private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
               
            
        }//GEN-LAST:event_jCheckBox1ActionPerformed
    
        private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
        }
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO add your handling code here:
           // <-----------------------------------------------REFRESH BUTTON CODE
            
        }
    
            public static void main(String args[]) {
                   try {
                for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                        javax.swing.UIManager.setLookAndFeel(info.getClassName());
                        break;
                    }
                }
            } catch (ClassNotFoundException ex) {
                java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            } catch (javax.swing.UnsupportedLookAndFeelException ex) {
                java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
            }
           
    
            /*
             * Create and display the form
             */
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Main().setVisible(true);
                    
                }
            });
            
        }
        // Variables declaration - do not modify//GEN-BEGIN:variables
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JCheckBox jCheckBox1;
        private javax.swing.JComboBox jComboBox1;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JLabel jLabel3;
        private javax.swing.JList jList1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JScrollPane jScrollPane2;
        private javax.swing.JScrollPane jScrollPane3;
        private javax.swing.JTextArea jTextArea1;
        private javax.swing.JTextArea jTextArea2;
        // End of variables declaration//GEN-END:variables
    }
    Abhi The Coder likes this.

  6. #6
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Dynamic jTextArea

    Quote Originally Posted by coderX View Post
    Since I'm using NetBeans I have no idea where to put thread and implements runnable,can someone tell me where to put it?here is the whole code:

    Java Code:
    public class Main extends javax.swing.JFrame {
    Java Code:
    public class Main extends javax.swing.JFrame implements Runnable
    Since you don't know much about threads you must start the thread
    Create another method:
    Java Code:
    public void blabla(){
    		new Thread(this).start();
    }
    at the main method you should add:
    Java Code:
    Main lol = new Main();
    lol.blabla();
    That's basically it.
    Inside the Run method you should add the infinite while loop:
    while(true){
    }
    and inside of that loop you add functions which you want to repeat until the program closes.

    Hope I was understandable xD
    :D
    coderX likes this.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  7. #7
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    It didnt work,does the run method go inside this?
    Java Code:
    public class MainGUI extends javax.swing.JFrame implements Runnable {

  8. #8
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Dynamic jTextArea

    Quote Originally Posted by coderX View Post
    It didnt work,does the run method go inside this?
    Java Code:
    public class MainGUI extends javax.swing.JFrame implements Runnable {
    Yes. Anything that implements Runnable MUST have a run() method that overrides the Interface Runnable's abstract public void run().

    In English: Runnable contains a method run that is not implemented. If you say your class is a Runnable class it has to implement the unimplemented method!

  9. #9
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    Thank you guys so much its working fine now,but how do you stop the jtextarea from [bold]scrolling up[/bold] every time it refresh's?

  10. #10
    JBelg is offline Member
    Join Date
    Oct 2012
    Posts
    36
    Rep Power
    0

    Default Re: Dynamic jTextArea

    Getting JScrollPane to stop auto-scrolling

    That post might help you out.

  11. #11
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    That didnt help man,since I'm using a loop to refresh the jTextArea ,it keeps going up continuously . Then I tried putting this into the loop:
    Java Code:
     jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1);
    But now it keeps scrolling down and I'm not able to scroll freely.I want it to refresh in such a way that it doesn't affect the scroll.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: Dynamic jTextArea

    Check out camickr's article on Text Area Scrolling Java Tips Weblog

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    So should I use this?
    Java Code:
     DefaultCaret caret = (DefaultCaret)textArea.getCaret();
    caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
    But where do I put this code,since NetBeans is creating most of the code,idk where.

  14. #14
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    This is what I did,but still it keeps going up,what am I doing wrong here?
    Java Code:
      public void run() {
             
                int i=1;
                int a=100;
                DefaultCaret caret = (DefaultCaret)jTextArea1.getCaret();
                caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
                while (i<=a){
                  
                  //Code for refreshing
    
                    
               
               }

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: Dynamic jTextArea

    To get better help sooner, post your latest attempt in the form of a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem. Note that the code should be as short as possible and not include components and methods irrelevant to the stated problem or inane IDE-generated comments.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    coderX is offline Member
    Join Date
    Feb 2012
    Location
    UK
    Posts
    16
    Rep Power
    0

    Default Re: Dynamic jTextArea

    Nvm guys,I made the program to work in such a way that it only refreshes when the data in the database changes.Thank you all for your time! :)

  17. #17
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Dynamic jTextArea

    Quote Originally Posted by coderX View Post
    Nvm guys,I made the program to work in such a way that it only refreshes when the data in the database changes.Thank you all for your time! :)
    Ok cool, glad to hear you made it work :D
    Good luck with your creations
    WARNING I am Russian so it's possible that I wont understand you correctly...

  18. #18
    joolske is offline Member
    Join Date
    Dec 2012
    Posts
    6
    Rep Power
    0

    Default Re: Dynamic jTextArea

    coderX, could you tell me exactly how you did this? the fact that it only refreshes when the data in the database change, is exactly what I need.

Similar Threads

  1. Print 20 #s of Fib using Dynamic A
    By Army in forum New To Java
    Replies: 5
    Last Post: 03-01-2012, 05:31 AM
  2. 2d dynamic arrays
    By seeder in forum Advanced Java
    Replies: 1
    Last Post: 03-15-2010, 10:03 AM
  3. JTextArea on PopUp -JTextArea isn't editable
    By Richy76 in forum AWT / Swing
    Replies: 3
    Last Post: 02-01-2010, 07:51 PM
  4. Dynamic GUI
    By ike2u in forum New To Java
    Replies: 4
    Last Post: 08-08-2009, 02:50 AM
  5. dynamic inherence
    By itaipee in forum Advanced Java
    Replies: 6
    Last Post: 02-07-2009, 01:05 PM

Posting Permissions

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