Adding a row to a JTable

The trick is to actaully create an instance of a new javax.swing.table.DefaultTableModel(). You will then have access to the .addRow and .addColumn methods of the TableModel which which will chage the data displayed in your table.

The initComponents() method in the code I pasted below was generated by Forte for Java. Since it does not let you edit the initComponents() method, I just overwrote the generated TableModel with an empty one.

public class TableTest extends javax.swing.JApplet {
 
   public TableTest() {
      initComponents ();
      javax.swing.table.DefaultTableModel t=new javax.swing.table.DefaultTableModel();
      jTable1.setModel (t);
      t.addColumn ((Object)"Test");
      t.addColumn ((Object)"Foo");
      t.addColumn ((Object)"Bar");
      t.addRow(new Object[] {"1","2","3"});
      t.addRow(new Object[] {"4","5","6"});
      t.removeRow(0);
   }
   
   private void initComponents() {
      jScrollPane1 = new javax.swing.JScrollPane();
      jTable1 = new javax.swing.JTable();
      getContentPane().setLayout(null);
          
      jTable1.setModel(new javax.swing.table.DefaultTableModel (
          new Object [][] {
              {null, null, null, null},
              {null, null, null, null},
              {null, null, null, null},
              {null, null, null, null}
          },
          new String [] {
              "Title 1", "Title 2", "Title 3", "Title 4"
          }
          ) {
              Class[] types = new Class [] {
                  java.lang.Object.class,
                  java.lang.Object.class,
                  java.lang.Object.class,
                  java.lang.Object.class
              };
              
              public Class getColumnClass (int columnIndex) {
                 return types [columnIndex];
              }
          });
      jScrollPane1.setViewportView(jTable1);
           
      getContentPane().add(jScrollPane1);
      jScrollPane1.setBounds(70, 50, 290, 150);  
   }

   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTable jTable1;
}

Creating spanned headers in a JTable

Courtesy of Nobuo Tamemasa (http://www2.gol.com/users/tame/swing/examples/JTableExamples1.html)



MultiWidthHeaderExample.java:

/*
 *  (swing1.1beta3)
 * 
 * |-----------------------------------------------------|
 * |   1st  |      2nd        |          3rd             |
 * |-----------------------------------------------------|
 * |        |        |        |        |        |        |
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
 
/**
 * @version 1.0 11/09/98
 */
public class MultiWidthHeaderExample extends JFrame {
 
  MultiWidthHeaderExample() {
    super( "Multi-Width Header Example" );
 
    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(new Object[][]{
      {"a","b","c","d","e","f"},
      {"A","B","C","D","E","F"}},
    new Object[]{"1 st","","","","",""});
 
    JTable table = new JTable( dm ) {
      protected JTableHeader createDefaultTableHeader() {
        return new GroupableTableHeader(columnModel);
      }
    };
    TableColumnModel cm = table.getColumnModel();
    ColumnGroup g_2nd = new ColumnGroup("2 nd");
    g_2nd.add(cm.getColumn(1));
    g_2nd.add(cm.getColumn(2));
    ColumnGroup g_3rd = new ColumnGroup("3 rd");
    g_3rd.add(cm.getColumn(3));
    g_3rd.add(cm.getColumn(4));
    g_3rd.add(cm.getColumn(5));
    GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
    header.addColumnGroup(g_2nd);
    header.addColumnGroup(g_3rd);
    JScrollPane scroll = new JScrollPane( table );
    getContentPane().add( scroll );
    setSize( 400, 100 );  
    header.revalidate(); 
  }
 
  public static void main(String[] args) {
    MultiWidthHeaderExample frame = new MultiWidthHeaderExample();
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.setVisible(true);
  }
}

ColumnGroup.java:

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
  
/** 
  * ColumnGroup
  *
  * @version 1.0 10/20/98
  * @author Nobuo Tamemasa
  */
 
public class ColumnGroup {
  protected TableCellRenderer renderer;
  protected Vector v;
  protected String text;
  protected int margin=0;
 
  public ColumnGroup(String text) {
    this(null,text);
  }
 
  public ColumnGroup(TableCellRenderer renderer,String text) {
    if (renderer == null) {
      this.renderer = new DefaultTableCellRenderer() {
        public Component getTableCellRendererComponent(JTable table, Object value,
                         boolean isSelected, boolean hasFocus, int row, int column) {
          JTableHeader header = table.getTableHeader();
          if (header != null) {
            setForeground(header.getForeground());
            setBackground(header.getBackground());
            setFont(header.getFont());
          }
          setHorizontalAlignment(JLabel.CENTER);
          setText((value == null) ? "" : value.toString());
          setBorder(UIManager.getBorder("TableHeader.cellBorder"));
          return this;
        }
      };
    } else {
      this.renderer = renderer;
    }
    this.text = text;
    v = new Vector();
  }
 
  
  /**
   * @param obj    TableColumn or ColumnGroup
   */
  public void add(Object obj) {
    if (obj == null) { return; }
    v.addElement(obj);
  }
 
  
  /**
   * @param c    TableColumn
   * @param v    ColumnGroups
   */
  public Vector getColumnGroups(TableColumn c, Vector g) {
    g.addElement(this);
    if (v.contains(c)) return g;    
    Enumeration enum = v.elements();
    while (enum.hasMoreElements()) {
      Object obj = enum.nextElement();
      if (obj instanceof ColumnGroup) {
        Vector groups = 
          (Vector)((ColumnGroup)obj).getColumnGroups(c,(Vector)g.clone());
        if (groups != null) return groups;
      }
    }
    return null;
  }
    
  public TableCellRenderer getHeaderRenderer() {
    return renderer;
  }
    
  public void setHeaderRenderer(TableCellRenderer renderer) {
    if (renderer != null) {
      this.renderer = renderer;
    }
  }
    
  public Object getHeaderValue() {
    return text;
  }
  
  public Dimension getSize(JTable table) {
    Component comp = renderer.getTableCellRendererComponent(
        table, getHeaderValue(), false, false,-1, -1);
    int height = comp.getPreferredSize().height; 
    int width  = 0;
    Enumeration enum = v.elements();
    while (enum.hasMoreElements()) {
      Object obj = enum.nextElement();
      if (obj instanceof TableColumn) {
        TableColumn aColumn = (TableColumn)obj;
        width += aColumn.getWidth();
        width += margin;
      } else {
        width += ((ColumnGroup)obj).getSize(table).width;
      }
    }
    return new Dimension(width, height);
  }
 
  public void setColumnMargin(int margin) {
    this.margin = margin;
    Enumeration enum = v.elements();
    while (enum.hasMoreElements()) {
      Object obj = enum.nextElement();
      if (obj instanceof ColumnGroup) {
        ((ColumnGroup)obj).setColumnMargin(margin);
      }
    }
  }
}

GroupableTableHeader.java:

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
 
/**
  * GroupableTableHeader
  *
  * @version 1.0 10/20/98
  * @author Nobuo Tamemasa
  */
 
public class GroupableTableHeader extends JTableHeader {
  private static final String uiClassID = "GroupableTableHeaderUI";
  protected Vector columnGroups = null;
     
  public GroupableTableHeader(TableColumnModel model) {
    super(model);
    setUI(new GroupableTableHeaderUI());
    setReorderingAllowed(false);
  }
   
  public void setReorderingAllowed(boolean b) {
    reorderingAllowed = false;
  }
     
  public void addColumnGroup(ColumnGroup g) {
    if (columnGroups == null) {
      columnGroups = new Vector();
    }
    columnGroups.addElement(g);
  }
 
  public Enumeration getColumnGroups(TableColumn col) {
    if (columnGroups == null) return null;
    Enumeration enum = columnGroups.elements();
    while (enum.hasMoreElements()) {
      ColumnGroup cGroup = (ColumnGroup)enum.nextElement();
      Vector v_ret = (Vector)cGroup.getColumnGroups(col,new Vector());
      if (v_ret != null) { 
        return v_ret.elements();
      }
    }
    return null;
  }
   
  public void setColumnMargin() {
    if (columnGroups == null) return;
    int columnMargin = getColumnModel().getColumnMargin();
    Enumeration enum = columnGroups.elements();
    while (enum.hasMoreElements()) {
      ColumnGroup cGroup = (ColumnGroup)enum.nextElement();
      cGroup.setColumnMargin(columnMargin);
    }
  } 
}

GroupableTableHeaderUI.java:

import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.plaf.basic.*;
 
public class GroupableTableHeaderUI extends BasicTableHeaderUI {
  
  public void paint(Graphics g, JComponent c) {
    Rectangle clipBounds = g.getClipBounds();
    if (header.getColumnModel() == null) return;
    ((GroupableTableHeader)header).setColumnMargin();
    int column = 0;
    Dimension size = header.getSize();
    Rectangle cellRect  = new Rectangle(0, 0, size.width, size.height);
    Hashtable h = new Hashtable();
    int columnMargin = header.getColumnModel().getColumnMargin();
    
    Enumeration enumeration = header.getColumnModel().getColumns();
    while (enumeration.hasMoreElements()) {
      cellRect.height = size.height;
      cellRect.y      = 0;
      TableColumn aColumn = (TableColumn)enumeration.nextElement();
      Enumeration cGroups = ((GroupableTableHeader)header).getColumnGroups(aColumn);
      if (cGroups != null) {
        int groupHeight = 0;
        while (cGroups.hasMoreElements()) {
          ColumnGroup cGroup = (ColumnGroup)cGroups.nextElement();
          Rectangle groupRect = (Rectangle)h.get(cGroup);
          if (groupRect == null) {
            groupRect = new Rectangle(cellRect);
            Dimension d = cGroup.getSize(header.getTable());
            groupRect.width  = d.width;
            groupRect.height = d.height;    
            h.put(cGroup, groupRect);
          }
          paintCell(g, groupRect, cGroup);
          groupHeight += groupRect.height;
          cellRect.height = size.height - groupHeight;
          cellRect.y      = groupHeight;
        }
      }      
      cellRect.width = aColumn.getWidth() + columnMargin;
      if (cellRect.intersects(clipBounds)) {
        paintCell(g, cellRect, column);
      }
      cellRect.x += cellRect.width;
      column++;
    }
  }
 
  private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
    TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
    TableCellRenderer renderer = aColumn.getHeaderRenderer();
    Component component = renderer.getTableCellRendererComponent(
      header.getTable(), aColumn.getHeaderValue(),false, false, -1, columnIndex);
    rendererPane.add(component);
    rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y,
                                cellRect.width, cellRect.height, true);
  }
 
  private void paintCell(Graphics g, Rectangle cellRect,ColumnGroup cGroup) {
    TableCellRenderer renderer = cGroup.getHeaderRenderer();
    Component component = renderer.getTableCellRendererComponent(
      header.getTable(), cGroup.getHeaderValue(),false, false, -1, -1);
    rendererPane.add(component);
    rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y,
                                cellRect.width, cellRect.height, true);
  }
 
  private int getHeaderHeight() {
    int height = 0;
    TableColumnModel columnModel = header.getColumnModel();
    for(int column = 0; column < columnModel.getColumnCount(); column++) {
      TableColumn aColumn = columnModel.getColumn(column);
      TableCellRenderer renderer = aColumn.getHeaderRenderer();
      Component comp = renderer.getTableCellRendererComponent(
        header.getTable(), aColumn.getHeaderValue(), false, false,-1, column);
      int cHeight = comp.getPreferredSize().height;
      Enumeration enum = ((GroupableTableHeader)header).getColumnGroups(aColumn);      
      if (enum != null) {
        while (enum.hasMoreElements()) {
          ColumnGroup cGroup = (ColumnGroup)enum.nextElement();
          cHeight += cGroup.getSize(header.getTable()).height;
        }
      }
      height = Math.max(height, cHeight);
    }
    return height;
  }
 
  private Dimension createHeaderSize(long width) {
    TableColumnModel columnModel = header.getColumnModel();
    width += columnModel.getColumnMargin() * columnModel.getColumnCount();
    if (width > Integer.MAX_VALUE) {
      width = Integer.MAX_VALUE;
    }
    return new Dimension((int)width, getHeaderHeight());
  }
 
  public Dimension getPreferredSize(JComponent c) {
    long width = 0;
    Enumeration enumeration = header.getColumnModel().getColumns();
    while (enumeration.hasMoreElements()) {
      TableColumn aColumn = (TableColumn)enumeration.nextElement();
      width = width + aColumn.getPreferredWidth();
    }
    return createHeaderSize(width);
  }
}

Displaying a popup-menu when right-clicking on a JTable header

Main.java:

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("TableModel Demonstration");
 
      // create our own custom TableModel
      WineTableModel wineModel = new WineTableModel();
      final JTable table = new JTable(wineModel);
 
      // add rows to our TableModel, each row is represented as a Wine object
      wineModel.addWine(new Wine("Chateau Meyney, St. Estephe", "1994", 18.75f, true));
      wineModel.addWine(new Wine("Chateau Montrose, St. Estephe", "1975", 54.25f, true));
      wineModel.addWine(new Wine("Chateau Gloria, St. Julien", "1993", 22.99f, false));
      wineModel.addWine(new Wine("Chateau Beychevelle, St. Julien", "1970", 61.63f, false));
      wineModel.addWine(new Wine("Chateau La Tour de Mons, Margeaux", "1975", 57.03f, true));
      wineModel.addWine(new Wine("Chateau Brane-Cantenac, Margeaux", "1978", 49.92f, false));

      final JPopupMenu popupMenu = new JPopupMenu();
      popupMenu.add(new JMenuItem("PopupItem 1"));
      popupMenu.add(new JMenuItem("PopupItem 2"));
      popupMenu.add(new JPopupMenu.Separator());
      popupMenu.add(new JMenuItem("PopupItem 3"));

      final JTableHeader header = table.getTableHeader();
      header.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent me) {
            if (SwingUtilities.isRightMouseButton(me))
               popupMenu.show(header, me.getX(), me.getY());
         }
      });
 
      // create the scroll pane and add the table to it. 
      JScrollPane scrollPane = new JScrollPane(table);
 
      // add the scroll pane to this window.
      getContentPane().add(scrollPane, BorderLayout.CENTER);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}
 
// a simple object that holds data about a particular wine
class Wine {
   private String  name;
   private String  vintage;
   private float   price;
   private boolean inStock;
 
   public Wine(String name, String vintage, float price, boolean inStock) {
      this.name = name;
      this.vintage = vintage;
      this.price = price;
      this.inStock = inStock;
   }
 
   public String getName()     { return name; }
   public String getVintage()  { return vintage; }
   public float  getPrice()    { return price; } 
   public boolean getInStock() { return inStock; }
 
   public String toString() { 
      return "[" + name + ", " + vintage + ", " + price + ", " + inStock + "]"; }
}
 
class WineTableModel extends AbstractTableModel {
   // holds the strings to be displayed in the column headers of our table
   final String[] columnNames = {"Name", "Vintage", "Price", "In stock?"};
 
   // holds the data types for all our columns
   final Class[] columnClasses = {String.class, String.class, Float.class, Boolean.class};
 
   // holds our data
   final Vector data = new Vector();
  
   // adds a row
   public void addWine(Wine w) {
      data.addElement(w);
      fireTableRowsInserted(data.size()-1, data.size()-1);
   }
 
   public int getColumnCount() {
      return columnNames.length;
   }
         
   public int getRowCount() {
      return data.size();
   }
 
   public String getColumnName(int col) {
      return columnNames[col];
   }
 
   public Class getColumnClass(int c) {
      return columnClasses1;
   }
 
   public Object getValueAt(int row, int col) {
      Wine wine = (Wine) data.elementAt(row);
      if (col == 0)      return wine.getName();
      else if (col == 1) return wine.getVintage();
      else if (col == 2) return new Float(wine.getPrice());
      else if (col == 3) return new Boolean(wine.getInStock());
      else return null;
   }
 
   public boolean isCellEditable(int row, int col) {
      return false;
   }
} 

Line numbers in a JTextArea

Here are two examples. The second was born from the first.

First Example: an extension to a component…

The first example LineNumberedPaper is a class that extends JTextArea and draws the line numbers in the paintComponent()
method. LineNumberedPaper artificially increases the size of the border by overriding the getInsets() methods and adding room for the “line numbers” (using the method lineNumberWidth()) to the left attribute of Insets.

paintComponent(), contains the logic for determining which line
numbers must be drawn based upon the the clip rectangle of the Graphics.

LineNumeredPaper can be modified to display the line numbers on the right side of the paper, this is left as an exercise for the reader. After, this idea was completed, the “border” increase forced the writing of the second example, LineNumberedBorder.

Second Example: a new border…

LineNumberedBorder is a border which draws a left (LEFT_JUSTIFY) or right (RIGHT_JUSTIFY) justified line number on either the left (LEFT_SIDE) or right side (RIGHT_SIDE) of the component. Currently, this only supports a JTextArea, due to the reliance on the getRows() and getLineCount() methods. paintBorder() contains the drawing of the line numbers and is very similar to LineNumberedPaper.paintComponent().

Summary

  • Each class has a main() which creates all possibilities for each class and can be easily removed.
  • The font of the line numbers is the same as the text.
    How would two different fonts play together? Which class/interface in javax.swing.text or subpackage should be used or extended?
  • paintComponent() and paintBorder() are very similar and speed is important, since each blink of the cursor causes a repaint.

LineNumberedPaper.java:

import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
 
import javax.swing.BorderFactory;
import javax.swing.JTextArea;
 
/**
 * Draws line numbers next to each line, in the same font as the text.
 * Based upon the comment in {@link #getInsets(Insets) getInsets} maybe the
 * "line numbering" could be a border?
 */
public class LineNumberedPaper extends JTextArea {
 
   public static void main(String[] args)
   {
      javax.swing.JFrame frame = new javax.swing.JFrame("Line Numbers...");
      frame.addWindowListener(new java.awt.event.WindowAdapter() {
         public void windowClosing(java.awt.event.WindowEvent ev) {
            System.exit(0);
         }
      });
 
      java.awt.Container contentPane = frame.getContentPane();
      contentPane.setLayout(new java.awt.GridLayout(0,2));
 
      javax.swing.JPanel subpanel;
      LineNumberedPaper lnp;
 
      int []justified = { LineNumberedBorder.LEFT_JUSTIFY,
                          LineNumberedBorder.RIGHT_JUSTIFY};
 
      String []labels = { "Left Justified",
                          "Right Justified"};
 
      for (int idx = 0; idx < labels.length; idx++) {
         lnp = new LineNumberedPaper(10,10);
         lnp.setLineNumberJustification(justified[idx]);
 
         subpanel = new javax.swing.JPanel(new java.awt.BorderLayout());
         subpanel.add(new javax.swing.JLabel(labels[idx]), java.awt.BorderLayout.NORTH);
         subpanel.add(new javax.swing.JScrollPane(lnp), java.awt.BorderLayout.CENTER);
         contentPane.add(subpanel);
      }
 
      frame.setSize(800,600);
      frame.show();
   } // main
 
   /**
    * The line number should be right justified.
    */
   public static int RIGHT_JUSTIFY = 0;
 
   /**
    * The line number should be left justified.
    */
   public static int LEFT_JUSTIFY = 1;
 
   /**
    * Indicates the justification of the text of the line number.
    */
   private int lineNumberJustification = RIGHT_JUSTIFY;
 
   public LineNumberedPaper(int rows, int cols) {
      super(rows, cols);
      setOpaque(false);
      // if this is NOT opaque...then painting is a problem...
      // basically...this draws the line numbers...
      // but...super.paintComponent()...erases the background...and the
      // line numbers...what to do?
      //
      // "workaround": paint the background in this class...
   }
 
   public Insets getInsets() {
      return getInsets(new Insets(0,0,0,0));
   }
 
   /**
    * This modifies the insets, by adding space for the line number on the
    * left. Should be modified to add space on the right, depending upon
    * Locale.
    */
   public Insets getInsets(Insets insets) {
      insets = super.getInsets(insets);
      insets.left += lineNumberWidth();
      return insets;
   }
 
   public int getLineNumberJustification() {
      return lineNumberJustification;
   }
 
   public void setLineNumberJustification(int justify) {
      if (justify == RIGHT_JUSTIFY || justify == LEFT_JUSTIFY) {
         lineNumberJustification = justify;
      }
   }
 
   /**
    * Returns the width, in pixels, of the maximum line number, plus a
    * trailing space.
    */
   private int lineNumberWidth() {
      //
      // note: should this be changed to use all nines for the lineCount?
      // for example, if the number of rows is 111...999 could be wider
      // (in pixels) in a proportionally spaced font...
      //
      int lineCount = Math.max(getRows(), getLineCount() + 1);
      return getFontMetrics(getFont()).stringWidth(lineCount + " ");
   }
 
   //
   // NOTE: This method is called every time the cursor blinks...
   //       so...optimize (later and if possible) for speed...
   //
   public void paintComponent(Graphics g) {
      Insets insets = getInsets();
 
      Rectangle clip = g.getClipBounds();
 
      g.setColor(getBackground()); // see note in constructor about this...
      g.fillRect(clip.x, clip.y, clip.width, clip.height);
 
      // do the line numbers need redrawn?
      if (clip.x < insets.left) {
         FontMetrics fm = g.getFontMetrics();
         int fontHeight = fm.getHeight();
 
         // starting location at the "top" of the page...
         // y is the starting baseline for the font...
         // should "font leading" be applied?
         int y = fm.getAscent() + insets.top;
 
         //
         // now determine if it is the "top" of the page...or somewhere else
         //
         int startingLineNumber = ((clip.y + insets.top) / fontHeight) + 1;
 
         //
         // use any one of the following if's:
         //
         //			if (startingLineNumber != 1)
         if (y < clip.y) {
            //
            // not within the clip rectangle...move it...
            // determine how many fontHeight's there are between
            // y and clip.y...then add that many fontHeights
            //
 
            y = startingLineNumber * fontHeight - (fontHeight - fm.getAscent());
         }
 
         //
         // options:
         // . write the number rows in the document (current)
         // . write the number of existing lines in the document (to do)
         //   see getLineCount()
         //
         // determine which the "drawing" should end...
         // add fontHeight: make sure...part of the line number is drawn
         //
         // could also do this by determining what the last line
         // number to draw.
         // then the "while" loop whould change accordingly.
         //
         int	yend = y + clip.height + fontHeight;
 
         // base x position of the line number
         int lnxstart = insets.left;
         if (lineNumberJustification == LEFT_JUSTIFY) {
            // actual starting location of the string of a left
            // justified string...it's constant...
            // the right justified string "moves"...
            lnxstart -= lineNumberWidth();
         }
 
         g.setColor(getForeground());
         //
         // loop until out of the "visible" region...
         //
         int length = ("" + Math.max(getRows(), getLineCount() + 1)).length();
         while (y < yend) {
            //
            // options:
            // . left justify the line numbers (current)
            // . right justify the line number (to do)
            //
 
            if (lineNumberJustification == LEFT_JUSTIFY) {
               g.drawString(startingLineNumber + " ", lnxstart, y);
            } else { // right justify
               String label = padLabel(startingLineNumber, length, true);
               g.drawString(label, insets.left - fm.stringWidth(label), y);
            }
 
            y += fontHeight;
            startingLineNumber++;
         }
      } // draw line numbers?
 
      super.paintComponent(g);
   } // paintComponent
 
   /**
    * Create the string for the line number.
    * NOTE: The <tt>length</tt> param does not include the
    * <em>optional</em> space added after the line number.
    *
    * @param lineNumber to stringize
    * @param length     the length desired of the string
    * @param length     the length desired of the string
    *
    * @return the line number for drawing
    */
   private String padLabel(int lineNumber, int length, boolean addSpace) {
      StringBuffer buffer = new StringBuffer();
      buffer.append(lineNumber);
      for (int count = (length - buffer.length()); count > 0; count--) {
         buffer.insert(0, ' ');
      }
      if (addSpace) {
         buffer.append(' ');
      }
      return buffer.toString();
   }
} // LineNumberedPaper

LineNumberedBorder.java:

import java.awt.Component;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Insets;
 
import javax.swing.JTextArea;
import javax.swing.border.AbstractBorder;
 
/**
 *  Draws line numbers next to each line, in the same font as the text.
 *  Currently, this can only be used with a <tt>JTextArea</tt> , since it relies
 *  on the <tt>getRows()</tt> and <tt>getLineCount()</tt> methods. A possible
 *  extension, create an interface to return this rows/linecount.
 *
 *@author     Administrator
 *@created    January 29, 2002
 */
public class LineNumberedBorder extends AbstractBorder {
   public static void main(String[] args) {
      javax.swing.JFrame frame = new javax.swing.JFrame("Line Numbers (as Borders)...");
      frame.addWindowListener(
         new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent ev) {
               System.exit(0);
            }
         });
 
      java.awt.Container contentPane = frame.getContentPane();
      contentPane.setLayout(new java.awt.GridLayout(0, 2));
 
      int[] sides = {
            LineNumberedBorder.LEFT_SIDE,
            LineNumberedBorder.LEFT_SIDE,
            LineNumberedBorder.RIGHT_SIDE,
            LineNumberedBorder.RIGHT_SIDE};
 
      int[] justified = {
            LineNumberedBorder.LEFT_JUSTIFY,
            LineNumberedBorder.RIGHT_JUSTIFY,
            LineNumberedBorder.LEFT_JUSTIFY,
            LineNumberedBorder.RIGHT_JUSTIFY};
 
      String[] labels = {
            "Left Side/Left Justified",
            "Left Side/Right Justified",
            "Right Side/Left Justified",
            "Right Side/Right Justified"};
 
      javax.swing.JPanel subpanel;
      JTextArea textArea;
 
      boolean useMultipleBorders = false;
      if (args.length > 0 && "multiple".equals(args[0])) {
         useMultipleBorders = true;
      }
 
      for (int idx = 0; idx < labels.length; idx++) {
         textArea = new JTextArea(10, 10);
         LineNumberedBorder lnb = new LineNumberedBorder(sides[idx], justified[idx]);
         if (useMultipleBorders) {
            textArea.setBorder(
                  javax.swing.BorderFactory.createCompoundBorder(
                  javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5),
                  javax.swing.BorderFactory.createCompoundBorder(
                  javax.swing.BorderFactory.createLineBorder(java.awt.Color.red, 1),
                  javax.swing.BorderFactory.createCompoundBorder(
                  lnb,
                  javax.swing.BorderFactory.createLineBorder(java.awt.Color.blue, 1)
                  )
                  )
                  )
                  );
         } else {
            textArea.setBorder(lnb);
         }
 
         subpanel = new javax.swing.JPanel(new java.awt.BorderLayout());
         subpanel.add(new javax.swing.JLabel(labels[idx]), java.awt.BorderLayout.NORTH);
         subpanel.add(new javax.swing.JScrollPane(textArea), java.awt.BorderLayout.CENTER);
         contentPane.add(subpanel);
      }
 
      frame.setSize(800, 600);
      frame.show();
   }
   // main
 
   /**
    *  The line numbers should be drawn on the left side of the component.
    */
   public static int LEFT_SIDE = -2;
 
   /**
    *  The line numbers should be drawn on the right side of the component.
    */
   public static int RIGHT_SIDE = -1;
 
   /**
    *  The line number should be right justified.
    */
   public static int RIGHT_JUSTIFY = 0;
 
   /**
    *  The line number should be left justified.
    */
   public static int LEFT_JUSTIFY = 1;
 
   /**
    *  Indicates the justification of the text of the line number.
    */
   private int lineNumberJustification = RIGHT_JUSTIFY;
 
   /**
    *  Indicates the location of the line numbers, w.r.t. the component.
    */
   private int location = LEFT_SIDE;
 
   public LineNumberedBorder(int location, int justify) {
      setLocation(location);
      setLineNumberJustification(justify);
   }
 
   public Insets getBorderInsets(Component c) {
      return getBorderInsets(c, new Insets(0, 0, 0, 0));
   }
 
   /**
    *  This modifies the insets, by adding space for the line number on the
    *  left. Should be modified to add space on the right, depending upon
    *  Locale.
    *
    *@param  c       Description of the Parameter
    *@param  insets  Description of the Parameter
    *@return         The borderInsets value
    */
   public Insets getBorderInsets(Component c, Insets insets) {
      // if c is not a JTextArea...nothing is done...
      if (c instanceof JTextArea) {
         int width = lineNumberWidth((JTextArea) c);
         if (location == LEFT_SIDE) {
            insets.left = width;
         } else {
            insets.right = width;
         }
      }
      return insets;
   }
 
   public int getLineNumberJustification() {
      return lineNumberJustification;
   }
 
   public void setLineNumberJustification(int justify) {
      if (justify == RIGHT_JUSTIFY || justify == LEFT_JUSTIFY) {
         lineNumberJustification = justify;
      }
   }
 
   public int getLocation() {
      return location;
   }
 
   public void setLocation(int loc) {
      if (loc == RIGHT_SIDE || loc == LEFT_SIDE) {
         location = loc;
      }
   }
 
   /**
    *  Returns the width, in pixels, of the maximum line number, plus a trailing
    *  space.
    *
    *@param  textArea  Description of the Parameter
    *@return           Description of the Return Value
    */
   private int lineNumberWidth(JTextArea textArea) {
      //
      // note: should this be changed to use all nines for the lineCount?
      // for example, if the number of rows is 111...999 could be wider
      // (in pixels) in a proportionally spaced font...
      //
      int lineCount =
            Math.max(textArea.getRows(), textArea.getLineCount() + 1);
      return textArea.getFontMetrics(
            textArea.getFont()).stringWidth(lineCount + " ");
   }
 
   //
   // NOTE: This method is called every time the cursor blinks...
   //       so...optimize (later and if possible) for speed...
   //
   public void paintBorder(Component c, Graphics g, int x, int y,
         int width, int height) {
 
      java.awt.Rectangle clip = g.getClipBounds();
 
      FontMetrics fm = g.getFontMetrics();
      int fontHeight = fm.getHeight();
 
      // starting location at the "top" of the page...
      // y is the starting baseline for the font...
      // should "font leading" be applied?
      int ybaseline = y + fm.getAscent();
 
      //
      // now determine if it is the "top" of the page...or somewhere else
      //
      int startingLineNumber = (clip.y / fontHeight) + 1;
 
      //
      // use any one of the following if's:
      //
//		if (startingLineNumber != 1)
      if (ybaseline < clip.y) {
         //
         // not within the clip rectangle...move it...
         // determine how many fontHeight's there are between
         // y and clip.y...then add that many fontHeights
         //
         ybaseline = y + startingLineNumber * fontHeight -
               (fontHeight - fm.getAscent());
      }
 
      //
      // options:
      // . write the number rows in the document (current)
      // . write the number of existing lines in the document (to do)
      //   see getLineCount()
      //
 
      // determine which the "drawing" should end...
      // add fontHeight: make sure...part of the line number is drawn
      //
      // could also do this by determining what the last line
      // number to draw.
      // then the "while" loop whould change accordingly.
      //
      //int	yend = y + clip.height + fontHeight;
      //int	yend = ybaseline + height + fontHeight; // original
      int yend = ybaseline + height;
      if (yend > (y + height)) {
         yend = y + height;
      }
 
      JTextArea jta = (JTextArea) c;
      int lineWidth = lineNumberWidth(jta);
 
      // base x position of the line number
      int lnxstart = x;
      if (location == LEFT_SIDE) {
         // x (LEFT) or (x + lineWidth) (RIGHT)
         // (depends upon justification)
         if (lineNumberJustification == LEFT_JUSTIFY) {
            lnxstart = x;
         } else {
            // RIGHT JUSTIFY
            lnxstart = x + lineWidth;
         }
      } else {
         // RIGHT SIDE
         // (y + width) - lineWidth (LEFT) or (y + width) (RIGHT)
         // (depends upon justification)
         if (lineNumberJustification == LEFT_JUSTIFY) {
            lnxstart = (y + width) - lineWidth;
         } else {
            // RIGHT JUSTIFY
            lnxstart = (y + width);
         }
      }
 
      g.setColor(c.getForeground());
      //
      // loop until out of the "visible" region...
      //
      int length = ("" + Math.max(jta.getRows(), jta.getLineCount() + 1)).length();
      while (ybaseline < yend) {
         //
         // options:
         // . left justify the line numbers
         // . right justify the line numbers
         //
 
         if (lineNumberJustification == LEFT_JUSTIFY) {
            g.drawString(startingLineNumber + " ", lnxstart, ybaseline);
         } else {
            // right justify
            String label = padLabel(startingLineNumber, length, true);
            g.drawString(label, lnxstart - fm.stringWidth(label), ybaseline);
         }
 
         ybaseline += fontHeight;
         startingLineNumber++;
      }
   }
   // paintComponent
 
   /**
    *  Create the string for the line number. NOTE: The <tt>length</tt> param
    *  does not include the <em>optional</em> space added after the line number.
    *
    *@param  lineNumber  to stringize
    *@param  length      the length desired of the string
    *@param  addSpace    Description of the Parameter
    *@return             the line number for drawing
    */
   private static String padLabel(int lineNumber, int length, boolean addSpace) {
      StringBuffer buffer = new StringBuffer();
      buffer.append(lineNumber);
      for (int count = (length - buffer.length()); count > 0; count--) {
         buffer.insert(0, ' ');
      }
      if (addSpace) {
         buffer.append(' ');
      }
      return buffer.toString();
   }
}
// LineNumberedBorder

Preventing a JTree to be expanded or collapsed

Use the TreeWillExpandListener interface!

The following example allows expanding of nodes, but prevents collapsing.

Main.java:

import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
 
public class Main extends JFrame
{
   public Main() {
      DefaultMutableTreeNode root = createNodes();
      JTree tree = new JTree(root);
 
      tree.addTreeWillExpandListener(new TreeWillExpandListener() {
         public void treeWillCollapse(TreeExpansionEvent event) 
                            throws ExpandVetoException 
         {
            // prevent collapsing
            throw new ExpandVetoException(event);
 
         }
         public void treeWillExpand(TreeExpansionEvent event) 
                            throws ExpandVetoException 
         {
            // do nothing
         }
      });
 
      getContentPane().add(new JScrollPane(tree));
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   }
 
   public static DefaultMutableTreeNode createNodes() {
      DefaultMutableTreeNode root = new DefaultMutableTreeNode("Java");
      
      DefaultMutableTreeNode j2se = new DefaultMutableTreeNode("J2SE");
      DefaultMutableTreeNode j2ee = new DefaultMutableTreeNode("J2EE");
      DefaultMutableTreeNode j2me = new DefaultMutableTreeNode("J2ME");
 
      j2se.add(new DefaultMutableTreeNode("http://java.sun.com/j2se/"));
      j2ee.add(new DefaultMutableTreeNode("http://java.sun.com/j2ee/"));
      j2me.add(new DefaultMutableTreeNode("http://java.sun.com/j2me/"));
 
      root.add(j2se);
      root.add(j2ee);
      root.add(j2me);
 
      return root;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(400, 400);
      main.setVisible(true);
   }
}

Programmatically selecting a JTable column

columnSelectionAllowed must be true and rowSelectionAllowed must be false.

Main.java:

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("TableModel Demonstration");
 
      // create our own custom TableModel
      WineTableModel wineModel = new WineTableModel();
      JTable table = new JTable(wineModel);
 
      // add rows to our TableModel, each row is represented as a Wine object
      wineModel.addWine(new Wine("Chateau Meyney, St. Estephe", "1994", 18.75f, true));
      wineModel.addWine(new Wine("Chateau Montrose, St. Estephe", "1975", 54.25f, true));
      wineModel.addWine(new Wine("Chateau Gloria, St. Julien", "1993", 22.99f, false));
      wineModel.addWine(new Wine("Chateau Beychevelle, St. Julien", "1970", 61.63f, false));
      wineModel.addWine(new Wine("Chateau La Tour de Mons, Margeaux", "1975", 57.03f, true));
      wineModel.addWine(new Wine("Chateau Brane-Cantenac, Margeaux", "1978", 49.92f, false));
 
      // create the scroll pane and add the table to it. 
      JScrollPane scrollPane = new JScrollPane(table);
 
      // add the scroll pane to this window.
      getContentPane().add(scrollPane, BorderLayout.CENTER);
 
      table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
      table.setRowSelectionAllowed(false);
      table.setColumnSelectionAllowed(true);
 
      table.setColumnSelectionInterval(0, 0);
      table.addColumnSelectionInterval(2, 2);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            System.exit(0);
         }
      });
   }
 
   public static void main(String[] args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}
 
// a simple object that holds data about a particular wine
class Wine {
   private String  name;
   private String  vintage;
   private float   price;
   private boolean inStock;
 
   public Wine(String name, String vintage, float price, boolean inStock) {
      this.name = name;
      this.vintage = vintage;
      this.price = price;
      this.inStock = inStock;
   }
 
   public String getName()     { return name; }
   public String getVintage()  { return vintage; }
   public float  getPrice()    { return price; } 
   public boolean getInStock() { return inStock; }
 
   public String toString() { 
      return "[" + name + ", " + vintage + ", " + price + ", " + inStock + "]"; }
}
 
class WineTableModel extends AbstractTableModel {
   // holds the strings to be displayed in the column headers of our table
   final String[] columnNames = {"Name", "Vintage", "Price", "In stock?"};
 
   // holds the data types for all our columns
   final Class[] columnClasses = {String.class, String.class, Float.class, Boolean.class};
 
   // holds our data
   final Vector data = new Vector();
  
   // adds a row
   public void addWine(Wine w) {
      data.addElement(w);
      fireTableRowsInserted(data.size()-1, data.size()-1);
   }
 
   public int getColumnCount() {
      return columnNames.length;
   }
         
   public int getRowCount() {
      return data.size();
   }
 
   public String getColumnName(int col) {
      return columnNames[col];
   }
 
   public Class getColumnClass(int c) {
      return columnClasses1;
   }
 
   public Object getValueAt(int row, int col) {
      Wine wine = (Wine) data.elementAt(row);
      if (col == 0)      return wine.getName();
      else if (col == 1) return wine.getVintage();
      else if (col == 2) return new Float(wine.getPrice());
      else if (col == 3) return new Boolean(wine.getInStock());
      else return null;
   }
 
   public boolean isCellEditable(int row, int col) {
      return false;
   }
}

Putting a component in a TitledBorder?

Courtesy of Nobuo Tamemasa (http://www2.gol.com/users/tame/swing/examples/JTableExamples5.html)

CompTitledPaneExample1.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
 
/**
 * @version 1.0 08/12/99
 */  
public class CompTitledPaneExample1 extends JFrame {
 
  String title = "<html>Title (" 
               + "<font color="#ffffff"><i>JLabel</i></font>"
               + ")";
 
  public CompTitledPaneExample1() {
    super("CompTitledPaneExample1");
    JLabel titleLabel = new JLabel(title);
    CompTitledPane p1 = new CompTitledPane(titleLabel);
    JPanel p = p1.getContentPane();
    p.setLayout(new BorderLayout());
    p.add(new SwitchPanel(p1), BorderLayout.CENTER);
    getContentPane().add(p1, BorderLayout.CENTER);
  }
  
  class SwitchPanel extends JPanel implements ActionListener {
    String[] posStr = {"", "ABOVE_TOP"   , "TOP"   , "BELOW_TOP"
                         , "ABOVE_BOTTOM", "BOTTOM", "BELOW_BOTTOM"};
    String[] jusStr = {"", "LEFT"        , "CENTER", "RIGHT"};
    TitledBorder border;
    CompTitledPane panel;
    
    SwitchPanel(CompTitledPane panel) {
      this.panel = panel;
      this.border = (TitledBorder)panel.getBorder();
      add(createPanel("Position"     ,posStr, 2));
      add(createPanel("Justification",jusStr, 1));
    }
    
    JPanel createPanel(String str, String[] strs, int selectPos) {
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(strs.length, 1));
      p.add(new JLabel(str));
      ButtonGroup g = new ButtonGroup();
      for (int i=1;i<strs.length;i++) {
        JRadioButton b = new JRadioButton(strs[i]);
        if (i == selectPos) {
          b.setSelected(true);
        }
        p.add( b );
        g.add( b );
        b.addActionListener(this);
      }
      return p;
    }
    
    public void actionPerformed(ActionEvent e) {
      JRadioButton b = (JRadioButton)e.getSource();
      String label = b.getText();
      for (int i=1; i<posStr.length; i++) {
        if (label.equals(posStr[i])) {
          border.setTitlePosition(i);
          panel.revalidate();
          panel.repaint();
          return;
        }    
      }
      for (int i=1; i<jusStr.length; i++) {
        if (label.equals(jusStr[i])) {
          border.setTitleJustification(i);
          panel.revalidate();
          panel.repaint();
          return;
        }    
      }
    }   
  }
 
  public static void main (String args[]) {
    CompTitledPaneExample1 frame = new CompTitledPaneExample1();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
	System.exit(0);
      }
    });
    frame.setSize(280, 230);
    frame.setVisible(true);
  }
}

CompTitledPane.java:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
    
/**
 * @version 1.0 08/12/99
 */
public class CompTitledPane extends JPanel {
  protected CompTitledBorder border;
  protected JComponent component;
  protected JPanel panel;
  protected boolean transmittingAllowed;
  protected StateTransmitter transmitter;
  
  public CompTitledPane() {
    this(new JLabel("Title"));
    // debug
    // JLabel label = (JLabel)getTitleComponent();
    // label.setOpaque(true);
    // label.setBackground(Color.yellow);
  }
  
  public CompTitledPane(JComponent component) {
    this.component = component;
    border = new CompTitledBorder(component);
    setBorder(border);
    panel = new JPanel();
    setLayout(null);
    add(component);
    add(panel); 
    transmittingAllowed = false;
    transmitter = null;
  }
  
  public JComponent getTitleComponent() {
    return component;
  }
  
  public void setTitleComponent(JComponent newComponent) {
    remove(component);
    add(newComponent);
    border.setTitleComponent(newComponent);
    component = newComponent;
  }
  
  public JPanel getContentPane() {
    return panel;
  }
  
  public void doLayout() {
    Insets insets = getInsets();
    Rectangle rect = getBounds();
    rect.x = 0;
    rect.y = 0;
 
    Rectangle compR = border.getComponentRect(rect,insets);
    component.setBounds(compR);
    rect.x += insets.left;
    rect.y += insets.top;
    rect.width  -= insets.left + insets.right;
    rect.height -= insets.top  + insets.bottom;
    panel.setBounds(rect);   
  }
   
  public void setTransmittingAllowed(boolean enable) {
    transmittingAllowed = enable;
  }
  
  public boolean getTransmittingAllowed() {
    return transmittingAllowed;
  }
  
  public void setTransmitter(StateTransmitter transmitter) {
    this.transmitter = transmitter;
  }
  
  public StateTransmitter getTransmitter() {
    return transmitter;
  }
  
  public void setEnabled(boolean enable) {
    super.setEnabled(enable);
    if (transmittingAllowed && transmitter != null) {
      transmitter.setChildrenEnabled(enable);
    }
  } 
    
}

StateTransmitter.java:

/**
 * @version 1.0 08/12/99
 */
public interface StateTransmitter {

  public void setChildrenEnabled(boolean enable);
  
}

// sample
//
//  public void setChildrenEnabled(boolean enable) {
//    
//    Component[] children = panel.getComponents();
//    for(int i=0; i<children.length; i++) { 
//      System.out.println(" " + i + 
//                         " " + children[i].getClass().getName() +
//                         " " + enable);
//      children[i].setEnabled(enable); 
//    }
//  }
//

CompTitledBorder.java:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
  
/**
 * @version 1.0 08/12/99
 */
public class CompTitledBorder extends TitledBorder { 
  protected JComponent component;
  
  public CompTitledBorder(JComponent component) {
    this(null, component, LEFT, TOP);
  }
  
  public CompTitledBorder(Border border) {
    this(border, null, LEFT, TOP);
  }
  
  public CompTitledBorder(Border border, JComponent component) {
    this(border, component, LEFT, TOP);
  }
    
  public CompTitledBorder(Border     border,
                          JComponent component,
                          int        titleJustification,
                          int        titlePosition)      {
    super(border, null, titleJustification,
                        titlePosition, null, null);
    this.component = component;
    if (border == null) {
      this.border = super.getBorder();
    }
  }
  
  
  public void paintBorder(Component c, Graphics g,
                          int x, int y, int width, int height) {
    Rectangle borderR = new Rectangle(x      +  EDGE_SPACING,
                                      y      +  EDGE_SPACING,
                                      width  - (EDGE_SPACING * 2),
                                      height - (EDGE_SPACING * 2));
    Insets borderInsets;
    if (border != null) {
      borderInsets = border.getBorderInsets(c);
    } else {
      borderInsets = new Insets(0, 0, 0, 0);
    }
    
    Rectangle rect = new Rectangle(x,y,width,height);
    Insets insets = getBorderInsets(c);
    Rectangle compR = getComponentRect(rect, insets);
    int diff;
    switch (titlePosition) {
      case ABOVE_TOP:
        diff = compR.height + TEXT_SPACING;
        borderR.y += diff;
        borderR.height -= diff;
        break;
      case TOP:
      case DEFAULT_POSITION:
        diff = insets.top/2 - borderInsets.top - EDGE_SPACING;
        borderR.y += diff;
        borderR.height -= diff;
        break;
      case BELOW_TOP:
      case ABOVE_BOTTOM:
        break;
      case BOTTOM:
        diff = insets.bottom/2 - borderInsets.bottom - EDGE_SPACING;
        borderR.height -= diff;
        break;
      case BELOW_BOTTOM:
        diff = compR.height + TEXT_SPACING;
        borderR.height -= diff;
        break;
    }
    border.paintBorder(c, g, borderR.x,     borderR.y, 
                             borderR.width, borderR.height);    
    Color col = g.getColor();
    g.setColor(c.getBackground());
    g.fillRect(compR.x, compR.y, compR.width, compR.height);
    g.setColor(col);
    component.repaint();
  }
   
  public Insets getBorderInsets(Component c, Insets insets) {
    Insets borderInsets;
    if (border != null) {
      borderInsets  = border.getBorderInsets(c);
    } else {
      borderInsets  = new Insets(0,0,0,0);
    }
    insets.top    = EDGE_SPACING + TEXT_SPACING + borderInsets.top;
    insets.right  = EDGE_SPACING + TEXT_SPACING + borderInsets.right;
    insets.bottom = EDGE_SPACING + TEXT_SPACING + borderInsets.bottom;
    insets.left   = EDGE_SPACING + TEXT_SPACING + borderInsets.left;
 
    if (c == null || component == null) {
      return insets;
    }
 
    int compHeight = 0;
    if (component != null) {
      compHeight = component.getPreferredSize().height;
    }
 
    switch (titlePosition) {
      case ABOVE_TOP:
        insets.top    += compHeight + TEXT_SPACING;
        break;
      case TOP:
      case DEFAULT_POSITION:
        insets.top    += Math.max(compHeight,borderInsets.top) - borderInsets.top;
        break;
      case BELOW_TOP:
        insets.top    += compHeight + TEXT_SPACING;
        break;
      case ABOVE_BOTTOM:
        insets.bottom += compHeight + TEXT_SPACING;
        break;
      case BOTTOM:
        insets.bottom += Math.max(compHeight,borderInsets.bottom) - borderInsets.bottom;
        break;
      case BELOW_BOTTOM:
        insets.bottom += compHeight + TEXT_SPACING;
        break;
    }
    return insets;
  }
  
  public JComponent getTitleComponent() {
    return component;
  }
  
  public void setTitleComponent(JComponent component) {
    this.component = component;
  }
  
  
  public Rectangle getComponentRect(Rectangle rect,Insets borderInsets) {
    Dimension compD = component.getPreferredSize();
    Rectangle compR = new Rectangle(0,0,compD.width,compD.height);
    switch (titlePosition) {
      case ABOVE_TOP:
        compR.y = EDGE_SPACING;
        break;
      case TOP:
      case DEFAULT_POSITION:
        compR.y = EDGE_SPACING + 
                 (borderInsets.top -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
        break;
      case BELOW_TOP:
        compR.y = borderInsets.top - compD.height - TEXT_SPACING;
        break;
      case ABOVE_BOTTOM:
        compR.y = rect.height - borderInsets.bottom + TEXT_SPACING;
        break;
      case BOTTOM:
        compR.y = rect.height - borderInsets.bottom + TEXT_SPACING +
                 (borderInsets.bottom -EDGE_SPACING -TEXT_SPACING -compD.height)/2;
        break;
      case BELOW_BOTTOM:
        compR.y = rect.height - compD.height - EDGE_SPACING;
        break;
    }
    switch (titleJustification) {
      case LEFT:
      case DEFAULT_JUSTIFICATION:
        compR.x = TEXT_INSET_H + borderInsets.left;
        break;
      case RIGHT:
        compR.x = rect.width - borderInsets.right -TEXT_INSET_H -compR.width;
        break;
      case CENTER:
        compR.x = (rect.width - compR.width) / 2;
        break;
    }
    return compR;
  }
 
}

Perform outline-dragging with JInternalFrame

Outline dragging is a JDesktopPane client property that provides the user with a outline rectangle when JInternalFrames are dragged instead of displaying the full contents of the JInternalFrame, making dragging internal frames a lot more performant. To perform outline dragging, set the property JDesktopPane.dragMode to outline. Swing 1.1.1 had added fast implementation for fast dragging. Put the client property to “faster”.
Here’s an example:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
 
public class Main extends JFrame implements ActionListener {
   JDesktopPane desktop;
   int nframes = 0;
 
   public Main() {
      getContentPane().setLayout(new BorderLayout());
      desktop = new JDesktopPane(); 
      createInternalFrame();
      getContentPane().add(BorderLayout.CENTER, desktop);
      setJMenuBar(createMenuBar());
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   
      setTitle(&quot;Outline Dragging Demonstration&quot;);
      setSize(new Dimension(400, 400));
   }
 
   protected JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();

      JMenu menu = new JMenu(&quot;JDesktopPane&quot;);
      JMenuItem menuItem1 = new JMenuItem(&quot;Create new JInternalFrame&quot;);
      menuItem1.addActionListener(this);
      menu.add(menuItem1);
      menu.add(new JSeparator());
      JMenuItem menuItem2 = new JMenuItem(&quot;Outline dragging normal&quot;);
      menuItem2.addActionListener(this);
      menu.add(menuItem2);
      JMenuItem menuItem3 = new JMenuItem(&quot;Outline dragging on&quot;);
      menuItem3.addActionListener(this);
      menu.add(menuItem3);
      JMenuItem menuItem4 = new JMenuItem(&quot;Fast dragging&quot;);
      menuItem4.addActionListener(this);
      menu.add(menuItem4);
      menuBar.add(menu);

      return menuBar;
   }
 
   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals(&quot;Create new JInternalFrame&quot;)) {
         createInternalFrame();
      }
      else if (e.getActionCommand().equals(&quot;Outline dragging normal&quot;)) {
         desktop.putClientProperty(&quot;JDesktopPane.dragMode&quot;, &quot;normal&quot;);
      }
      else if (e.getActionCommand().equals(&quot;Outline dragging on&quot;)) {
         // Make dragging faster
         desktop.putClientProperty(&quot;JDesktopPane.dragMode&quot;, &quot;outline&quot;);
      }
      else if (e.getActionCommand().equals(&quot;Fast dragging&quot;)) {
         desktop.putClientProperty(&quot;JDesktopPane.dragMode&quot;, &quot;faster&quot;);
      }
   }
 
   protected void createInternalFrame() {
      JInternalFrame frame = new JInternalFrame(&quot;InternalFrame&quot;, 
         true,    // resizable
         true,    // closable
         true,    // maximizable
         true);   // iconifiable
      frame.setVisible(true); 
      desktop.add(frame);
      frame.setSize(200, 200);
      frame.setLocation(30*nframes, 30*nframes);
      nframes++;
      try {
         frame.setSelected(true);
      } catch (java.beans.PropertyVetoException e) {}
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.show();
   }
}

Changing a JButton’s label

Use the method setText.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   JList list; 
 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      // following objects are final because they are used
      // in a inner class
      final JTextField textfield = new JTextField(20); 
      final JButton button = new JButton("initial label");
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            if (!textfield.getText().equals(""))
               button.setText(textfield.getText());
         }
      });
 
      getContentPane().add(textfield);
      getContentPane().add(button);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Getting an event when a JComboBox is about to appear/disappear

Since JDK1.4, you can add PopupMenuListeners to your JComboBox. This allows you to get the events popupMenuWillBecomeVisible and popupMenuWillBecomeInvisible. Another event is supposed to be thrown, for a popupMenuCanceled, but is not, due to a bug.

Now that you have the possibility to get an event when the JComboBox popup is about to appear, you can lazily populate it. So only when the user wants to change the value of a JComboBox, you fill it up.

Here’s a short example. Due to a bug related to the order in which getPreferredSize and popupMenuWillBecomeVisible are called, the size of the component is not the desired size. Click twice on the JComboBox to see the desired size.

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
 
      final JComboBox combobox = new JComboBox(new Object[] {"Default Item"});
      getContentPane().add(combobox);
 
      combobox.addPopupMenuListener(new PopupMenuListener() {
         public void popupMenuCanceled(PopupMenuEvent e) {
            System.out.println("popupMenu canceled");
         }
         public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
            System.out.println("popupMenu will become invisible");
         }
         public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
            if (combobox.getModel().getSize() <= 1) {
               // lazily populate combobox
               for (int i=0; i<100; i++) {
                  combobox.insertItemAt("Item #" + i, 0);
               }
               combobox.validate();
            }
            System.out.println("popupMenu will become visible");
         }
      });  
 
      setSize(300, 300);
   }
 
   public static void main(String[] args) {
      (new Main()).setVisible(true);
   }
}