Setting boundaries for JPanel - java

I am new on using Swings
my requirement is to align components within the jpanel(panel2)
I have taken 2 JPanels (panel1, panel2) and added to the jframe
panel1.add(panel2);
panel2.setLayout(new flowLayout());
panel2.setBounds(80,120,100,100);
getContentPane() .add(Panel1);
and I have created a "Create" button that will generate text area dynamically in panel2
now my problem is if the created textarea is reaching out of panel2 it has to show an error
message "You reached the boundaries of the jpanel so the textarea cant be created "
Thanks in advance

Set the layout before you add any elements to the panel. No not call setBounds as with layout manager present it likely has no effect at all.
I cannot explain how to layout your elements as from your question seems not possible to figure out that do you want to do. Best, post the drawing with elements as they should look like. GridLayout maybe would be good if you want to align multiple elements as in the table.

Related

How to put multiple JTextfields on different lines in the center of BorderLayout?

So firstly, I added the text fields and their labels into separate panels as I wanted them to be in line with each other, I added them via Flow layout;
//add them in Flow Layout
panel.add(label_1);panel.add(file_name);
pane2.add(label_2);pane2.add(h_link);
pane3.add(label_3);pane3.add(pages);
What i wanted to do next is to add them using BorderLayout in a way that all of them will be in the center of the window. Additionally, I wanted to add submit button of the details;
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.CENTER, pane2);
frame.getContentPane().add(BorderLayout.CENTER, pane3);
frame.getContentPane().add(BorderLayout.SOUTH, submit);
frame.setVisible(true);
As I am writing this post, I did not achived the result that i wanted , the screenshot attached shows the resulting frame which is not I wanted.
By all means, if you have also any suggestions for how to do it more efficiently please do share.
The key to solving this is to nest JPanels, each using its own layout, thereby allowing you to effectively nest layouts:
Place a JPanel in the BorderLayout.CENTER position
Give the JPanel a GridLayout, one allowing multiple rows and one column, e.g., new GridLayout(0, 1)
Add your JTextFields to the JPanel
Another approach is to simply use a JTable, one with a single column and multiple rows.
Another approach -- if you're trying to gain input from a user in a JLabel/JTextField grid -- is to use GridBagLayout to space the components nicely together

Java JPanel Layout

Im new in Java Swing, and want to make my layout, but can't do this
Look Now :
Look I want :
Code Now :
JPanel MainPanel = new JPanel(new GridBagLayout());
JLabel MoneyLabel = new JLabel(MoneyIcon);
MoneyLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyLabel.setText("Money:" + CarMain.Money);
JLabel MoneyClicksLabel = new JLabel();
MoneyClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
MoneyClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
MoneyClicksLabel.setText("Money Clicks: " + CarMain.MoneyClicks);
JLabel BoxesLabel = new JLabel(BoxLv9_10Icon);
BoxesLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesLabel.setText("Boxes: " + CarMain.Boxes);
JLabel BoxesClicksLabel = new JLabel();
BoxesClicksLabel.setHorizontalTextPosition(JLabel.CENTER);
BoxesClicksLabel.setVerticalTextPosition(JLabel.BOTTOM);
BoxesClicksLabel.setText("Boxes Clicks: " + CarMain.BoxesClicks);
MainPanel.add(MoneyLabel);
MainPanel.add(MoneyClicksLabel);
MainPanel.add(jbtnMoney);
MainPanel.add(BoxesLabel);
MainPanel.add(BoxesClicksLabel);
MainPanel.add(jbtnBoxes);
This is simple example of, what i want, becouse i'm building ingame shop, with 13 labels like these, in each tabbedpane window. How can i make it look, like in second picture, what I want?
Im new in Java Swing, and want to make my layout, but can't do this
Probably no single layout can suit everyone's needs. But combining several layouts can usually handle most scenarios.
From the image you showed in the question. There is no need to write your own layout. You can always use sub panels to hold your components and set a specific layout for each sub panel to handle what you need for those individual areas.
The reason for the alignment in your first attached image is because:
JPanel uses FlowLayout as its default layout. Hence all the components added will appear in a linear fashion and tries to fill up the row as much as possible the panel's width can hold. Once exceeded the panel's width, the components will be pushed to the next row.
If you want to achieve the alignment in the second attached image:
You may create a main panel to contain several sub-panels (see image below).
The red box is your main panel and you may continue to use the default FlowLayout.
Then add your components into sub-panels (orange boxes) before adding it to the main. You may then use BoxLayout, FlowLayout or even GridBagLayout for the sub panels (orange boxes).
Artis Uljanovs, at night after work i will give a look at this to help you.
I recommend you already to read the following: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
You need some foundations on Java Layouts.

How to display two canvas in a JFrame java

I recently have a requirement to display a word file within a JFrame. With this link I was able to achieve what I want (Open MS documents into JFrame). What i need is to display a word file and a pdf file side by side within a JFrame.
In the link mentioned above, the word file was displayed in a JFrame via a Canvas from SWT.
I would like to know:
Whether it is possible to add two canvases to a single JFrame.
If not, is it possible to display a word document or a PDF file in a JPanel (since I know that adding two panels to a frame is possible)?
In the example you linked the canvas is added directly to the content pane of the JFrame. What you need to do is to insert a JPanel with a Layout to the JFrame first, and after that add one or many Canvas objects to the layout. A trivial example with the default layout FlowLayout is below, feel free to modify it to use a different layout manager or add a JScrollPane or JSplitPane depending on the layout you want.
JPanel panel = new JPanel(); //Default layout manager is FlowLayout
//You could change the layout here with panel.setLayout(new ..Layout);
frame.getContentPane().add(panel);
panel.add(canvas1);
panel.add(canvas2);
Here is a useful link to layout managers. Look for example into BorderLayout if you wish to add menus etc. to your frame.

Why does BorderLayout overwrite components when new ones are added?

BorderLayout does something strange. If I add two panels to a Container with the same constraint (BorderLayout.CENTER for instance), then the first one goes away, even if the second one is deleted or made invisible
It seems as though it would make sense for it to "stack" each element on top of the previous ones.
Is this correct and by design? If so, is there some documentation on it?
Has anyone else been frustrated by it? Have you a solution, such as a custom LayoutManager?
Sample code:
JFrame frame = new JFrame();
frame.setSize(500, 500);
JPanel panel1 = new JPanel();
panel1.setBackground(Color.blue);
frame.getContentPane().add(panel1);
JPanel panel2 = new JPanel();
panel2.setBackground(Color.red);
frame.getContentPane().add(panel2);
panel2.setVisible(false); // Seems like it should allow us to see panel1.
frame.setVisible(true);
This creates and displays a 500x500 blank box.
BorderLayout was simply not designed to do what you want. Separation of responsibility. If you want that behavior you should compose: combine the BorderLayout with a CardLayout. Though for the actual stack behavior, you'll have to code something yourself (or find someone who already has.)
Is this correct and by design?
Yes.
You need to understand the basics of how layout managers work. One of the jobs of the layout manager is to set the "location" and "size" of the components added to the panel. In the case of a BorderLayout it only tracks 5 components so only the last component added to the CENTER is known by the layout manager.
Layout management is not done when components are added to the panel. It is done when the frame is packed, or made visible (or the revalidate() method is invoked) . In this case the blue panel is not part of the components managed by the BorderLayout so its size remains (0, 0), which means there is nothing to paint.
Try changing your code to:
JPanel panel1 = new JPanel();
panel1.setSize(200, 200);
and you will see the blue panel painted at the specified size.
Now try commenting out:
//panel2.setVisible(false);
and you will see both panels. This is because as components are added to the panel they are assigned a ZOrder. Basically the last component added is painted first, which is why the blue panel is painted on top of the red panel. Check out the setComponentZOrder() method of the Container class for more information.
The CardLayout is probably the layout manager you should be using, but you can check out the Overlap Layout as well.

JLabel on top of another JLabel

Is it possible to add a JLabel on top of another JLabel? Thanks.
The short answer is yes, as a JLabel is a Container, so it can accept a Component (a JLabel is a subclass of Component) to add into the JLabel by using the add method:
JLabel outsideLabel = new JLabel("Hello");
JLabel insideLabel = new JLabel("World");
outsideLabel.add(insideLabel);
In the above code, the insideLabel is added to the outsideLabel.
However, visually, a label with the text "Hello" shows up, so one cannot really see the label that is contained within the label.
So, the question comes down what one really wants to accomplish by adding a label on top of another label.
Edit:
From the comments:
well, what i wanted to do was first,
read a certain fraction from a file,
then display that fraction in a
jlabel. what i thought of was to
divide the fraction into 3 parts, then
use a label for each of the three.
then second, i want to be able to drag
the fraction, so i thought i could use
another jlabel, and place the 3'mini
jlabels' over the big jlabel. i don't
know if this will work though..:|
It sounds like one should look into how to use layout managers in Java.
A good place to start would be Using Layout Managers and A Visual Guide to Layout Managers, both from The Java Tutorials.
It sounds like a GridLayout could be one option to accomplish the task.
JPanel p = new JPanel(new GridLayout(0, 1));
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));
In the above example, the JPanel is made to use a GridLayout as the layout manager, and is told to make a row of JLabels.
The answer to your original question is yes for the reasons given that any Component can be added to a Container.
The reason you don't see the second label is because by default a JLabel uses a null layout manager and the size of the second label is (0, 0) so there is nothing to paint. So all you need to do is set the bounds of the second label and away you go.
You can't use a layout manager if you want to drag components around because as soon as you resize the frame etc, the layout manager will be invoked and the components will be repositioned based on the layout manager of the component.
it's a matter of layout.
you can do that using null layout (with hard coded locations) or with a custom layout.
you can use a JLayeredPane and set it's border to No Border.
you can add put them above each others by using the horizontal or vertical gap (hgap,vgap) the attributes of the layout
JPanel p = new JPanel(new GridLayout(2, 1,-40,0));
//the 40 is the hgap , make it the same with the label height .

Categories

Resources