Moderator (MDR): John Zukowski of the MageLang Institute
MDR-jaz: Welcome to the Swing short course office hours. My name is John Zukowski, with MageLang Institute (http://www.magelang.com) and I'll be here for the next hour to answer questions about the first part of the Swing Short Course, available here at the JDC.
analogue: Why doesn't JTextField
use setKeyAccelerator
? I want to use it in conjunction with JLabel.setDisplayedKeyAccelerator()
, but don't want to resort to JComponent.registerKeyboardAction()
.
MDR-jaz: What do you mean you want to "use it in conjunction with" JLabel
?
analogue: From a GUI perspective, if I have a label and a textfield, I want the accelerator for the label to give focus to the textfield. As I understand it, JLabel.setDisplayedKeyAccelerator()
is only there for show.
MDR-jaz: I believe that is correct. From the source comments, it appears like what they thought people would use the accelerator for the reason you mention exactly. However, they do not have the appropriate hooks set up to do the actual connections. May I suggest you submit a message to the swing-feedback email to recommend they add an appropriate method to support this. If you would like, I can also submit the suggestion.
analogue: I'll make sure I send them some feedback on this.
MDR-jaz: It sounds like something that really should be there.
analogue: It is definitely needed for mouseless operation. On a related note, how do I control focus order? Assume I have X number of widgets in a panel and want to explicitly set the focus order (user uses tab key). I can't make any sense out of FocusManager
or how to use it.
MDR-jaz: The easiest way to control focus order is the order the components that are added to the container.
analogue: Assume I add/remove components dynamically and don't want to rely on the order in which they are added.
MDR-jaz: I'm not an expert on FocusManager
yet.... The way it looks is you create a FocusManager
subclass/instance, then FocusManager.setCurrentManager()
. In the subclass, you would maintain some kind of ordering collection, such that given "focusedComponent," you can determine the previous/next components in focusNext/PreviousComponent
.
analogue: OK, so the static method setCurrentManager()
will relinquish control to my FocusManager
. Sounds possible. I will have to play with it some more.
MDR-jaz: In the case of a JComponent
, you would move focus with grabFocus
. In the case of AWT components, you would use requestFocus
. The hardest part is how to handle depth when the "next" component is a container.
dpliu: I have a couple of questions. First, why do Swing objects all extended from AWT.component rather than from individual control such as TextArea
? Can anybody tell me how to add scroll bar to JTextArea
using JScrollPane
?
MDR-jaz: Hello, dpliu. The reason that Swing components do not subclass AWT components is if they did, there would be no way to make them "lightweight" or peerless.
dpliu: Ok, I see your point.
MDR-jaz: Regarding scrollpane, just add the JTextArea
to the JScrollPane ( JScrollPane pane = new JScrollPane();, pane.getViewport().add(textarea);
dpliu: Jaz, I've used new JScrollPane(JTextArea)
form of constructor, it did not work. Later, I'll try your suggestion.
MDR-jaz: What version of Swing are you using with JTextArea
form of constructor?
dpliu: Swing 0.6.1
MDR-jaz: Short course is only at .5.1.... ;-) Just got back from being away for a week and haven't had a chance to use .6.1 much yet. If the JScrollPane (JTextArea)
constructor doesn't work, send a note to the Swing team about it. It's possible that the capabilities might not be implemented yet, but they wanted to get the API fixed more. Good luck.
dpliu: OK, I let you know if it makes difference with .5.1. Thanks for your help.
MDR-jaz: You're welcome. It looks like this week's Office Hours have come to a close. Thanks for coming by and don't forget about next week's Code Clinic.