JLayeredPane disrupting Layout - java

I want to have a calendar with entries overlapping a JTable.
That JTable is inside a scrollpane which again is inside a JLayeredPane.
setLayout(new MigLayout("", "[:20%:200px,grow][26%,grow][26%,grow][26%]", "[:15%:80px,grow][85%,grow]"));
add(layeredPane, "cell 1 1 3 1,grow");
layeredPane.setLayout(new MigLayout("", "[100%,grow]", "[100%,grow]"));
layeredPane.setBackground(Color.BLACK);
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
layeredPane.add(scrollPane, "cell 0 0,grow",0);
I'm now calculating the bounds for my panel which is supposed to be an entry. When adding that entry to the JLayeredPane however, the whole layout is disrupted and it seems to be on the same layer. Even if my calculated values are wrong, that wrong result is still supposed to show up on a different layer.
JPanel panel = new JPanel();
panel.setBounds((int)position.getX(), (int)position.getY(), width, height);
panel.setBackground(Color.BLACK);
pane.add(panel, 300); // This is the JLayeredPane
When I go into fullscreen however, this happens:
Why is the pane not showing the panel on a different layer and why is my layout corrupted by it?

pane.add(panel, 300);
Well, that appears to be the code where you add your panel to the layered pane. Although elsewhere in your code you also have a variable called "layeredPane", so I'm not sure.
If that is referring to the layered pane then that is not how you add a component to the layer:
why would you use 300 for the layer? that is not a pixel location
The layer is specified by an Integer value, not an int.
Read the section from the Swing tutorial on How to Use Layered Panes for more information and a working example.

Related

Text Field setBounds function is not working on a particular JFrame

I am trying to create a JPanel with 3 text fields. Everything else including buttons is falling into place except for textArea3. The final panel is something like this. As you can see in picture, textArea3 uses entire JFrame instead of following setBounds method.
//Text Area 1
JTextArea textArea = new JTextArea();
JScrollPane jScrollPane1 = new JScrollPane(textArea);
jScrollPane1.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
textArea.setWrapStyleWord(true);
textArea.setLineWrap(true);
textArea.setFont(new Font("Consolas", Font.LAYOUT_LEFT_TO_RIGHT, 20));
JTextArea textArea2 = new JTextArea();
JScrollPane jScrollPane2 = new JScrollPane(textArea2);
jScrollPane2.setVerticalScrollBarPolicy(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
jScrollPane2.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
textArea2.setWrapStyleWord(true);
textArea2.setLineWrap(true);
textArea2.setFont(new Font("Consolas", Font.LAYOUT_LEFT_TO_RIGHT, 20));
//Text Area 3
JTextField textArea3 = new JTextField();
textArea3.setFont(new Font("Consolas", Font.LAYOUT_LEFT_TO_RIGHT, 20));
jScrollPane1.setBounds(30,30,300,300);
jScrollPane2.setBounds(30,400,200,200);
//textArea3 is not working
textArea3.setBounds(600,800,100,50);
button2.setBounds(350,30,80,30);
button1.setBounds(350,400,80,30);
frame.add(button1);
frame.add(button2);
frame.add(jScrollPane2);
frame.add(jScrollPane1);
frame.add(textArea3);
frame.setVisible(true);
EDIT: So this was a bug within the JDK probably. I made another class called class frame and set methods to produce text area etc.
As you can see in picture, textArea3 uses entire JFrame instead of following setBounds method.
No I can't see. I see 5 components. I don't see any component that uses the entire frame.
If in fact you do see the text area take up the whole frame that is because:
the default layout manager for a JFrame is the BorderLayout
when you add components to a BorderLayout and don't specify a constraint, the CENTER is assumed.
however only a single component can be displayed in the CENTER so the layout manager will only give a size/location to the last component added, which happens to be textArea3.
The other components only happen to appear because you manually set the bounds of each component.
You should NOT be attempting to set the bounds of a component. It is the job of the layout manager to set the size and location of each component.
So the solution is to get rid of all the setBounds() statement and use layout managers.
Read the section from the Swing tutorial on Layout Managers for more information. It appears you are tying to use a grid so you can probably use a GridBagLayout.
Also when you create a JTextArea you should use something like:
JTextArea textArea = new JTextArea(15, 20);
This will allow the text area to calculate its size so that 15 rows will display with about 20 characters in each row. The size will be calculated based on the Font used.

Make buttons split JPanel - Java Swing

I've got a BorderLayout going on, and am working on the North panel. Inside the North panel, I'd like to have 3 components: a picture that is on the left, and two buttons that split the remaining width of the Frame. Right now I'm attempting to accomplish this with another BorderLayout.
The Frame is resizable.
The picture is assigned to BorderLayout.WEST, and with the following code I attempt to add another panel that contains only buttons. The panel is then added to the CENTER of the Frame's NORTH layout component.
//create panel to hold buttons
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BorderLayout());
JButton btnMatrix = new JButton("Matrix View");
btnPanel.add(btnMatrix);
JButton btnList = new JButton("List View");
btnPanel.add(btnList);
add(btnPanel);
however, the buttons both try to take up the entire panel. If I leave it to a flow layout (I don't use btnPanel.setLayout(new BorderLayout()); in the above code), the buttons sit nicely in the center, but do not expand and share the btnPanel.
Thoughts? I'm new enough to Java that I could be going about this the wrong way from the start.
btnPanel.setLayout(new BorderLayout());
You didn't specify a constraint when you added the buttons to the panel. So both buttons are added to the CENTER. However, only one component can be added to the CENTER, so only the last one added is displayed.
You can try a different layout:
btnPanel.setLayout( new GridLayout(0, 2));
Then each button will be the same size and both buttons will fill the space available.

Java GUI Layouts

Could somebody tell me which java layout I need to use to achieve the layout below:
I am currently playing out with the FlowLayout however I can’t get the entry fields to line up beside the output window:
Apologies if this is a simple question this is my first time using java. Here is my frame code:
private void makeFrame()
{
setLayout(new FlowLayout(0));
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JPanel panel5 = new JPanel();
JPanel panel6 = new JPanel();
JPanel panel7 = new JPanel();
panel1.setLayout(new FlowLayout(0));
panel2.setLayout(new FlowLayout(0));
panel3.setLayout(new FlowLayout(0));
panel4.setLayout(new FlowLayout(0));
panel5.setLayout(new FlowLayout(0));
panel6.setLayout(new FlowLayout(0));
panel7.setLayout(new FlowLayout(0));
JLabel firstnameJLabel = new JLabel("First Name");
JLabel lastnameJLabel = new JLabel("Last Name");
JLabel streetJLabel = new JLabel("Street");
JLabel townJLabel = new JLabel("Town");
JLabel postcodeJLabel = new JLabel("Post Code");
panel1.add(listAllBtn);
panel1.add(listPersonalBtn);
panel1.add(listBusinessBtn);
panel1.add(addPersonalBtn);
panel1.add(addBusinessBtn);
panel1.add(deleteBtn);
panel1.add(findBtn);
panel1.add(quitBtn);
panel2.add(firstnameJLabel);
panel2.add(this.firstNameField);
panel2.add(this.bookScrollPane);
this.outputArea.setEditable(false);
panel3.add(lastnameJLabel);
panel3.add(this.lastNameField);
panel4.add(streetJLabel);
panel4.add(this.streetField);
panel5.add(townJLabel);
panel5.add(this.townField);
panel6.add(postcodeJLabel);
panel6.add(this.postcodeField);
panel7.add(enterBtn);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
add(panel5);
add(panel6);
add(panel7);
enterBtn.addActionListener(this);
}
Use multiple nested layout managers
The example looks like a top-level BorderLayout with scroll pane in the CENTER location, a row of buttons (using FlowLayout) in the NORTH location and a GridLayout for the text fields in the WEST location. The latter could be improved by using a GroupLayout, which allows rows and columns to be sized individually but is somewhat complex to use.
There's a great tutorial on using layout managers (unfortunately it seems to have disappeared from Oracle's servers and the link points to a probably transient copy).
Been a while since I worked with Swing, but it looks like the architecture is something like this:
You have a panel in the bottom which is BorderLayout
Inside that, you add a total of 4 new panels, NORTH, WEST, CENTER and EAST
in BorderLayout.NORTH you add a panel which have FlowLayout.LEFT
in BorderLayout.WEST you add a panel which have GroupLayout.YAXIS. this panel contains the labels for names etc and the ENTER button
in BorderLayout.CENTER you add the textfields that corresponds with the labels
in BorderLayout.EAST you add the JSCrollpane.
This might give you an idea and you can play around with these different panels to achive what you want
I would just use MigLayout for the high level page layout, and then dropdown to the simple layout managers for the . It's essentially a grid layout, but it's very easy to use. Miglayout cannot wrap items though, and this is apparently a design issue in Swing. WrapLayout is a layout manager that gives you that functionality, but it can have issues.
http://tips4java.wordpress.com/2008/11/06/wrap-layout/
Looking at the screen shot you've provided I've mocked up the way you would want to divide it up. You'd have 9 rows total (0-8) and 3 columns total (0-2). For stuff like the controls at the very top, you will "span" them across all three columns.
For the text, you just put the text inside of it's individual box in row 1, column 1 for First Name, or row 2, column 1 for Last Name, etc.
You do the same thing with the input boxes.
In the picture below the blue are the columns and the orange are the rows.
So to summarize;
Use Miglayout for your high level layout. It's like using a table in HTML.
Use other layout managers to layout the items inside of the grid boxes that Miglayout provides.
The best choise is using Gridbag layout as you have a bit complex UI and GridBag layout provides all the support you needed to achive the exact UI.You will have to use a parent panel and then the child panels in it.Each panel you will have to add seperate GridBag layouts.You can add insets and necessary growing to achieve what you want.
As stated in the previous comment, I would use MigLayout in this project. As you can use split, span and wrap after each field or textbox in order to get the correct layout. You can also debug Miglayout and see where your layout its right or wrong.
download the latest version of MigLayout here : http://www.migcalendar.com/miglayout/versions/
if using eclipse,
save it in a folder called Lib in the project folder.
configure the project build path but adding the jar file to the classpath.
Then you should be able to set the layout to MigLayout.
panel.setLayout(new MigLayout());
or to debug the layout - panel.setLayout(new MigLayout("debug"));

Automatic wrapping using MigLayout

I'm looking to wrap a JPanel when it reaches the 'edge' of the screen using MigLayout. At the moment I have a JScrollPane (which I only want to be enabled vertically). The JScrollPane contains any number of JPanels which are arranged horizontally - when a panel is added so that the JPanel would go off the edge I want it to add to the next line. Is this possible?
This is the code:
public void setupPanels(){
JScrollPane scrollPane = new JScrollPane();
JPanel mainPanel = new JPanel(new MigLayout("insets 2"));
for (Object object : objects){
JPanel subPanel = new JPanel(new MigLayout("insets 0"));
mainPanel.add(subPanel, "alignx left, gapx 2px 5px, gapy 2px 2px, top");
}
scrollPane.setViewportView(mainPanel);
}
Also, to add an extra factor, every time it reaches the edge I need to add a new/different panel (a timeline) - so is there a way of finding out when it is going to wrap onto a new line?
Thanks
MigLayout does not have such a feature. It is based on a grid and while you can use the nogrid option to flow components horizontally or vertically in a cell span, you cannot make them flow into the next row or column.
The java.awt.FlowLayout contained in the JDK wraps the contained components automatically:
JPanel mainPanel = new JPanel(new FlowLayout());
mainPanel.add(subPanel1);
mainPanel.add(subPanel2);
mainPanel.add(subPanel3);
...
The preferred height is off, but there are ways to fix this, see WrapLayout.
As for the second requirement:
Also, to add an extra factor, everytime it reaches the edge I need to
add a new/different panel (A timeline) - so is there a way of finding
out when it is going to wrap onto a new line?
A layout manager should layout components that have already been added to a container, not add new components based on the results of the layout. Adding invisible placeholder components for the timeline after each subpanel that are made visible by the layout manager on demand might work.
You definitely need a custom layout manager to do this. To get started I would recommend to take the source of FlowLayout. In the layoutContainer implementation there is a loop that iterates over all components. After a line wrap, check if the next component is a timeline placeholder component, make it visible and wrap again.

How do I add ScrollPane to a panel in Java?

I want to add different buttons, vertically stacked, to a JPanel at run-time and use a JScrollPane so that all buttons will be visible (with some scrolling).
In order to do this, I have added my JPanel to a JScrollPane, after which I add buttons to my JPanel.
However, when I do this the vertical scrollbar does not allow me to see all images. For example when I add 7 buttons I can only scroll to see 5 full images and half of the 6 images.
Why doesn't my scrollbar allow me to display all 7 buttons?
Create the panel and scrollpane like:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane( panel );
When you add buttons to the panel at run time the code should be:
panel.add( button );
panel.revalidate();
As long as you are using a layout manager the preferred size will be recalculated and the scrollbar will appear.
Make scroll pane a wrapper over your panel - new JScrollPane (myPanel) and add it instead of naked panel in your panel's container.
You also may want to play with its setPreferredSize() method.

Categories