I have created a GUI with NetBeans GUI Builder and manually wrote some additional code.
I don't really know how to properly describe this problem, so please have a look at the picture below.
This is a default view, where you can notice the JTable that has been created inside a JPanel manually:
jPanel1.setLayout(new BorderLayout());
TableModel model = db.populateJTable();
JTable jTable1 = new JTable(model);
jTable1.setModel(model);
JScrollPane tableContainer = new JScrollPane(jTable1);
jPanel1.add(tableContainer);
View.super.getContentPane().add(jPanel1);
//View.super.pack();
View.super.setVisible(true);
When I try to resize the window horizontally, all components react properly by resizing as specified, but the JTable just stay where rendered before, overlapping with other elements:
Can someone please tell me what is happening and how should I proceed?
many thanks
A powerful solution: use Layouts. They do all the job for you while resizing.
A good one is GridBagLayout. If you need more complex layout, you can use the powerful BoxLayout
That if you want to design your layout manually. For me, I prefer using NetBeans 7.2.1 designer. It automatically configures the layout for you while designing them in the design window.
Related
I'm starting using java with NetBeans IDE. I'm using drag and dop GUI, it's so easy to use, but I got a problem. I'm writing this code at the contructor:
JComboBox combobox=new JComboBox();
combobox.addItem("Apple");
combobox.addItem("Banana");
for(int i=1;i<=10;i++){
combobox.addItem(i);
}
just right above initComponents(); hoping that my new combobox will shown when I run the project, but it doesn't. Did I do something wrong? Thanks in advance
Yes, you are creating a JComboBox, and yes, you are adding items to it (numeric and int -- that's a problem, but that's a discussion for another day), but no, you're not showing any code where you add this newly created JComboBox to a component that is displayed in the GUI. To display a component in a Swing GUI, it must be created and added to a component that is ultimately displayed in a top-level window, in the "GUI".
So this begs the question, how do you add your created JComboBox to your GUI that you've created with drag and drop code? One way: you could add it to say a JPanel that's already in your GUI, but you will need to do this after initializing components, usually this means after the constructor calls initComponents(), and you'll also need to make sure that this JPanel uses a layout manager that makes it easy for it to accept new components (this means most of the layout managers except NetBean's default layout, GroupLayout).
There are other issues, such as whether or not the container holding your JComboBox is large enough to display it, but the best suggestion that I can give is for you to go through the Swing tutorials, and hit especially hard the layout manager section. You can find links to the Swing tutorials and other Swing resources here: Swing Info.
I would like to add components (JButton and JSpinner) to a JPanel which was created using Netbeans GUI builder. This panel uses GroupLayout, and I can't seem to use add() to add a component. Is there any way to either add something to a panel which has a GroupLayout, or change this panel to a FlowLayout?
It's definitely not an easy thing to do. It depends on where you want to add those components. I usually reserve an empty placeholder JPanel with GUI builder, and then add components to that panel, using whatever layout I want. However, this only works when you want to add components in one place. If they are scattered around the GUI, it may be not that easy.
Another options is to migrate to manual GUI creation, possibly using Netbeans-generated code as a starting point. But depending on how complex your GUI is, it may be a tedious work.
There is an option if you right-click the JPanel to change it to a FlowLayout (Set Layout). This fixed all the problems I was having.
I am somewhat new to the whole java swing scene, and I just want some clarifications due to a slight confusion I have. I have learned about orientation and buttons and all the basics. Also pointing to some good (non oracle) tutorials will be highly appreciated.
As far as I understand, we have our JFrame which is a window.
Then our JFrame consists of ContentPane, which I am using a container for.
Container content = frame.getContentPane();
Now that I have this container, can I add more containers within those containers? Let's say that I would like to have different parts that do different things, and for that I would like to create classes and such that each handle their own containers?
So what I am asking for is, how does one go about storing different content within the container? What is the proper way to go about it?
An example I would give is let's say I have a scoreboard (for soccer) that is on the top part of the window, on the middle part of the window there is some work related business stuff, and on the bottom part of the window I have some textbox that does its thing with a few buttons.
Sorry if this question is stupid, I am just trying to learning swing, and want to know proper way to arrange different components within the window.
Yes, you can. Create an instance of JPanel and add your components to it, and then add them to the frame's content pane using a string:
JPanel panel = new JPanel();
//code to add stuff to the panel
frame.getContentPane().add("Center", panel); //"North", "South", "East", "West", or "Center"
I have search on internet and found a lot of information about drawing in Java. But when I add new JFrame class in Netbeans then I cannot add a own JPane in the JFrame. Hopefully somebody can help me with this issue/question.
Drawing the JPane is possible when I make a new JFrame in a class, but I would like to use the design view in Netbeans. That is not possible when I make a new JFrame.
I look forward to receiving an answer.
It looks like you made a JFrame instance yourself, and then tried to add a JPane(l) to it using NetBeans swing builder. This wont work. Try creating a new swing class using the swing builder and let netbeans make the Jframe.
Also, make sure you set a correct layout for your JFrame.
Also, like BenCole said, I think you mean a JPanel, not a JPane.
As an alternative, create your own top level container and add your designer panel to its content pane. Here's a simple example. In this way you can limit the use of the designer to a single panel, while you explore other layouts.
Please help me create my own custom layout, container, component, layout manager...
Example:
Containers and Layout Managers
Create a window frame.
Nest panels within a frame for better layout control.
Create and display buttons in a panel.
List two component attributes that are controlled by a layout manager.
Set the layout manager for a container.
Place components in a panel using BorderLayout, GridLayout, and FlowLayout.
Name one advantage of each of the layout managers.
Create panels with titles.
i was search on google but can't find any that match my requirement
Thanks for your help
Edit: I was found with keyword "Open Source UI"
Updated: 31, Oct 2016
I would like to updated some information to make it clearly for someone who concern. Back in 6 years ago what i want to know is how to build "UI Framework" from beginning.
If you have interesting like me i would like recommend Android UI Framework is good start because of open source and well document. Enjoy deep dive in to legacy code :) Good luck
Create a window frame
new JFrame();
Nest panels within a frame for better layout control
final JFrame jframe = new JFrame();
final JPanel innerOne = new JPanel();
jframe.add(innerOne);
innerOne.add(otherComponents);
Create and display buttons in a panel
innerOne.add(new JButton("Hello World!"));
List two component attributes that are controlled by a layout manager
Obviously check out JavaDoc of BorderLayout: BorderLayout.NORTH and SOUTH
Set the layout manager for a container
innerOne.setLayout(...);
Place components in a panel using BorderLayout, ...
Just apply the layout, and add providing the arguments for the LayoutManager:
innerOne.setLayout(new BorderLayout());
innerOne.add(..., BorderLayout.NORTH);
Name one advantage of each of the layoutmanager.
Check out the JavaDoc's. They are really helpful in these situations.
Create panels with titles.
innerOne.setBorder(new TitledBorder("Hello World"));
You could use the following set of tutorials: http://java.sun.com/docs/books/tutorial/uiswing/.