SimpleTableLayout
OI, Hey, Sun Micr.. erm I mean Oracle! Where is our simple HTML table like layout for java, huh? WHERE?
Sorry to say such a simple layout manager just doesn't exist, that is, until I got sick of not having one and wrote it myself. This is it.
Here is the code for that simple application. Unfortunately via strings it the only way you can set these at the moment, I might change that later.
package org.csdgn.layout; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.*; public class TableTest { /** * @param args */ public static void main(String[] args) { JFrame frame = new JFrame("SimpleTableLayout Test"); JPanel panel = new JPanel(new SimpleTableLayout(4,4)); panel.add(makeLabel(120,30,"1. Add a few without")); panel.add(makeLabel(120,30,"2. contraints to see")); panel.add(makeLabel(120,30,"3. how they lay out.")); panel.add(makeLabel(400,30,"4. A really big one that spans 3 columns!"),"colspan=3"); panel.add(makeLabel(120,90,"5. A really tall one that spans 3 rows!"),"rowspan=3;x=1;y=0"); panel.add(makeLabel(120,40,"6. A wide one that spans 2 columns!"),"colspan=2;x=2;y=0"); panel.add(makeLabel(120,90,"7. A really tall one that spans 4 rows, and overlaps!"),"rowspan=4;x=2;y=1"); panel.add(makeLabel(140,50,"8. Just add one after that overlap one. No Contraints")); panel.add(makeLabel(140,50,"9. How about one that spans 2 columns and 2 rows?"),"rowspan=2;colspan=2"); panel.add(makeLabel(140,50,"10. Jump back to the left!"),"x=0;y=last"); panel.add(makeLabel(140,50,"11. Move up 2!"),"y=last-2"); panel.add(makeLabel(140,50,"12. Move right 3!"),"y=last;x=last+3"); frame.add(panel); frame.pack(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static JPanel makeLabel(int w, int h, String text) { JPanel panel = new JPanel(new BorderLayout()); JTextPane label = new JTextPane(); label.setText(text); label.setEditable(false); label.setFocusable(false); label.setBackground(new Color((int)(Math.random()*0xFFFFFF)).brighter().brighter()); label.setOpaque(true); label.setPreferredSize(new Dimension(w,h)); panel.add(label); return panel; } }
A small example of this table layout manager I made for Java. Thats right, java. It looks a great deal like an html table doesn't. Well it is suppose to.
For those looking for a technical definition, it is a table with with variable content sized rows and columns. Like HTML tables, they size based on the largest component in that dimension for that column and row. Unlike java grid layouts, which are fixed in every row and column to that size.
Also, it happens to have the ability to span columns or rows, just like an html table. It uses simple equal distribution for sizing rows spanned based on content size. Basically I wrote an HTML table renderer, and then converted it for JAVA.
The main difference between this and a HTML table is that this can have overlapping components, multiple components in the same cell, and so on.
Unfortunately, unlike HTML tables it cannot yet stretch based on its container, I will probably add that eventually. That only consists of checking if the parent component is larger, and if so stretch each of the cells by the proportional side difference. Smaller is possible too. Just need to add getMinimalSize and getMaximumSize.
The file I link is just a library!