Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to insert the value to table in form

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default how to insert the value to table in form

    hi, how to insert the value to table without using database . i hava a form, form having 2 combox field,2 textfields. if i am entering the value to that field means the value has been inserted to table. how to do that..pls help me anyone.....


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: how to insert the value to table in form

    Please define what you mean by 'table'. Are you referring to a JTable? You mention database, are you using JDBC?

  3. #3
    Junior Member
    Join Date
    Aug 2010
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: how to insert the value to table in form

    i am using only JTable ...pls help me......

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,104
    Thanks
    169
    Thanked 780 Times in 726 Posts
    Blog Entries
    5

    Default Re: how to insert the value to table in form

    What do you have so far? Please post a short portion of code which makes it much easier to know how far you've gotten and lead you in the right direction. If you are completely lost, I suggest reading How to Use Tables (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,554
    Thanks
    24
    Thanked 304 Times in 286 Posts

    Default Re: how to insert the value to table in form

    I'll give a brief overview on what a JTable needs based on my preferred way of doing it because when I first started using JTables that Tutorial confused the hell out of me.

    When creating a JTable, you can send it a TableModel. Since TableModel is an interface, it is best to send it a DefaultTableModel. A DefaultTableModel requires either 2 arrays or 2 Vectors. Since the number of rows in your JTable will be dynamic, we will talk about the Vectors. The first Vector needs to be a 2 dimensional Vector (Vector of Vectors of Objects) because this is going to be our Table's Data and that is 2 dimensional (rows and columns). The second Vector needs to be a 1 dimensional Vector (Vector of Objects) because this is going to be our Column Titles. So, lets first set up our data storage (someone please check my syntax, havent used Vectors as primary storage before):
    Vector<Vector> data = new Vector<Vector>();
    Vector columnNames = new Vector();
    Now we probably want to set our Column Names now, since they probably wont change throughout the program. So, add elements to the columnNames Vector. I am going to assume that our JTable includes 4 columns:
    columnNames.add("JComboBox 1");
    columnNames.add("JComboBox 2");
    columnNames.add("JTextField 1");
    columnNames.add("JTextField 2");
    Now that we have our columnNames, (tell me if I'm wrong here) since we are using Vectors we dont really need to add any elements yet, since the Vectors will have a size of 0. So, we can go ahead and create our DefaultTableModel. When we create our DefaultTableModel, we send it the data Vector first, then the columnNames Vector (DefaultTableModel API):
    DefaultTableModel tableModel = new DefaultTableModel(data,columnNames);
    Now that we have our DefaultTableModel, we want to create our JTable. So, we just create a new JTable object and send it our DefaultTableModel (JTable API):
    JTable table = new JTable(tableModel);
    Oh, but there is a catch, JTable doesnt like to display its Column Heads on its own for some reason (JTables like to be difficult as you will find). So, the easiest and most convenient fix for this is to put it inside a JScrollPane:
    JScrollPane tableScroller = new JScrollPane(table);
    Now, the JTable and the JScrollPane dont really like sizing themselves most conveniently for you. So you have to tell the JTable and the JScrollPane what sizes you want. I've had mixed results doing this. It is best to set the PreferredSize first, but if they fail to make any change, try setting the MaximumSize as well. If they both didnt work, check to make sure you do it correctly. But, JComponent (where JTable and JScrollPane inherit setPreferredSize()) likes to be difficult as well. So we cant send it standard width and height values, we need to send it a Dimension Object. Dimension Objects take in a width first, then a height. We will set it as a width of 200 and a height of 300:
    //It is unnecessary to do one of these calls I think, not sure though
    table.setPreferredSize(new Dimension(200,300));
    tableScroller.setPreferredSize(new Dimension(200,300));
    The last little trick is that you need to send the JPanel (or whatever Container you are using) the JScrollPane instead of the JTable. This is because the JScrollPane contains the JTable and all that garbage. So, we add the JScrollPane to the JPanel (this is different based on how your GUI is set up):
    JPanel.add(tableScroller);

    Now, the nice thing about the DefaultTableModel is that it allows you to easily add rows of data to the JTable. To do this, we use the DefaultTableModel's addRow(Vector) method (DefaultTableModel API). So, we first need to create a Vector of data, then send it that Vector:
    Vector tempData = new Vector();
    tempData.add(JComboBox1.getSelectedItem());
    tempData.add(JComboBox2.getSelectedItem());
    tempData.add(JTextBox1.getText());
    tempData.add(JTextBox2.getText());
    tableModel.addRow(tempData);

    And I think that covers everything you want to know. I assume this code works, but I didnt try it. I dont think I made any mistakes, but I'll help you out with anything incorrect I might have done. Tell me if something is unclear. I'm trying to teach you how to create a JTable and add rows to it, not give you all your code.

Similar Threads

  1. How to insert a proper child in the XML Doc
    By Sarakh in forum What's Wrong With My Code?
    Replies: 0
    Last Post: August 31st, 2010, 12:36 PM
  2. form
    By IBANGS in forum AWT / Java Swing
    Replies: 3
    Last Post: June 15th, 2010, 03:25 AM
  3. Unable to insert records
    By javaprogrammer11 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: March 24th, 2010, 03:16 AM
  4. Java Swing :Back Button from one form to another form
    By srinivasan_253642 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 26th, 2009, 08:51 AM
  5. Insert sort algorithm
    By Alysosh in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 26th, 2009, 09:28 AM