I came up with a recently problem that, any component that I add to a JPanel (JTextField, JTextArea, JTable) can't edit even when I force it, in code, to be enabled and editable. I'm using NetBeans for developing the project.
Anyone here faced this problem? Looks like I'll be forced to change all to JFrame. Even though, hope that someone reply this topic with some constructive idea/help.
Try to call setFocusable(true) for the top level container.
I have two reasons/opinions/thoughts as to why you are getting "errors".
You aren't adding the components to the panel correctly.
panel.add(someComponent);
panel.add(anotherComponent);
panel.add(yetAnotherComponent);
frame.add(panel);
You are overriding the default behavior for the components. All components in a JFrame, JWindow, etc. are by default enabled and editable (meaning there is an implied setEnabled(true) and setEditable(true), respectively).
Related
I'm having a problem with Java KeyListener when adding adding another JPanel with 5 JLabels, I've searched around this website, and most solutions to this problem involve switching from KeyListener to KeyBindings. This wont work for me because I need to know exactly when a key is pressed, released and held down. To my knowledge, KeyBindings does not provide all those.
I've tried to use
this.requestFocus();
after creating the new JPanel, but it didn't work, however when I use the same line inside the paintComponent(), it works. Which brings me to my questions: How does this reflect on performance? My paintComponent() is called about 60 times/sec. Is there a way to call it once and still have this working? I see that requestDefaultFocus() from the type JComponent is deprecated...
I've also tried adding same KeyListener to the second JPanel, but that didn't help. Im guessing one of the JLabels is the one that gets focus?
This wont work for me because I need to know exactly when a key is pressed, released and held down. To my knowledge, KeyBindings does not provide all those.
Yes it does. You have an Action for "pressed" and "released". There is no such Action as "held down" (even for a KeyListener), you just get multiple events generated.
this.requestFocus();
That is not the proper method to use for requestion focus on a component. Read the API for that method and it will tell you the proper method to use.
however when I use the same line inside the paintComponent(), it works.
This is because you can't request focus on a component until the frame has been realized, which means you've invoked pack() or setVisible() on the frame.
Is there a way to call it once and still have this working?
See the RequestFocusListener class in Dialog Focus.
The proper solution is to use Key Bindings so you don't need to use these work arounds.
The code pretty huge and involves a lot of different class/methods:
But here is the gist:
There is a main frame : A_Main
Selecting something in the main frame A: opens a JDialog B_Dialog
This B_Dialog has a JPanel on it: C_Panel
This C_Panel comprises of a textfield and a button
On clicking on the textfield/button: opens a tooltip and another JPanel: D_Panel
Now, the problem is:
1) The tooltip overflows the size of B_Dialog and therefore gets truncated
2) D_Panel however; even if its outside the boundary of B_Dialog gets displayed fully
2.1) There are some texfields and drop down menus in this D_Panel
2.2) The mouse events function correctly in this D_Panel items (drop down menus)
2.3) But Keyboard events do not function correct (Textfield)
I would be glad if you could help!
Thanks!
This can only be done in newer versions of the JDK.
See, Mixing Heavyweight and Lightweight Components.
Now, when I try to enter something in the JAR JPanel's text field, I am not able to do so as this pops out of the border of the main JDialog that contains it.
Add a JScrollPane around the JPanel, and allow it to expand both horizontally and vertically. If this doesn't work, you may need a customized Layout Manager, or use one of the default ones like GridBagLayout.
Also, you'll need to gain focus before you can enter text, but that doesn't seem to be the problem here.
I've got a Problem: In my Java application I've got an AWT Component (cannot change that one) that streams and shows an avi-file. Now I want to draw upon this movie and thought about putting a transparent JPanel above it and draw on that one.
This does not work since I either see the avi-stream or the drawn lines but not both.
I read somewhere that AWT does not support transparency of components - but the panel is a JPanel which is able to do so.
Can someone please help me with this one - thanks in advance.
The Mixing Light and Heavyweight Components article explains how this is handled only in the most recent version of the JDK.
did you try a GlassPane since i think thats exactly it's use case. soemthing like JFrame.setGlassPane() if i remember correctly. check here:
Java API JFrame
GlassPane examples
I've searched for a heavyweightcomponent that could help me but I didn't found one, and regarding the internalframes I don't know how you managed to set the opacity to 50. All what I found about seetting the opacity wast the method setOpaque(boolean value) and this doesn't help me at all. Would you please explaine me how did you set that opacity. It's my final alternative either that or I'll have to change my entire project design.
Try running Stream and Drawing on separate thread. I think this can help
The program I am writing has a Swing GUI and several of the components have Tooltips. These are all on JButton, JCheckBox and JRadioButton components, they are set using the setToolTipText method and all work perfectly. I have just tried to add one to a custom component that extends JPanel using the same method and no tool tip appears.
The JPanel contains 2 JLabel components and a JSlider. I tried to override the setToolTipText method and then use it to call setToolTipText on the slider. This didn't work either.
public void setToolTipText(String text) {
super.setToolTipText(text);
slider.setToolTipText(text);
}
Am I doing something wrong? or can't you have a tooltip on a JSlider or JPanel. I would have thought that it should work on anything that extends JComponent.
The control is disabled when the program starts although even after it is enabled there is still no tool tip if that is relevant. It doesn't really need to show it until the control is enabled anyway.
Thanks.
try settings the tooltip to the JLabels as well. they may be obstructing the JPanel.
Try making the following call:
ToolTipManager.sharedInstance().registerComponent(customComponent);
I am currently trying to build an expanding panel in Swing (akin the WPF's Expander control) and I'd like to retain the usual methods for manipulating it (i. e. setLayout, add, etc.). Only they should be routed to an embedded panel (the one being shown or hidden).
How would one do that? Overriding every method of JComponent and re-routing that to an embedded JPanel would be cumbersome, but that's the only way I see.
Or should I rather make the embedded panel visible to the outside and force users to use something like ExpanderPanel.getInnerPanel() instead. But then it's no drop-in replacement for JPanel which I think would be nice to have.
Take a look at the JXTaskPane from Swingx project. It already does what you need.
In 1.5(ish) Swing routed a few methods to the content pane in JFrame, JApplet, etc. Whilst there appeared to be some usability benefits for those just starting, it doesn't actually fix the problem. So everyone has to deal with a very strangely behaving API. So my advice is to avoid this approach.
If you have a Container widget which holds a panel you want to show and hide, why not layout your inner panel however you want, then add it to the Container panel, then use static methods against the Container to say
JPanel p = new JPanel();
//do something with the JPanel...
ContainerWidget.setContent(p);
ContainerWidget.expandPanel(p,true);
Would somethign like this work?