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.....
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.
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.....
Please define what you mean by 'table'. Are you referring to a JTable? You mention database, are you using JDBC?
i am using only JTable ...pls help me......
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)
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):
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:
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):columnNames.add("JComboBox 1"); columnNames.add("JComboBox 2"); columnNames.add("JTextField 1"); columnNames.add("JTextField 2");
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):DefaultTableModel tableModel = new DefaultTableModel(data,columnNames);
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:
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:JScrollPane tableScroller = new JScrollPane(table);
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:
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.