• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

value changed method is called twice

 
ravindra janapreddy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have implemented the following code for booking the seat on a selected flight :
public class FBNTable extends JFrame implements ListSelectionListener {
pubic FBNTable() {
m_table = new JTable();
m_table.setCellSelectionEnabled(true);
ListSelectionModel cellSelectionModel = m_table.getSelectionModel();
cellSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
cellSelectionModel.addListSelectionListener(this);
} // end of constructor
public void valueChanged(ListSelectionEvent evt) {
int m_rows, m_columns;
String m_selectedData = null;
int[] m_selectedRow = m_table.getSelectedRows();
int[] m_selectedColumns = m_table.getSelectedColumns();
for ( int i = 0; i < m_selectedRow.length; i++) {
for ( int j = 0; j < m_selectedColumns.length; j++) {
m_selectedData = (String) m_table.getValueAt(m_selectedRow[i], m_selectedColumns[j]);
}
}
System.out.println("Selected: " + m_selectedData);
// code for seat booking
} // end of method
} // end of class
The problem is when I click on the selected flight, the code for seat booking is executed twice as a result the available seats is reduced by two. Is there a way of preventing this ? Programmatically, I could do this by using flag but I feel that there must be still a better way.
Thanks in advance
Ravi
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure why it happens twice, but I looked at your code and I strongly recommend that you follow a standard MV (Model-View) pattern for your table code. All you have right now is V. You are missing the M that controls the table data.
Sun has an excellent tutorial on the use of tables.
Eugene.
[ August 01, 2002: Message edited by: Eugene Kononov ]
 
Richard Mok
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ravindra
I had similar problem before. My case is, when I click "Book" button, the quantity reduced twice. Later, I found out the reason was, after "Book" button is clicked, I remove the mouse listener for the "Book" button so that others cannot click the button during the database update. After the seat is booked, I add the mouse listener again to that button. That's why the mouseClick event gets called twice for my case. I am not sure how is your case, hope it can help.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ditto what Eugene said. You really should not be allowing single cell selection. Single row is what you want and create an area on your GUI dedicated to booking. Have that component follow row changes, and do your booking from there. Dumping client status to stdout once the GUI is up is not a real good way to do it either. You can show the selected flight in the booking component instead.
Hope this helps,
Michael Morris
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic