aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes question about the jcombox focus 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 "question about the jcombox focus" Watch "question about the jcombox focus" New topic
Author

question about the jcombox focus

Jason Chen
Greenhorn

Joined: May 09, 2002
Posts: 9
Hello! Are there anyone can help me out?
In my client Frame,I use two panel,One for search
,One for book.I hope users can use my software only with keyboard,so I set the focus squence with setNextFocusableComponent() and users can select different components using TAB. I also add a keylistener on JTable to make JTable lose focus when users press TAB or Shift+TAB.
This runs well at first,but after I booked a seat,
strange thing happens --- if the seatcounts JComBox(I use combox to get seats to book) get the focus,it doesn't do anything when I press TAB or Shift+TAB.
Can anyone help me about this?
Thanks a lot !
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
I guess your combobox is inside the JTable as a CellEditor. If not, I don't know what's happening.
When a component is managing focus it loses the focus either by pressing Ctrl+Tab or by pressing Ctrl+Shift+Tab. That's the case of JTable. That's the usual behaviour, and I think it should not be changed.
I suppose the JTable gets a bit messy when the user presses a Tab: on the one hand it tries to finish the cell edition; on the other hand your listener makes the table lose the focus. But it is just a supposition.
You can remove the listener or make it force the edition to be finished before transfering the focus.
Jason Chen
Greenhorn

Joined: May 09, 2002
Posts: 9
Thank you very much for your reply.
It gives me sth. useful.
In my client Frame,I use two panel,One for search
,One for book.The Jtable is in the search panel and the Jcombox in the book panel which is used for accepting seats' count to be booked. I also
have a book button in book panel.
I want to set focus transfering route is
jtable -> jcombox -> jbutton
After I booked a seat sccucessful,I cannot transfer focus from jcombox to jbutton.
Is there anyone has the same trouble?
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
Give me a clue:
when the user presses the book button, what do you do?
I mean, do you show a JDialog during the booking? and if you do, do you do something with the GUI after closing the JDialog and returning control to the user?
I have had problems with focus ownership with such situation.
Jason Chen
Greenhorn

Joined: May 09, 2002
Posts: 9
I think you get the key to this question now!

1. Use JOptionPane.showConfirmDialog to ask whether the user really want to book
2. if yes,
2.1 lock,modify,unlock
2.2 reset TableModel of JTable. Code is listed below:
//refresh data in flight table
int selected = flightTable.getSelectedRow();
String origin = (String)originCmBox.getSelectedItem();
String dest = (String)destCmBox.getSelectedItem();
try {
DataInfo[] records = flight.searchFlight(ORIGIN_AIRPORT_FIELD_NAME + "='" + origin + "'," +
DEST_AIRPORT_FIELD_NAME + "='" + dest + "'");
flightTable.setModel(new FlightTableModel(records, flight.GetFlightDescription()));
} catch (Exception ex) {
}
flightTable.setRowSelectionInterval(selected, selected);

private class FlightSelectListener implements ListSelectionListener {
public void valueChanged(ListSelectionEvent e) {
//Ignore extra messages.
if (e.getValueIsAdjusting()) {
return;
}
ListSelectionModel lsm = (ListSelectionModel)e.getSource();
if (lsm.isSelectionEmpty()) {
......
} else {
int selectedRow = lsm.getMinSelectionIndex();
//show information of this flight in JTextFields
for (int i = 0; i < flightTable.getColumnCount(); i++) {
String columnName = flightTable.getColumnName(i);
String value = flightTable.getValueAt(selectedRow, i).toString();
//those are fields to display a flight info
((JTextField)fieldNameToTextField.get(columnName)).setText(value);
}
bookButton.setEnabled(true);
}
}
}
Eduard Jodas
Ranch Hand

Joined: May 14, 2002
Posts: 80
Difficult solution.
In general, the thread that shows a modal JDialog should not modify directly the GUI after that JDialog is closed.
Two workarounds worked for me:
1. The best but more difficult: put the code that modifies the GUI after the dialog is closed inside an invokeLater call, for instance:
JOptionPane.show();
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// your code here
}
});
2. Unbelieveable: AFTER the JDialog is closed and the GUI modifications are made call:
SearchFrame.setContentPane(SearchFrame.getContentPane());
where SearchFrame is the JFrame that holds your jcombox. It worked for me in JDK1.3, have faith and don't ask why.
But maybe we are facing different problems.
Hope it helps.
Jason Chen
Greenhorn

Joined: May 09, 2002
Posts: 9
Thank you very much!
I use the second,It just need to add one sentence to my code.
Thanks again!

[ June 10, 2002: Message edited by: jason chan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about the jcombox focus