Display element outside the frame - java

I have to realize this design with Java Swing (see screen shot).
Is it possible? How can an element appear off the frame?

Add a JToolBar (non-floatable) to JFrame or parent JPanel (in the BorderLayout.NORTH position).
Put an Icon inside a JButton and add to tool bar.
Set "MODE DIAL" (or whatever) as the tool tip for the button.
Repeat for each icon/button required.
Use a PLAF to tweak the look.

You need to use a JWindow to contain your component.

Related

Is it possible to run a JPanel (without JFrame) and display all components inside dynamically?

So basically, I am trying to get a JPanel window which will display all components inside dynamically. In other words, which will re-size the window, and display to fit its content.
I have been able to do it with help of JFrame and its pack() method which : "causes this Window to be sized to fit the preferred size and layouts of its subcomponents".
In my situation, I dont want to use JFrame because it will require much effort to make all changes.
Right now, I am able to make it work but only with the help of jscroll inside which wraps the text and or any new lines, so the window size is more static. So my JPanel is extending a TopComponent and am able to display it with:
jpanel.open();
jpanel.requestActive();
So the question is how to resize a window to fit its content upon actions in that window.
The JPanel has to be added to a Window in order to make sense. So I suggest you use layout managers correctly and you will get to a decent user interface.
When you add/remove components from a visible panel you need to use:
panel.revalidate();
panel.repaint();
Then the layout manager will lay out the components again.

Can't add component to JPanel in Swing

I want to add at runtime a JLabel under the purple pane which contains already some components, say under the progress bar:
Here is the structure of the elements:
And this is my code which is issued when an event occurs (it's getting there i checked with debug) :
jPanel1.add(new JLabel("Stack Overflow"));
jPanel1.revalidate();
jPanel3.revalidate();
I'm not seeing any changes whatsoever and have no clue where to go from here. When i put a
textarea in the purple pane and then call it's setText() method at the same place i try to add the JLabel component it works.
You need to learn more about layouts and how they work. I strongly suggest you read the entire layout manager tutorial, since understanding layouts are the solution here, and just using BorderLayout isn't the way to solve it. You'll likely want to nest layouts, perhaps using BorderLayout for the overall GUI, and having a central JPanel use BoxLayout to allow you to stack components on top of each other inside of it. Then perhaps add this JPanel to the main JPanel that uses BorderLayout in the BorderLayout.CENTER position.
Just a hunch, but maybe you need to call repaint() in addition to revalidate()
Java Swing revalidate() vs repaint()

How to add multiple components to a JFrame?

I have a JFrame.
I also have a Box class which extends Component.
This box class has a paint method which makes a filled rectangle.
When I add multiple of these Box components to my JFrame, only the most recently added one is displayed when I call repaint on the JFrame.
I took a look at the layout managers, but I am not sure that's what I want. All I want is to be able to make an animation of whole bunch of rectangles wherever I want on the screen.
(I also tried creating a panel, adding the panel to the JFrame, and then adding all the Box components to the panel. This did not work either).
Thanks in advance!
You have 2 choices.
You can change the layout of your frame:
JFrame frame;
frame.setLayout(new FlowLayout());
Now, if you add more than one box, it will show up on the frame.
The other option is to do what you said you tried. (Adding a panel to the frame)
JPanel pane = new JPanel();
frame.add(pane);
(add the boxes to 'pane')
Also, you should be careful with the sizing of your Box. You will probably want a call to setPreferredSize() somewhere in the creation of the Box. This will tell Java what size to make the box when it is added to the layout.
You should also take a look at the Java Layout Manager Tutorials. There is lots of great info there.
And, one more thing. The reason only one box at a time was being displayed on the frame was because JFrame's layout manager is BorderLayout. And, when you call add on a component that has a BorderLayout, the component is automatically added to the center of the component. Subsequent calls to add will overwrite the center component, leaving only one component in the middle.
You do need to check out other layout managers. JFrame by default uses BorderLayout and without specifying the "place" a component is added, they get added to CENTER. Depending on what you want your UI to look like depends on the layout manager to use. I would suggest maybe using Netbeans GUI builder.
EDIT: Missed the part about what you want to add but the concept is still the same, if you just add these components to the default layout manager, they will get overwritten. Sounds like you may need to do your painting inside of just one of your Box components or create a JPanel and set the layout to null but then you would have to place them explicitly. Really depends on what you want to do with it exactly.
Do your layout on paper first, then read up on Swing layout managers.
Be aware that some Swing components only allow one component to be added to them. I've run across this when using Tabbed panes. Each tab can only accept one control (JPane?) so you have to create a separate panel with a layout to arrange the related controls and then as a unit add the pane to the tab. There are similar arrangements in the Swing library.
You could set the frame layout to null and then use setBounds() to position your boxes exactly where you want.
Thank you for all your answers.
Since I am using my own custom class, Box, I have the ability of setting the position of my the rectangle through the paint method.
I realized my Box class was extending the wrong thing. It should have been extending javax.swing.Jcomponent.
If I now use a panel with an OverlayLayout, add my components to that panel, they all show up properly.

How to set JPanel size?

I'm using Netbean's form creator and I'm trying out some things. I'm not sure if it's the layout manager, but when I create my own JPanel and add it to the main content pane of the my window, the size of the panel always maximizes inside the FrameView no matter what re-dimensioning methodology I use such as setSize or setPreferred size. I'm very new to AWT and Swing.
With NetBeans WYSIWYG designer it's a peace of cake - just be sure to use Free Form Design and use mouse for resizing. Maybe the panel is itself larger than FrameView, so double click on it (you are editing it exclusively) and make it smaller. Than double click to get back to parent component and you should be fine.
Or maybe check some tutorials at NetBeans site.
You're not supposed to set sizes manually. Leave that to the layout managers, and your GUI will not break when the window size or the font or even just a button label changes.
The trick with layout managers is that you can use not just one but several, in nested JPanels. That way, nearly any layout is possible.
This happens because the layout manager of JPanel's container (perhaps it is either JFrame or another JPanel) instructs the JPanel to maximize.
Do the following:
Find out, who is the parent of this JPanel (take a look at NetBeans's containment object tree)
Check, what layout is defined for the container
Read the documentation about LayoutManager's (they're in java.awt package)
???
PROFIT!
In my experience, Swing is a very picky thing. I'd try setMaximumSize and setPreferedSize. As a side note though: when ever I used GridLayout, it always stretches whatever is in each of the cells (to make it symmetrical I guess). Flow, Box, Border, and I think GridBag don't have that problem though.
-Brett

Creating a transparent panel

I need to create a panel which should be invisible but the components inside it (for example, JTextArea, JButton, etc.) should be visible. When I click on the invisible panel, it should become visible.
I can only use JRE 1.4 and nothing more than that. :(
Any idea how to create such a transparent panel???
Transparancy is controlled with
setOpaque(false)
JComponent.setOpaque(false) is the way to go.

Categories

Resources