• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I get the value of JtextField or JTable and insert in a Vector?  RSS feed

 
Riccardo Rossi maletti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have to retrieve some Jtextfield or Jtable and put the value into a Vector, but I can't understand how can I do.

Ebook.class



This is the model:



my GUI contains JTextfield for each vector fields

Example:



...
and Button Add





When I'm edit the Listener I can't create a Vector




 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use Vector unless you have some code which specifically requires Vector. I think the constructor for JComboBox requires Vector. Otherwise everything else takes List, so use ArrayList instead.
I can never remember how tables work, but I can tell you where you can find out: Java™ Tutorials. In the case of a JTextField you can try something like this:-Obviously that is very simple code and will throw lots of number format exceptions if your text doesn't match an Integer.
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think in your case you will want a Book class with such fields as titolo autore anno and pagini.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, welcome again!

When you add an element to the Vector, the table model doesn't know that has happened so it doesn't attempt to update the table. You could fireRowsInserted(...) to solve that.

Apart from which, you are using DefaultTableModel and ignoring its features, which include storing its data. If you're going to reimplement the data storage then the class to extend is AbstractTableModel.

Either way, you should avoid adding new rows directly to the data structure (Vector<Ebook> in your case -- if you have already covered Generics) and use or provide an addRow(...) method that adds the row and fires an event that will let the table know it's time to update itself.

Finally, since this is related to Swing, I shall move it to a more appropriate section.

@Campbell: You're correct about JComboBox, but there are 3 other constructors too.

 
Riccardo Rossi maletti
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this is the right way:

Ebook.class


Biblioteca.class





I create a vector Biblioteca


and an object row that retrieve the field of each JTextfield like this:



Why I can't use ?

I have to create a Vector of Ebook and at the same time populate the JTable with the model
 
Campbell Ritchie
Marshal
Posts: 56576
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I missed this post altogether. Sorry for the delay.
You can create a table model and you can use that to populate a JTable. I presume you have read the Java™ Tutorials section. It would appear you can get an Object[] from a row in a table model. Create a constructor in the book class which takes an Object[]There may be a better way to do this.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!