I am trying to make this layout in Java swing. It is a 3x3 grid layout in the middle, with an upper panel and a lower panel on the bottom.
By default a JFrame uses a BorderLayout. You can take advantage of the BorderLayout with code like:
frame.add(topPanel. BorderLayout.PAGE_START);
frame.add(gridPanel, BorderLayout.CENTER);
frame.add(bottomPanel, BorderLayout.PAGE_END);
Read the section from the Swing tutorial on Using Layout Managers for more information and working examples.
Keep a link to the tutorial handy for Swing Basics.
Related
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!
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);
How can I specify the location on JFrame that a component (JLabel specifically) is placed? I created a JFrame object and added a JLabel and a JList to the frame but both objects are being placed on top of each other. I have tried using
label.setBounds(10,10,10,10);
list.setBounds(20,20,100,100);
and
label.setLocation(10,10);
list.setLocation(10, 50);
Neither of these are working. Any help is appreciated! Thanks.
In Java, layout managers are used which determine the way the components will be placed. You can find more information about layout managers here: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
If you definitely want to use coordinates to put your components, you could try:
JFrame frame = new JFrame();
frame.setLayout(null);
Otherwise, a very good GUI editor for Java is NetBeans which is using the GroupLayout by default.
Layout Managers will do that for you.
With the default Layout BorderLayout, try
setLayout(new BorderLayout());
getContentPane().add(yourLabel, BorderLayout.NORTH);
getContentPane().add(yourList, BorderLayout.CENTER);