How can I position a JButton under a JTable? What kind of layouts? How? I have a JTable table what is scrollable, and the table is in a frame.
There are many layouts to fulfill this need.
The simplest is using BorderLayout:
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(new JScrollPane(jtable), BorderLayout.CENTER);
contentPane.add(button, BorderLayout.SOUTH);
If you want this button to be not resized just add JPanel to contentPane. This JPanel should contain your button centered using almost any layout.
If you would like to use more sophisticated layout - the best in my opinion is MigLayout
To correct LoveToCode's well-meaning but misleading advice, you would never want to give a JTable itself a layout. The solution to your problem is not to set the JTable's layout but to set the layout for the JPanel that holds both the JTable's JScrollPane and the JButton. Likely a BorderLayout would work best with the JScrollPane being placed BorderLayout.CENTER and the JPanel that holds the JButton BorderLayout.SOUTH.
Note, if you're adding these components to the JFrame, then know that its contentPane already uses BorderLayout (the tutorials will tell you this -- please read them). So just add these guys to the contentPane as described above.
If hard coding of swing and awt is not mandatory, try using the WindowsBuilder Pro, a Free tool now from google, install its plugin in to eclipse, then Use
GroupLayout - Introduced by NetBeans team in 2005 integrated in WindowsBuilder Pro, is one of the most convenient way create a good gui in less time in Java.
It seems that you can set the layout of a jtable the same way in which you can set the layout of a jframe or a jpane:
table.setLayout(new grideLayout(4, 3)
would give it a grid layout with 4 rows and 3 columns.
the scrollable feature allows you to 'scroll' through your table with the moving bar. It may be a default feature, or it may be that you must use it, try seeing what methods your table gives you.
Lastly, your table should be in a frame, so that you can view it on your window, make you class extend JFrame and it will automatically be a frame upon which you can simply add a table!
Related
I'am using panel with absolute layout (don't ask why) and I need to add elements on it programmatically. I done that part, but now I want to surround panel with JScrollPane so that when user add more items, scroll bar does its job. But surrounding panel with scroll bar doesn't work. What can I do here.
JFrame frame = new JFrame();
frame.setSize(582, 451);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 11, 546, 391);
frame.getContentPane().add(scrollPane);
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
panel.setLayout(null);
for(int i=0;i<labelList.size();i++){
if(a==4){
a=0;
b++;
}
labelList.get(i).setBounds(10+120*a+10*a, 10+130*b+10*b, 120, 130);
labelList.get(i).setOpaque(true);
labelList.get(i).setBackground(Color.WHITE);
labelList.get(i).setText(Integer.toString(i));
panel.add(labelList.get(i));
a++;
}
You're not going to like my answer, but I feel that we should be compelled to give you the best answer, which is not always what you want to hear. And that is this: use layout managers to do your component layouts. Yes while it might seem to newbies that null layouts are the easiest to use, as you gain more experience you will find that exactly the opposite is true. Null layout use makes for very inflexible GUI's that while they might look good on one platform look terrible on most other platforms or screen resolutions and that are very difficult to update and maintain. Instead you will want to study and learn the layout managers and then nest JPanels, each using its own layout manager to create pleasing and complex GUI's that look good on all OS's.
As for ease of use, I invite you to create a complex GUI with your null layout, and then later try to add a component in the middle of this GUI. This will require you to manually re-calculate all the positions and sizes of components added to the left or below the new component, which is prone to many errors. The layout managers will all do this automatically for you.
Specifically use of valid layout managers will update your JPanel's preferredSize automatically increase as more components are added and as the JPanel is revalidated (the layouts are told to re-layout their components). Your null JPanel will not do this, and so the JScrollPane will not work. Yes, a work around is for you to manually calculate and set your JPanel's preferredSize, but this is dangerous and not recommended for the same reasons noted above.
Is there any way to autoresize a JTabbedPane to the same size that the JFrame has?
I'm using netbeans to create the swing interface and I can't figure out whether this is a property that I can set through the GUI designer or if I have to do it programmatically. If I need to do it programatically, netbeans doesn't allow users to modify the auto generated UI code.
Thanks a lot in advance
The Visual Guide to Layout Managers should be useful for understanding how components are arranged in container.
For your task I would use BorderLayout and placed the JTabbedPane into it's center area.
For example:
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(pane, BorderLayout.CENTER);
In netbeans you just change your frame layout to BorderLayout and after that in properties for the JTabbedPane set layout constraint CENTER
I am using netbeans 7.0.1 to build a simple JFrame application
I am putting a textarea and a couple of buttons on using the gui builder
the buttons are on the same vertical level and the right hand button shifts right on resize of the window - that is fine but I would like the text area to do the same - i.e. resize to fit the relevant width of the window.
For the life of me I cannot see how this is done - I have looked around and I can find code for a hand coded app but not for netbeans gui builder
Update: Ah sorry, didn't read the full question, you really want to do it with netbeans.. :) Well, well, now you have this post how to do it hand-crafted aswell! :)
I wouldn't use an GUI builder for this task. It is easy to create such layout with FlowLayout and BorderLayout:
Screenshot was produced by this code:
public static void main(String... args) throws Exception {
JFrame frame = new JFrame("Test");
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
buttonPanel.add(new JButton("Hello"));
buttonPanel.add(new JButton("World!"));
frame.add(buttonPanel, BorderLayout.NORTH);
frame.add(new JTextArea("Hello World!"), BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.setVisible(true);
}
It's all about the layout you're using. I would personally use GridBagLayout, probably because I am accustomed to it. Basically, you should follow these steps:
Change the layout of the container which owns the textarea to GridBagLayout. You can do that by right-clicking on the container(being it the JFrame, a panel, whatever) and there you will see the Layout menu. It contains a GridBagLayout option.
In the component inspector select the JScrollPane that owns the JTextArea. Check out the "Layout" section in the properties tab. It contains the GridBagConstraints which command the layout behaviour of the JScrollPane and thus commands the JTextArea.
Play with the layout properties :). Basically you should set the X and Y weight to 1, and the Fill to "Both". This will tell the JScrollPane to fill any vertical and horizontal space there is on the Frame, and the X and Y weight will pull any other components as far as possible.
You can read more about GridBagLayout here: http://netbeans.org/kb/docs/java/gbcustomizer-basic.html
Learning GridBagLayout could take a couple of hours, getting used to it could take a couple of days, but it's worth learning. Just my 2 cents.
I want to add a Jpanel on a jscrollpane; also I want to have only vertical scrolling. I want to set layout of my jPanel "flowLaout" and add several components to my jPanel in my code by jpanel.add(component) method. The result is that all components are placed in just one row that excide width of jpanel and are not shown. I have used this tricks and both failed:
jScrollPane1.setHorizontalScrollBar(null);
jScrollPane1.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
Wrap Layout should work for you.
I am unsure of the particulars for your current project, but i would recommend MigLayout. It has never served me wrong.
I am currently writing a touchscreen interface with nested MigLayout panels up to 4 or five layers deep, and have not had a single problem.
Please use the below policy to turn on vertical scrolling and turn off horizontal scrolling(Works with Java SE 7):
Panel graphicPanel = new Panel();
JScrollPane scrollbar = new JScrollPane(graphicPanel);
scrollbar.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollbar.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scrollbar.setPreferredSize(new Dimension(1300, 600));
scrollbar.setVisible(true);
add(scrollbar, BorderLayout.CENTER);
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.