Swing (Java): Difference between revisions

Content deleted Content added
Exoad (talk | contribs)
m remove irrelevancy
Greggwon (talk | contribs)
No edit summary
Tags: Reverted possible conflict of interest
Line 211:
</syntaxhighlight>
The layout is set to null using the {{Javadoc:SE|member=setLayout(LayoutManager)|java/awt|Container|setLayout(java.awt.LayoutManager)|module=java.desktop}} method since JFrame uses java.awt.BorderLayout as its default layout-manager. With BorderLayout anything which is added to the container is placed in the center and stretched to accommodate any other widgets. Of course, most real world GUI applications would prefer to use a layout-manager instead of placing everything on absolute co-ordinates.<ref>{{cite book|last1=Eckel|first1=Bruce|title=Thinking in Java|date=2006|publisher=Prentice Hall|isbn=978-0131872486|page=942|edition=4|url=http://www.agentgroup.unimore.it/~nicola/courses/IngegneriaDelSoftware/java/books/ThinkingInJava.pdf|access-date=13 May 2016|url-status=dead|archive-url=https://web.archive.org/web/20160514000820/http://www.agentgroup.unimore.it/~nicola/courses/IngegneriaDelSoftware/java/books/ThinkingInJava.pdf|archive-date=14 May 2016}}</ref>
===A GridBagLayout example using the Packer subclass of the GridBagLayout Manager===
[[File:RunningApplication.png|thumb]]
The GridBagLayout class provides a rectangular layout mechanism which is specified using instances of the GridBagConstraints class. This mechanism is very tedious in code because of all the individual lines of code needed to specify the attributes in the GridBagConstraints instances, and the fact that the constraints are not reusable due to each instance used being referenced continuously at runtime. The Packer and PackAs class and interface available at https://github.com/greggwon/Packer, provide a repackaging of the power of this layout manager into a different API. The instances of GridBagConstraints are managed behind the scenes and this makes it much easier to create layouts of components.
 
The name Packer was used because this chaining API style mimics the original TCL/TK "pack" command that this API was designed to mimic when Java was first released and people were using tcl/tk for UIs on lots of different systems.
 
The example application below shows how easy it is to create a 10 line input form using a loop without having to use layered/stacked FlowLayout or other hard coded static layouts.
<syntaxhighlight lang="java">
import javax.swing.*;
import java.awt.*;
import org.wonderly.awt.*;
 
public class ExampleGridbagLayout extends JFrame {
 
public static void main(String args[]) {
ExampleGridbagLayout f = new ExampleGridbagLayout(args);
f.pack();
f.setLocationRelativeTo( null );
f.setVisible(true);
}
 
protected ExampleGridbagLayout( String args[] ) {
Packer pk = new Packer(this);
int y = -1;
for( int i = 0; i < 10; ++i ) {
// On new row
pk.pack( new JLabel("Field #"+(i+1)) ).gridx(0).gridy(++y).inset(0,4,0,4).fillx(0);
pk.pack( new JTextField(10) ).gridx(1).gridy(y).fillx();
}
 
// On new row
pk.pack( new JSeparator() ).gridx(0).gridy(++y).gridw(2).fillx().inset(4,4,4,4);
 
// On new row
pk.pack( new JButton("Cancel") ).gridx(0).gridy(++y).west();
pk.pack( new JButton("Okay") ).gridx(1).gridy(y).east();
}
}
</syntaxhighlight>
==See also==
*[[swingLabs]] – Extensions to Swing