• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

JTable problems.

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody help me with my struggles with
JTable. I'm using the model-view-control pattern
and I have implemented the search and the data
presentation, in a JTable, part of the GUI.
I have added a JTextFiled for chair bookins to
each row of the JTable but I don't know how to add
an actionlistner to them. And the number entered
does not "stick" when focus is moved from that
cell?
/Felix
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could change your GUI to have one JTextfield underneath the JTable, so that the user selects a record then enters the number in the TextField below.
Now for adding ActionListeners, I always suggest a Hook method for an outside class to be able to call that will Hook that class with the Object on the GUI that you want to add this ActionListener to.
FOr example there is a public method in the gui called AddBookingTextFieldListener, or some better name than that. Actually check out the code below for a good example of a Hook method and good name.

Now the above is to allow me to add an ActionListener to a JButton on my GUI. personally I didn't have any for the JTextField that I had since I was only concerned with button actions.
then you Controller class can create an ActionListener and pass it to this GUI method.
like

Hope that helps
Mark
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Felix,
If you are really set on having data entry next to each row in the table then you could add a column to the table in the View before creating the TableModel - you would have to create your own TableModel so that you could specify that the extra column was editable.
The Sun tutorials show how to make the table editable, and how to limit the editing to various cells.
It is very easy, and has the advantage that it allows two rows to be booked simultaneously. For the FBNS system this could mirror real life better - most people book a return flight rather than 2 one way fares.
I went down this path originally, and found it very easy, but I decided against it because I could see potential problems with expandability - say if FBNS decided to have adult and child fares, the screen would have to have another column entered (and eventually another, and another ....) or the column would have to be removed which would dramatically change the way the user interacted with the system. This goes against the requirement:

Your user interface should be designed with the expectation of future functionality enhancements, and it should establish a control scheme that will support this with minimal disruption to the users when this occurs.


Regards, Andrew
 
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Andrew. search something about TableModel maybe help. it is clear and easy.
 
Felix Erendi
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mark and Andrew!
After considering your answers, I decided to go for the JTextField outside of the table.
/Felix
reply
    Bookmark Topic Watch Topic
  • New Topic