Accueil > Java > JForm

JForm

28/01/2009
  1. import java.awt.BorderLayout;  
  2. import java.awt.Color;  
  3. import java.awt.Dimension;  
  4. import java.util.ArrayList;  
  5.   
  6. import javax.swing.JButton;  
  7. import javax.swing.JComponent;  
  8. import javax.swing.JLabel;  
  9. import javax.swing.JPanel;  
  10. import javax.swing.JTextField;  
  11.   
  12. /** 
  13.  * This is a swing class that handle Forms by managing lines. 
  14.  * In one line you can put a label (left), a component (auto resize - middle) and an other component (rigth) 
  15.  * @author Gabriel Dromard 
  16.  */  
  17. public class JForm extends JPanel {  
  18.     public static final long serialVersionUID = 364635478;  
  19.     private ArrayList leftComponents = new ArrayList();  
  20.     private ArrayList rightComponents = new ArrayList();  
  21.     private JPanel lastLine = null;  
  22.     private int hgap, vgap;  
  23.   
  24.     private int rightWidth;  
  25.     private int leftWidth;  
  26.   
  27.     /** 
  28.     * Initialization of the component 
  29.     * @param hgap the horizontal gap of the layout. 
  30.     * @param vgap the vertical gap of the layout. 
  31.     */  
  32.     public JForm(int hgap, int vgap) {  
  33.         super();  
  34.         this.hgap = hgap;  
  35.         this.vgap = vgap;  
  36.         this.setLayout(new BorderLayout(hgap, vgap));  
  37.         lastLine = this;  
  38.     }  
  39.   
  40.     /** 
  41.     * Centralize creation of panels 
  42.     * @return A new instance of panel (with border layout and opaque false) 
  43.     */  
  44.     public JPanel buildPanel() {  
  45.         JPanel panel = new JPanel(new BorderLayout(hgap, vgap));  
  46.         panel.setOpaque(this.isOpaque());  
  47.         panel.setBackground(this.getBackground());  
  48.         return panel;  
  49.     }  
  50.   
  51.     /** 
  52.     * Add a line to the form 
  53.     * @param label  The label (WEST) 
  54.     * @param middle A component (middle) 
  55.     * @param right  A component (EAST) 
  56.     */  
  57.     public void addLine(JLabel label, JComponent middle, JComponent right) {  
  58.         JPanel line = buildPanel();  
  59.         if(label != null) {  
  60.             leftWidth = addComponent(leftComponents, label, leftWidth);  
  61.             line.add(label, BorderLayout.WEST);  
  62.         }  
  63.         if(right != null) {  
  64.             rightWidth = addComponent(rightComponents, right, rightWidth);  
  65.             line.add(right, BorderLayout.EAST);  
  66.         }  
  67.         if(middle != null) {  
  68.             line.add(middle, BorderLayout.CENTER);  
  69.         }  
  70.         lastLine.add(line, BorderLayout.NORTH);  
  71.         JPanel nextLine = buildPanel();  
  72.         lastLine.add(nextLine, BorderLayout.CENTER);  
  73.         lastLine = nextLine;  
  74.     }  
  75.   
  76.     /** 
  77.      * This methods is used to set width of left or rigth component. All the component on left must have the same width ! 
  78.      * And it is the same for rigth ones. 
  79.      * 
  80.      * @param components The ArrayList of components. 
  81.      * @param component The component to add into the ArrayList. 
  82.      * @param maxSize The current max size of the components. 
  83.      * @return The new max size of the components 
  84.      */  
  85.     private int addComponent(ArrayList components, JComponent component, int maxSize) {  
  86.         int size = (int)component.getPreferredSize().getWidth();  
  87.   
  88.         System.out.println("size="+size);  
  89.         System.out.println("maxSize="+maxSize);  
  90.         if (size > maxSize) {  
  91.             maxSize = size;  
  92.             adaptWidth(components, maxSize);  
  93.         } else {  
  94.             component.setPreferredSize(new Dimension(maxSize, (int)component.getPreferredSize().getHeight()));  
  95.         }  
  96.   
  97.         components.add(component);  
  98.   
  99.         return maxSize;  
  100.     }  
  101.   
  102.     /** 
  103.      * This methods is used to adapt the prefered size of components of an array. 
  104.      * 
  105.      * @param components The components. 
  106.      * @param size The new prefered size. 
  107.      */  
  108.     private void adaptWidth(ArrayList components, int size) {  
  109.         JComponent cmp;  
  110.         // Set preferred size for left components  
  111.         for (int i=0; i<components.size(); i++) {  
  112.             cmp = ((JComponent)components.get(i));  
  113.             cmp.setPreferredSize(new Dimension(size, (int)cmp.getPreferredSize().getHeight()));  
  114.         }  
  115.     }  
  116.   
  117.     /** 
  118.      * This is a way of testing this class. 
  119.      * 
  120.      * @param args Not used. 
  121.      */  
  122.     public static void main(String[] args) {  
  123.         JForm form = new JForm(55);  
  124.         form.setBackground(Color.WHITE);  
  125.         form.setOpaque(true);  
  126.   
  127.         for (int i = 0; i < 10; ++i) {  
  128.             if (i == 1) {  
  129.                 form.addLine(new JLabel("JLabel n°" + i), new JTextField(), null);  
  130.             } else if (i == 2) {  
  131.                 form.addLine(nullnew JTextField(), new JButton("JButton n°" + i));  
  132.             } else if (i == 3) {  
  133.                 form.addLine(nullnew JTextField(), null);  
  134.             } else if (i == 4) {  
  135.                 form.addLine(new JLabel("JLabel n°" + i), nullnull);  
  136.             } else if (i == 5) {  
  137.                 form.addLine(nullnullnew JButton("JButton n°" + i));  
  138.             } else {  
  139.                 form.addLine(new JLabel("JLabel n°" + i), new JTextField(), new JButton("JButton n°" + i));  
  140.             }  
  141.         }  
  142.         SwingHelper.openInFrame(form);  
  143.     }  
  144. }  

Java

Les commentaires sont fermés.