Add Contents Dynamically to java swing jpanel - java

I am new to java. I was Developing apps in c# in past. I am using NetBeans gui designer. I have created a new JFrame in which layout is set to group layout in which there's scrollpane. I am adding components on runtime by using following code.
MyScrollPane.add(new javax.swing.JButton("Button1");
MyScrollPane.add(new javax.swing.JCheckBox("CheckBox1");
this.revalidate();
The problem is that these components are not showing in scrollpane.
Another problem is that there is no option for absolute layout. Because I want to generate a scrollable list which will get values from database and generate list at runtime.

NetBeans gui designer leverages GroupLayout to facilitate building of UIs. Adding components to this layout manager at runtime involves adding horizontal & vertical sequential groups which can be quite complex. A good reason to start coding using standard layout managers when starting starting out.

You shouldn't be adding anything to a JScrollPane (I assume MyScrollPane is a JScrollPane). Your buttons should be added the JPanel that is the view of the scroll pane.

Related

Java Swing JPanel scrollpane not working? [duplicate]

Before I start, I'm aware that its a bad idea to not use a Layout Manager and usually I do use one, however, I also have all my components automatically re-size and relocate based on the size of the window. In addition the program I'm working on is only intended to run on 1 machine throughout its entire lifetime. Please don't downvote me just because of lack of layout manager, I found it to be what I need for this particular program.
To my issue, I found a similar post on stackoverflow but a solution was never achieved.
I'm adding a dynamic amount of JLabels to my JPanel, I've noticed that when not using a layout manager, the scroller doesn't work.
This is a simplified version of my initialization code.
JPanel mypanel = new JPanel();
mypanel.setLayout(null);
mypanel.setSize(800,450);
mypanel.setForeground(Color.WHITE);
mypanel.setBackground(Color.BLACK);
scrollablePanel = new JScrollPane(mypanel);
scrollablePanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollablePanel.setPreferredSize(new Dimension(800,300));
scrollablePanel.setSize(800,300);
scrollablePanel.setLocation(250,156);
myContainer.add(scrollablePanel);
where myContainer would be the container (also without a layout manager). The JLabels are added later on to the JPanel with:
enter code heremypanel.add(label1);
after some basic settings are set for the labels such as setForeground and setBackground.
Unfortunately when run, the scrollbar appears as if its not required (see image)
http://i.imgur.com/zp0QKGG.png
The table text seen in the image is made up of multiple JLabels.
If it's not possible to resolve the issue without using a Layout Manager I will switch to BoxLayout, I was just hoping there would be a solution.
The problem is JScrollPanel needs the preferred size of the view component to determine when the view exceeds the scroll pane's viewable area.
The preferred size of a component is normally determined via the layout manager. While you can use setPreferredSize, it is typically discouraged and you will simply run into the same problem as the content exceeds what ever value you decide to set.
A better solution would be to use a LayoutManager or compound layout (using multiple layout managers over multiple containers) to achieve the result you desired...or write your own...

Java Swing Application Fullscreen mode

I'm working on a Swing Application. The application needs to be in fullscreen all the time including its components. I made the application fullscreen using frmSwingapplication.setExtendedState(Frame.MAXIMIZED_BOTH);
The JFrame contains 2 JPanels. One has JTree and upon the selection of node of the JTree, a new panel is painted in another JPanel.
The problem is, when the application is minimized, it looks like
However when I maximize it, it looks like That is, only covering the portion it did while minimized. How can I resolve this issue? I tried setting size and bounds of container I got from getContentPane() but it was all useless. I even tried it with frame.pack(); and frame.setVisible(true);
You have to use proper layout managers:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
Certain layout managers will use all the available space and size/lay out components inside them to fill the whole area. Examples for such layout managers are BorderLayout, GridLayout.

Adding JScrollPane to a JPanel without a Layout Manager

Before I start, I'm aware that its a bad idea to not use a Layout Manager and usually I do use one, however, I also have all my components automatically re-size and relocate based on the size of the window. In addition the program I'm working on is only intended to run on 1 machine throughout its entire lifetime. Please don't downvote me just because of lack of layout manager, I found it to be what I need for this particular program.
To my issue, I found a similar post on stackoverflow but a solution was never achieved.
I'm adding a dynamic amount of JLabels to my JPanel, I've noticed that when not using a layout manager, the scroller doesn't work.
This is a simplified version of my initialization code.
JPanel mypanel = new JPanel();
mypanel.setLayout(null);
mypanel.setSize(800,450);
mypanel.setForeground(Color.WHITE);
mypanel.setBackground(Color.BLACK);
scrollablePanel = new JScrollPane(mypanel);
scrollablePanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollablePanel.setPreferredSize(new Dimension(800,300));
scrollablePanel.setSize(800,300);
scrollablePanel.setLocation(250,156);
myContainer.add(scrollablePanel);
where myContainer would be the container (also without a layout manager). The JLabels are added later on to the JPanel with:
enter code heremypanel.add(label1);
after some basic settings are set for the labels such as setForeground and setBackground.
Unfortunately when run, the scrollbar appears as if its not required (see image)
http://i.imgur.com/zp0QKGG.png
The table text seen in the image is made up of multiple JLabels.
If it's not possible to resolve the issue without using a Layout Manager I will switch to BoxLayout, I was just hoping there would be a solution.
The problem is JScrollPanel needs the preferred size of the view component to determine when the view exceeds the scroll pane's viewable area.
The preferred size of a component is normally determined via the layout manager. While you can use setPreferredSize, it is typically discouraged and you will simply run into the same problem as the content exceeds what ever value you decide to set.
A better solution would be to use a LayoutManager or compound layout (using multiple layout managers over multiple containers) to achieve the result you desired...or write your own...

Resize JFrame and Components Based On Computer Resoultion

I've made a java application in netbeans and am wondering how to have the size of the jframe half the width and height of the computer resolution and also having the components comply with this change. I tried putting code and it did make the frame half the height of the computer resolution but my components, such as buttons and textfields, stopped showing. How can I achieve this? Thanks.
(EDITED)
Set the JFrame's layout manager to GridLayout. In the properties window of the GridLayout itself (select in the navigator window) set columns to 1 and rows to 2. This should give you what you want and you won't have to get into the code.
This is the key code being called within the initComponents() method of your JFrame subclass (created by NetBeans) but it is important to understand where it is:
getContentPane().setLayout(new java.awt.GridLayout(2, 1));
I love Netbeans but you do have to understand the basics.
Good luck with your project. Swing is an awesome toolset that was way ahead of it's time.
As usual in this situation the key is using the right combination of layout managers for your containers. You're probably using NetBeans generated code (something I recommend you avoid until you are very comfortable with Swing coding), and it's probably having you use GroupLayout, a fine layout, but one that might not behave as well as you'd like on resizing components. I suggest that you go through the layout manager tutorial and try to nest JPanel containers and play with different layouts that re-size well such as GridLayout, GridBagLayout and BorderLayout to try to create the best layout that can re-size well.

Adding more than one JPanel in a single JFrame

I have created a JFrame - now I want to add the 4 JPanel in that frame at a particular location. How can set the location of panels in the frame?
Use (possibly nested1) layouts for the logic. See Laying Out Components Within a Container for details. They can:
Include default spacing in the constructor (often)
Calculate how big the GUI needs to be in order to display the components (in whatever PLAF, on whatever system the app. is deployed).
Extra spacing can be organized by adding an EmtpyBorder to child components.
See the nested layout example
Placing components in a container is quite a complicated subject in Swing. Instead of defining the exact places for your components, you would normally use a layout manager that arranges them in a certain way.
Here is the tutorial you should read to get a (visual) clue about the different layout managers: A Visual Guide to Layout Managers
However, the standard layout managers of Swing can be cumbersome for more complex layouts. Either, you could use nested layouts to get the desired result, or you could use a very powerful third-party library: JGoodies Forms. The downside is of course that you have to learn yet another library. Therefore, I would only recommend it for a bigger project.
For me it is good way to set GridbagLayout for the container of the frame. There are several visual swing GUI editors available to do this easily. You can use NetBeans GUI editor or GWT Designer (https://developers.google.com/web-toolkit/tools/gwtdesigner/) for complex GUI designing tasks
If its 4 locations, you can use BorderLayout,by default its the CENTRE, but it also have EAST, WEST , NORTH, SOUTH locations for the placement of the components. You can also use setLocation to put the panels in the appropriate locations, if a layout isn't used.
Its even better to use GroupLayout developed my NetBeans team in 2005, use Windows Builder Pro, now provided by google for free.
set the layout of the Frame to be null via setLayout(null)
create 4 JPanel and set their location using setLocation method
add these panels using JFrame's add method

Categories