Source Code Cross Referenced for GridLayout3.java in  » IDE » tIDE » snow » utils » gui » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE » tIDE » snow.utils.gui 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package snow.utils.gui;
002:
003:        import java.awt.*;
004:        import java.awt.event.*;
005:        import javax.swing.*;
006:        import javax.swing.event.*;
007:        import javax.swing.border.*;
008:        import java.util.*;
009:
010:        /** A grid layout using gridbag layout.
011:         It has better resizes features than the GridLayout2
012:         */
013:        public class GridLayout3 extends GridBagLayout {
014:            int cols;
015:            JPanel target;
016:            final public GridBagConstraints constr = new GridBagConstraints();
017:
018:            final Hashtable<Integer, Integer> columnAligmnents = new Hashtable<Integer, Integer>();
019:
020:            /** USAGE: add the component to this layout with
021:               add(comp, true/false), not with the panel add !
022:             */
023:            public GridLayout3(int cols, JPanel target) {
024:                super ();
025:
026:                this .cols = cols;
027:                this .target = target;
028:
029:                constr.gridwidth = 1;
030:                constr.anchor = GridBagConstraints.WEST;
031:                constr.insets = new Insets(1, 5, 1, 1);
032:
033:                target.setLayout(this );
034:            }
035:
036:            /** @param alignment is one of GridBagConstraints.WEST, ...
037:                if not specified, default is WEST
038:             */
039:            public void setColumnAlignment(int col, int alignment) {
040:                columnAligmnents.put(new Integer(col), new Integer(alignment));
041:            }
042:
043:            // used as a counter
044:            private int actualCol = 0;
045:            private double[] weights = null;
046:
047:            /** Example: give {0,10,0} to cause only the second column to be resized
048:             */
049:            public void setColumnWeights(double[] w) {
050:                this .weights = w;
051:            }
052:
053:            public JLabel add(String a) {
054:                JLabel jl = new JLabel(a);
055:                jl.setOpaque(false);
056:                jl.setAlignmentX(JComponent.LEFT_ALIGNMENT);
057:                add(jl, false);
058:
059:                Dimension ps = jl.getPreferredSize();
060:                jl.setMinimumSize(ps); // MUST be set, otherwise, resize is NOT possible
061:                return jl;
062:            }
063:
064:            /** Uses a textfield.
065:             */
066:            public JTextField addTF(String a) {
067:                JTextField jl = new JTextField(a);
068:                jl.setOpaque(false);
069:                jl.setEditable(false);
070:                jl.setAlignmentX(JComponent.LEFT_ALIGNMENT);
071:                jl.setBorder(null);
072:                add(jl, false);
073:                Dimension ps = jl.getPreferredSize();
074:                jl.setMinimumSize(ps); // MUST be set, otherwise, resize is NOT possible
075:                return jl;
076:            }
077:
078:            /** Don't fills.
079:             */
080:            public JComponent add(JComponent comp) {
081:                add(comp, false);
082:                return comp;
083:            }
084:
085:            public JTextPane addExplanationArea(String expl) {
086:                JTextPane tp = new JTextPane();
087:                tp.setAlignmentX(JComponent.LEFT_ALIGNMENT);
088:                tp.setEditable(false);
089:                tp.setText(expl);
090:                tp.setBackground(GUIUtils.giveYellowTouch(target
091:                        .getBackground()));
092:
093:                constr.gridwidth = constr.REMAINDER;
094:                constr.fill = constr.HORIZONTAL;
095:                this .setConstraints(tp, constr);
096:                target.add(tp);
097:                actualCol = 0;
098:
099:                return tp;
100:            }
101:
102:            public JComponent addTitleSeparator(String title) {
103:                // Color.darkGray
104:                return addTitleSeparator(title,
105:                        TitledBorder.DEFAULT_JUSTIFICATION);
106:            }
107:
108:            public JComponent addTitleSeparator(String title,
109:                    int TitledBorderjustification) {
110:                // Color.darkGray
111:                return addTitleSeparator(title, UIManager
112:                        .getColor("TitledBorder.titleColor"),
113:                        TitledBorderjustification,
114:                        TitledBorder.DEFAULT_POSITION, UIManager.getFont(
115:                                "Label.font").getSize(), 0);
116:            }
117:
118:            public JComponent addSeparator() {
119:                constr.gridwidth = constr.REMAINDER;
120:                constr.fill = constr.HORIZONTAL;
121:                JPanel pan = new JPanel(new BorderLayout(0, 0));
122:                pan.setOpaque(false);
123:                pan.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
124:                this .setConstraints(pan, constr);
125:                target.add(pan);
126:                actualCol = 0;
127:                return pan;
128:            }
129:
130:            /** you have to import javax.swing.border.*;
131:             @param TitledBorderjustification TitledBorder.CENTER, ...
132:             @param TitledBorderposition TitledBorder.DEFAULT_POSITION, ...
133:               the color should be UIManager.getColor("TitledBorder.titleColor");
134:             */
135:            public JComponent addTitleSeparator(String title, Color lineColor,
136:                    int TitledBorderjustification, int TitledBorderposition,
137:                    int spaceAbove, int spaceBelow) {
138:                constr.gridwidth = constr.REMAINDER;
139:                constr.fill = constr.HORIZONTAL;
140:                JPanel pan = new JPanel(new BorderLayout(0, 0)); //new BorderLayout());
141:                pan.setOpaque(false);
142:                pan.setBorder(BorderFactory.createCompoundBorder(BorderFactory
143:                        .createEmptyBorder(spaceAbove, 0, spaceBelow, 0),
144:                        BorderFactory.createTitledBorder(BorderFactory
145:                                .createMatteBorder(1, 0, 0, 0, lineColor),
146:                                title, TitledBorderjustification,
147:                                TitledBorderposition)));
148:
149:                this .setConstraints(pan, constr);
150:                target.add(pan);
151:                actualCol = 0;
152:                return pan;
153:            }
154:
155:            public JComponent add(JComponent comp, boolean fillHorizontally) {
156:                constr.fill = (fillHorizontally ? constr.HORIZONTAL
157:                        : constr.NONE);
158:
159:                if (actualCol % cols == cols - 1) {
160:                    // this is for the last column of each row
161:                    constr.gridwidth = constr.REMAINDER;
162:                } else if (enforceLineBreakAfter) {
163:                    constr.gridwidth = constr.REMAINDER;
164:                } else {
165:                    // all the other columns
166:                    constr.gridwidth = 1;
167:                }
168:
169:                if (this .columnAligmnents.containsKey(actualCol)) {
170:                    constr.anchor = this .columnAligmnents.get(actualCol);
171:                } else {
172:                    // default
173:                    constr.anchor = constr.WEST;
174:                }
175:
176:                if (weights != null && actualCol < weights.length) {
177:                    constr.weightx = weights[actualCol];
178:                } else {
179:                    // default behaviour, resize all but the first
180:                    if (actualCol == 0) {
181:                        constr.weightx = 0;
182:                    } else {
183:                        constr.weightx = 1000;
184:                    }
185:                }
186:
187:                this .setConstraints(comp, constr);
188:                target.add(comp);
189:
190:                actualCol++;
191:                if (actualCol == cols) {
192:                    actualCol = 0;
193:                }
194:
195:                // always reset, use only once when set to true
196:                if (enforceLineBreakAfter) {
197:                    // always reset, use only once when set to true
198:                    enforceLineBreakAfter = false;
199:                    actualCol = 0;
200:                }
201:                return comp;
202:
203:            }
204:
205:            boolean enforceLineBreakAfter = false;
206:
207:            /** force next added component to terminate the line
208:             */
209:            public void insertLineBreakAfterNextComponent() {
210:                enforceLineBreakAfter = true;
211:                //   constr.gridwidth = constr.REMAINDER;
212:                //   actualCol = 0;
213:
214:            }
215:
216:            /**
217:             *  Usage demos
218:             public static void main( String[] arguments )
219:             {
220:                test1();
221:             }
222:
223:
224:             public static void test1()
225:             {
226:               JPanel panel = new JPanel();
227:
228:               GridLayout3 gl = new GridLayout3(3, panel);
229:               gl.setColumnAlignment(0, GridBagConstraints.EAST);
230:               gl.setColumnWeights(new double[]{0,1000,0});
231:
232:               JFrame jf = new JFrame("test");
233:
234:               jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
235:
236:               jf.setContentPane(panel);
237:
238:               gl.add( "a");
239:               gl.add( new JTextField(8), true);
240:               gl.add( new JLabel("mm"), false);
241:
242:               gl.add( "b");
243:               gl.add( new JTextField(6), true);
244:               gl.add( new JLabel("cm"), false);
245:
246:               gl.addTitleSeparator("Hello");
247:
248:               gl.add( "Hello");
249:               gl.add( new JTextField(), true);
250:               gl.add( new JLabel("km"), false);
251:
252:               gl.addTitleSeparator("Hello", Color.yellow, TitledBorder.CENTER, TitledBorder.ABOVE_TOP, 100, 50);
253:
254:               jf.pack();
255:               jf.setLocationRelativeTo(null);
256:               jf.setVisible(true);
257:
258:             } // main
259:
260:
261:             public static void test2()
262:             {
263:               JFrame frame = new JFrame("GridLayout3 line break test");
264:               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
265:
266:               JPanel content = new JPanel();
267:               frame.add(content, BorderLayout.CENTER);
268:               GridLayout3 gl3 = new GridLayout3(5,content);
269:
270:               for(int i=0; i<20; i++)
271:               {
272:                 JButton jb = new JButton(getRandomString());
273:
274:                 if(i==2)
275:                 {
276:                   gl3.insertLineBreakAfterNextComponent();
277:                   jb.setBackground(Color.red);
278:                 }
279:
280:                 gl3.add(jb);
281:
282:               }
283:
284:               frame.pack();
285:               frame.setLocationRelativeTo(null);
286:               frame.setVisible(true);
287:             }
288:
289:             public static String getRandomString()
290:             {
291:                int len = (int) (Math.random()*30) + 1;
292:                StringBuilder sb = new StringBuilder();
293:                for(int i=0; i<len; i++)
294:                {
295:                  sb.append(""+(char)('a'+i));
296:                }
297:                return sb.toString();
298:             }*/
299:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.