What could probably go wrong? Using getContentPane() as default layer JPanel - java

I found out I can use (JPanel) frame.getContentPane() easily as default layer JPanel, even with borders. (Which was surprising to me at first.)
Is this a common/uncommon/good/bad behavior? Why?
While doing this: What coul probably co wrong?

Read the section from the Swing tutorial on Adding Components to the Content Pane for a brief discussion on this topic.
There is no real drawback. Your posted code already addresses the issue, mentioned in the link.

Related

How to auto resize java swing elements?

I'm trying to auto resize the left side of my application. I have a JTextField and a JTree on the left and 3 JButtons on the right. But I just don't know how to make the left side auto resizeable.
I did it with the Netbeans GUI Creator (or whatever it is called) but I don't know how to to it without Netbeans. (I usually don't program with Netbeans, this was just an exception to see if it's even possible to do so with Swing.
Here is the code Netbeans created: http://pastebin.com/ERwY4rBC
It's not that the code is completely unusable but I wanted to try it manually.
The GroupLayout looks nice, but the Oracle site says it's mainly for the use for GUI tools. So, using GroupLayout would be not "Java like" or how do I have to understand it? Or is there even a better way to achieve this without GroupLayout?
Thanks!
So, using GroupLayout would be not "Java like" or how do I have to understand it
GroupLayout is to put it simply really hard to hand-code, and results mostly in a lot of code. But it is not "not Java like", it is just not something you want to do by hand, and the code afterwards is hard to read as it is rather verbose.
What you try to achieve (according to the screenshot) is easily achievable using some 'nested layouts'. If your main panel uses a BorderLayout where you put the left, resizable panel in the BorderLayout.CENTER and the other, non-resizable panel in the BorderLayout.EAST you will obtain the desired resize behavior.
Then you just have to decide which LayoutManager to use for those individual panels. I think that both the BoxLayout as well as the FlowLayout will do just fine.
Do yourself a favour and use MigLayout for all your layout needs. It is especially convenient for coding UI by hand.
There is a WebStart application on their site that demos different layout situations with code samples provided.

How to resize controls at runtime in java

Is there a way to resize a control, a JTextfield for example, at runtime in java? I want my textfield to have the resize cursors (just like when you point your cursor on the corner of a window) and will be able to resize on runtime. Ive read on the internet that vb6 and C# have those capabilities, is there anything for java? A sample code or a link to a good tutorial will be very much appreciated. Thank you.
It sounds like you are trying to implement a component editor, such as the GUI editors available in popular programing IDEs. The essential feature is a handle, a graphical object that can be selected and dragged to change the geometry. GraphPanel is a simple example of an object drawing program that illustrates many of the required techniques.
That depends on the Layout of the JTextField's container. A good tutorial is available at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
For a quick and cheap solution you could use a JSplitPane component, with the JTextField to be resized in the left side, and an empty JPanel in the right side. By default a JSplitPane is decorated with a border and a divider, but you can remove these by setting an empty border.

Can't edit any component in GUI

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

Z-order on Java Swing components

I've been looking all over for some mention of this, but I cannot find any substantial information on it. Is there a way to change the z-order of Swing components, or at least change the draw order so that they appear to be above others?
You are looking for setComponentZOrder
Here's an example.
If you want to overlay components over other components, you should use JLayeredPane, which will let you add components to specific layers. This is precisely what's used for things like tooltips and drag-and-drop operations.
Here's a tutorial for it as well.
Look at the setComponentZOrder(...) method of the Container class.
You can also check out the Overlap Layout which has a brief description on how ZOrder works.

Add transparent JPanel upon AWT Component to paint on

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

Categories