Results 1 to 5 of 5

Thread: JTable Repaint

  1. #1
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default JTable Repaint

    I'd made a save button to save a record and as I checked the database is updated, the new rows is added, however it doesn't appear straight away in the table.
    Here's the code

    Java Code:
    		btnUpdateS.addActionListener(new ActionListener() {
    		      public void actionPerformed(ActionEvent msUpdate) {
    		        try {
    		        	qstm.qDB.executeUpdate("insert into matsub (matsubid, matsubname) values ('" + txMatSID.getText() + "','" + txMatSName.getText() + "');");
    		        	System.out.println("Mat Sub ID : " + txMatSID.getText());
    		        	getContentPane().add(scrollPane);
    				    MatSTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    					MatSTable.setFillsViewportHeight(true);
    					getContentPane().repaint();
    				} catch (SQLException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    getContentPane().repaint(); >>> is this the correct one to do this ?
    I used it to refresh a search text and it's working but not in this one. I have no idea what to do to repaint.

    Thanks in advance
    Last edited by Levian; 07-11-2011 at 03:25 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You don't have to call repaint or anything similar. All you have to do is to update the data in your table model and then have the model fire the proper update method (if you are extending an AbstractTableModel). You don't even have to do that if you extend a DefaultTableModel. For that all that's needed is to update the model and it will take care of the rest. Incidentally, I don't see where you in fact are updating your model (unless I'm missing it).

    Edit: in fact your code confuses me a bit. Why are you adding something called scrollpane to the contentPane from within this listener? Again, where do you update your table's model? Could you explain a bit what you're trying to do with that code?
    Last edited by Fubarable; 07-11-2011 at 04:12 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Also, use a PreparedStatement. Your code is vulnerable to SQL injection.
    xkcd: Exploits of a Mom

    db

  4. #4
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Thanks, I'll read more on the PreparedStatement, does it mean that PreparedStatement is more secure than Statement ?
    Anyway the repaint works after I added a query statement, but seems got to change it using PreparedStatement instead.

  5. #5
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    I'd read the PreparedStatement in Java Tutorial, but it's kinda confusing...
    I wonder if I get this right...
    PreparedStatement is derived from Statement, all are similar except that the data types of the fields are identified in PreparedStatement ?

    Thanks in advance

Similar Threads

  1. repaint() problems
    By Isong in forum AWT / Swing
    Replies: 1
    Last Post: 10-28-2010, 11:22 PM
  2. repaint method
    By manojp476 in forum CLDC and MIDP
    Replies: 10
    Last Post: 08-12-2010, 03:12 PM
  3. repaint every
    By 3xpr1ment in forum AWT / Swing
    Replies: 10
    Last Post: 03-23-2010, 05:39 PM
  4. Problem with repaint();
    By dunafrothint in forum AWT / Swing
    Replies: 8
    Last Post: 01-07-2010, 12:33 AM
  5. Repaint() not working
    By Catkill in forum AWT / Swing
    Replies: 3
    Last Post: 09-09-2009, 10:51 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
  •