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
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
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.