File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes JTable problems. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "JTable problems." Watch "JTable problems." New topic

JTable problems.

Felix Erendi

Joined: Apr 16, 2003
Posts: 11
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
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

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.

Hope that helps

Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11776

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

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Bigwood Liu
Ranch Hand

Joined: Feb 26, 2003
Posts: 240
I agree with Andrew. search something about TableModel maybe help. it is clear and easy.
Felix Erendi

Joined: Apr 16, 2003
Posts: 11
Thank you Mark and Andrew!
After considering your answers, I decided to go for the JTextField outside of the table.
I agree. Here's the link:
subject: JTable problems.
It's not a secret anymore!