Results 1 to 6 of 6
  1. #1
    bilgohan is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Add datas to cells of JTable ???

    Hi I got some datas from database but i couldn't insert to cells of Table?? how can I do it?

    Please help me thx.
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
     
    import javax.swing.GroupLayout;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.LayoutStyle;
     
    import javax.swing.WindowConstants;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableModel;
    import javax.swing.SwingUtilities;
    import java.sql.*;
     
    /**
    * This code was edited or generated using CloudGarden's Jigloo
    * SWT/Swing GUI Builder, which is free for non-commercial
    * use. If Jigloo is being used commercially (ie, by a corporation,
    * company or business for any purpose whatever) then you
    * should purchase a license for each developer using Jigloo.
    * Please visit www.cloudgarden.com for details.
    * Use of Jigloo implies acceptance of these licensing terms.
    * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
    * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
    * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
    */
    public class ShowRecords extends javax.swing.JFrame implements ActionListener{
    	private JButton btnShow;
    	private JTable jTable1;
    	private JScrollPane jScrollPane1;
    	private String connect="jdbc:postgresql://localhost:5432/People";
    	private String username="postgres";
    	private String password="bilgohan";
    	private String name="";
    	private String surname="";
    	private String gender="";
    	Statement stmt = null;
        ResultSet rs = null;
     
    	/**
    	* Auto-generated main method to display this JFrame
    	*/
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				ShowRecords inst = new ShowRecords();
    				inst.setLocationRelativeTo(null);
    				inst.setVisible(true);
    			}
    		});
    	}
    	
    	public ShowRecords() {
    		super();
    		initGUI();
    	}
    	
    	private void initGUI() {
    		try {
    			setTitle("Records");
    			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
    			getContentPane().setLayout(thisLayout);
    			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    			{
    				btnShow = new JButton();
    				btnShow.setText("Show Records");
    				btnShow.addActionListener(this);
    			}
    			{
    				jScrollPane1 = new JScrollPane();
    				{
    					
    					TableModel jTable1Model = 
    						new DefaultTableModel(
    								new String[][] { { name, surname,gender } },
    								new String[] { "Name", "Surname","Gender" });
    					jTable1 = new JTable();
    					jScrollPane1.setViewportView(jTable1);
    					jTable1.setModel(jTable1Model);
    					
    				}
    			}
    			thisLayout.setHorizontalGroup(thisLayout.createParallelGroup()
    				.addComponent(jScrollPane1, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 392, GroupLayout.PREFERRED_SIZE)
    				.addGroup(thisLayout.createSequentialGroup()
    				    .addGap(53)
    				    .addComponent(btnShow, GroupLayout.PREFERRED_SIZE, 296, GroupLayout.PREFERRED_SIZE)
    				    .addContainerGap(43, 43)));
    			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
    				.addComponent(jScrollPane1, GroupLayout.PREFERRED_SIZE, 189, GroupLayout.PREFERRED_SIZE)
    				.addGap(39)
    				.addComponent(btnShow, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
    				.addContainerGap(11, 11));
    			pack();
    			setSize(400, 300);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    	
    	
     
    		if(e.getSource()==btnShow)
    		{
    			try{
    			String sql = "Select * From  \"Person\" ";
    			Connection conn=DriverManager.getConnection(connect,username,password);
    			stmt=conn.createStatement();
    			System.out.println(sql);
    			 if (stmt.execute(sql)) {
    	             rs = stmt.getResultSet();
    	         } else {
    	             System.err.println("select failed");
    	         }
    	         while (rs.next()) {
    	            name = rs.getString(1);
    	            surname = rs.getString(2);
    	            gender = rs.getString(3);
    	       **************************************What should ı write here?
    	         }
    			conn.close();
    			}
    			catch (Exception ex) {
    				 System.err.println(ex.getMessage());
    			}
    		}
    		}
     
    }

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

    Default

    You can use the DefaultTableModel.addRow(...) method to insert a new row of data into the model.

  3. #3
    bilgohan is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default it does not work

    it does no work :confused:
    Java Code:
    DefaultTableModel DefTabMod;DefTabMod.addRow(new Object[]{name,surname,gender});
    Last edited by bilgohan; 02-22-2010 at 04:58 AM.

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

    Default

    Sure it works. Forget about the SQL and try creating a demo program that does it manually. Something like:
    Java Code:
    DefaultTableModel model = new DefaultTableModel();
    model.addRow(...);
    model.addRow(...);
    JTable table = new JTable( model );
    JScrollPane scrollPane = new JScrollPane( table );
    frame.add( scrollPane );
    Once you understand how is works with hardcoded data you try it again with your SQL.

  5. #5
    bilgohan is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    i did like this but it returns null? but normally if i write
    Java Code:
    System.out.println(id+" "+name+" "+ surname+" "+gender);
    it will be ok

    but here it returns null:confused:



    Java Code:
      while (rs.next()) {
    	            name = rs.getString(1).trim();
    	            surname = rs.getString(2).trim();
    	            gender = rs.getString(3).trim();
    	            id=rs.getString(4);
    	            DefaultTableModel model = new DefaultTableModel();
    	            model.addRow(new Object[]{id,name,surname,gender});
    	           
    	            JTable table = new JTable( model );
    	            JScrollPane scrollPane = new JScrollPane( table );
    	            
    				frame.add( scrollPane ); 
    	            //DefTabMod.addRow(new Object[]{id,name,surname,gender});
    	            System.out.println(id+" "+name+" "+ surname+" "+gender);
    	         }

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by bilgohan View Post
    Hi I got some datas from database but i couldn't insert to cells of Table?? how can I do it?
    one approach is to create a tablemodel with a jtable linked to a list. so, if you change something to the data, you change it first in the list-elements and then you can synchronize your jtable with a simple fireTableDataChanged(). in order to do that you first need to create your own table-model and for this read How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

Similar Threads

  1. Coloring JTable cells
    By ProgrammingPup in forum Advanced Java
    Replies: 2
    Last Post: 11-04-2009, 10:57 PM
  2. How to merge cells of JTable
    By nehaa in forum AWT / Swing
    Replies: 1
    Last Post: 05-19-2009, 01:07 PM
  3. how to merge cells /colspan in Jtable
    By ravrajesh.ap in forum AWT / Swing
    Replies: 0
    Last Post: 01-03-2009, 04:25 PM
  4. Getting objects from JTable Cells.
    By girigl in forum AWT / Swing
    Replies: 3
    Last Post: 12-24-2008, 10:46 AM
  5. No datas in added to arraylist
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-15-2008, 11:07 AM

Posting Permissions

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