Results 1 to 18 of 18
Thread: Dynamic jTextArea
- 10-26-2012, 04:24 PM #1
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
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:But when I use a loop,the program freezes and the UI becomes unresponsive.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"); } } }
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?
- 10-26-2012, 04:30 PM #2
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
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...
- 10-26-2012, 06:25 PM #3
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
- 10-26-2012, 07:14 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Dynamic jTextArea
Yup, that's how it works.
- 10-26-2012, 08:22 PM #5
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
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 }
- 10-26-2012, 11:57 PM #6
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
Re: Dynamic jTextArea
Since you don't know much about threads you must start the threadJava Code:public class Main extends javax.swing.JFrame implements Runnable
Create another method:
at the main method you should add:Java Code:public void blabla(){ new Thread(this).start(); }
That's basically it.Java Code:Main lol = new Main(); lol.blabla();
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
:DWARNING I am Russian so it's possible that I wont understand you correctly...
- 10-27-2012, 04:49 AM #7
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
Re: Dynamic jTextArea
It didnt work,does the run method go inside this?
Java Code:public class MainGUI extends javax.swing.JFrame implements Runnable {
- 10-27-2012, 05:13 AM #8
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Dynamic jTextArea
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!
- 10-27-2012, 02:10 PM #9
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
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-27-2012, 02:15 PM #10
Member
- Join Date
- Oct 2012
- Posts
- 32
- Rep Power
- 0
Re: Dynamic jTextArea
Getting JScrollPane to stop auto-scrolling
That post might help you out.
- 10-27-2012, 07:08 PM #11
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
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:
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.Java Code:jTextArea1.setCaretPosition(jTextArea1.getText().length() - 1);
- 10-27-2012, 09:26 PM #12
Re: Dynamic jTextArea
Check out camickr's article on Text Area Scrolling « Java Tips Weblog
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-28-2012, 05:06 AM #13
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
Re: Dynamic jTextArea
So should I use this?
But where do I put this code,since NetBeans is creating most of the code,idk where.Java Code:DefaultCaret caret = (DefaultCaret)textArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
- 10-28-2012, 05:21 AM #14
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
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 }
- 10-28-2012, 07:37 AM #15
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-28-2012, 03:05 PM #16
Member
- Join Date
- Feb 2012
- Location
- UK
- Posts
- 16
- Rep Power
- 0
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! :)
- 10-28-2012, 08:05 PM #17
Senior Member
- Join Date
- May 2012
- Posts
- 170
- Rep Power
- 1
- 12-12-2012, 04:51 PM #18
Member
- Join Date
- Dec 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Print 20 #s of Fib using Dynamic A
By Army in forum New To JavaReplies: 5Last Post: 03-01-2012, 05:31 AM -
2d dynamic arrays
By seeder in forum Advanced JavaReplies: 1Last Post: 03-15-2010, 10:03 AM -
JTextArea on PopUp -JTextArea isn't editable
By Richy76 in forum AWT / SwingReplies: 3Last Post: 02-01-2010, 07:51 PM -
Dynamic GUI
By ike2u in forum New To JavaReplies: 4Last Post: 08-08-2009, 02:50 AM -
dynamic inherence
By itaipee in forum Advanced JavaReplies: 6Last Post: 02-07-2009, 01:05 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks