-5

I found the following code:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        DefaultTableModel tableModel = new DefaultTableModel();
        JTable table = new JTable();
        table.setModel(tableModel);
        int c=0;
        Vector rl=new Vector<Object(6);        
        rl.add(c+1);
        rl.add(jTextField1.getText());
        rl.add(jTextField11.getText());
        rl.add(jTextField7.getText());
        rl.add(jTextField8.getText());
        rl.add(jTextField9.getText());
        tableModel.addRow(rl);}

But it's not working.

I have to copy the fields corresponding to pcode, product name, price, Quantity, total price which are the textfields, when I click add button to the table given in it

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 4
    Don't use "found" code directly out of the box. Use the ideas in found code, and then write your own code. If you're having problems with code, you'll want to tell us what problems you may be having. "not working" tells us little that we can use to be able to help you. – Hovercraft Full Of Eels Mar 29 '13 at 21:24
  • well actually the problem is after coding it in the button click .......and after running the file when i click the button ntng happens.......... it just remains as it was.... i dont knw how to insert the values in jtable from a jtextfield please help me out.... – Akhilesh M Nair Mar 29 '13 at 21:28

1 Answers1

6

Don't use "found" code directly out of the box. Use the ideas in found code, and then write your own code. If you're having problems with code, you'll want to tell us what problems you may be having. "not working" tells us little that we can use to be able to help you.

You shouldn't be creating a new JTable inside of your ActionListener if all you want to do is add a row to an existing JTable. Instead,

  • Get the JTable's model.
  • Get the data from your JTextFields and use the data to create an array of Object or a Vector filled with the data if the JTable uses a DefaultTableModel.
  • Or if the JTable uses an AbstractTableModel, then create an appropriate row object with the data held by the JTextFields. The type of object needed will depend on what type of objects are held by the TableModel.
  • If it's a DefaultTableModel, then you can call its addRow(...) method to add a Vector or Object[] array to the model. Otherwise you'll need to create your own addRow(...) method for your AbstractTableModel class.
  • If you do create your own addRow(...) method, be sure to call the appriate fireTableXXX(...)` method after making your changes to the model's data. Check the AbstractTableModel API for more details on these methods, but know that they're important for notifying all listeners to the model of the changes, including the JTable itself.
  • First and foremost, be sure to read the Swing JTable tutorial as most of this is discussed there. The only down side to the tutorial in my own opinion is that it discusses AbstractTableModel and doesn't give hardly any information on DefaultTableModels, but we can help you some with that.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • can just explain me a lil more breifly ...?? – Akhilesh M Nair Mar 29 '13 at 21:30
  • 2
    @AkhileshMNair: explain what exactly? Have you read the tutorial? I have placed the link in my answer above, and again, this is the first thing that you should do. – Hovercraft Full Of Eels Mar 29 '13 at 21:30
  • ntng much sir.... thank u for ur advice to read Swing JTable tutorial.... lemme check dat out if i dint get help from it.... i may again disturb u..... thanks alot... – Akhilesh M Nair Mar 29 '13 at 21:35
  • 2
    @AkhileshMNair: please feel free to come back and ask as you're not disturbing me or the rest of us at all. I do request that you avoid using non-standard abbreviations. They're not necessary, unless you are sending this from a mobile device, they're very distracting, and they can hamper your ability to communicate clearly, not something that you should be doing when communicating in a technical forum. – Hovercraft Full Of Eels Mar 29 '13 at 21:37
  • I am sorry .I wont use anymore non-standard abbreviations. Sir can you just help me a little more in how to enter a data that has been retrieved from database and shown in a jtextfield as given in above code from those fields i need to get those datas to be entered in the table . – Akhilesh M Nair Mar 29 '13 at 21:52
  • 1
    @AkhileshMNair: At this point, I think that you will want to try to do this yourself, and if the code doesn't work, show us your most recent attempt as an addition to your original post, explaining how the new code doesn't work for you. Remember that one trick for trying something new like this is to create a very small simple program that tries to solve what you are attempting but on a much smaller scale which allows you to tackle one problem at a time. Thanks for your not using the non-standard abbreviations by the way. It is much appreciated! – Hovercraft Full Of Eels Mar 29 '13 at 21:54
  • and also I would like to know how scrolpane can help me in any way to enter the data to table? – Akhilesh M Nair Mar 29 '13 at 21:54
  • Sir as u said "You shouldn't be creating a new JTable inside of your ActionListener if all you want to do is add a row to an existing JTable." I tried the same and made the changes in code like this ` DefaultTableModel tableModel = new DefaultTableModel(); int c=0; Vector rl = new Vector(6);// no of columns... rl.add(c+1); rl.add(jTextField1.getText()); rl.add(jTextField11.getText()); rl.add(jTextField7.getText()); rl.add(jTextField8.getText()); rl.add(jTextField9.getText()); tableModel.addRow(rl);` – Akhilesh M Nair Mar 29 '13 at 21:59
  • @AkhileshMNair: I'm confused as a JScrollPane and data entry are two completely orthogonal concepts as far as I understand them. One doesn't really effect the other all that much. Also, please don't try to post code in comments as it is unreadable. As I requested above, edit your original question and add new code at the end. – Hovercraft Full Of Eels Mar 29 '13 at 21:59
  • but am worried that its output is still the same its just showing no warning or error message . Just left the table intact as if nothing has happend . I will try not to enter anymore code in comments . Am sorry for the inconvenience as am completely new to this I was really not knowing and more over i cannot even chat or post the screenshot of the table as my reputation is just 1. – Akhilesh M Nair Mar 29 '13 at 22:06