HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Meet the Swing JTable"
Volume: 4 Issue: 5, p. 30

	

Listing 1: Code for Simple JTable.
 
import com.sun.java.swing.*; 
import com.sun.java.swing.table.*; 

public class SimpleTableTest extends JFrame { 

   public SimpleTableTest() { 
      setLocation(100,100); 
      setSize(250,100); 

      String[][] data = {  {"eggs", "sandwich", "steak", "snickers"}, 
                           {"bacon", "pickles", "potato", "apple"}, 
                           {"syrup", "mayo", "corn", "banana"}, 
                           {"pancakes", "chips", "broccoli", "crunch bar"}, 
                           {"sausage", "pizza", "pie", "protein shake"}}; 
      String[] headers = {"Breakfast", "Lunch", "Dinner", "Snack"}; 
      DefaultTableModel model = new DefaultTableModel(data, headers); 
      JTable table = new JTable(model); 

      getContentPane().add(table); 

      setVisible(true); 
   } 

   public static void main(String[] args) { 
      SimpleTableTest simpleTableTest = new SimpleTableTest(); 
   } 
} 

Listing 2: JTable Inside JScrollPane.
 
import javax.swing.*; 
import javax.swing.table.*; 

public class SimpleTableTest extends JFrame { 

   public SimpleTableTest() { 
      setLocation(100,100); 
      setSize(250,100); 

      String[][] data = {  {"eggs", "sandwich", "steak", "snickers"}, 
                           {"bacon", "pickles", "potato", "apple"}, 
                           {"syrup", "mayo", "corn", "banana"}, 
                           {"pancakes", "chips", "broccoli", "crunch bar"}, 
                           {"sausage", "pizza", "pie", "protein shake"}}; 
      String[] headers = {"Breakfast", "Lunch", 
"Dinner", "Snack"}; 
      DefaultTableModel model = new DefaultTableModel(data, headers); 
      JTable table = new JTable(model); 
      JScrollPane scroll = new JScrollPane(table); 

      getContentPane().add(scroll); 

      setVisible(true); 
   } 

   public static void main(String[] args) { 
      SimpleTableTest simpleTableTest = new SimpleTableTest(); 
   } 
} 

Listing 3: Code to test the autoResizeMode property of the JTable.
 
import javax.swing.*; 

class TableColumnSize extends JFrame { 
    private static int offset = 50; 

    public TableColumnSize(int resizeMode, String title) { 
        // Dummy data for table. 
        String[][] tableData = { 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}}; 
        String[] headerData = {"Header 1", "Header 2", "Header 3", "Header 4"}; 

        // Set the frame's title and position. 
        setTitle(title); 
        offset += 50; 
        setLocation(offset,offset); 
        setSize(300,150); 

        // Create the JTable using the dummy data. 
        JTable table = new JTable(tableData, headerData); 

        // This is the important part of this example: Set the autoResizeMode 
        // of the JTable. 
        table.setAutoResizeMode(resizeMode); 

        // Create a scroll pane and insert the JTable into the scroll pane. 
        JScrollPane scroll = new JScrollPane(table); 

        getContentPane().add(scroll); 
    } 

    public static void main(String[] args) { 
        // Create 5 TableColumnSize frames - each demonstrating a 
        // different value of the autoResizeMode property. 
        (new TableColumnSize(JTable.AUTO_RESIZE_OFF,"AUTO_RESIZE_OFF")) 
.setVisible(true); 
        (new TableColumnSize(JTable.AUTO_RESIZE_ALL_COLUMNS, 
        "AUTO_RESIZE_ALL_COLUMNS")) 
.setVisible(true); 
        (new TableColumnSize(JTable.AUTO_RESIZE_LAST_COLUMN, 
        "AUTO_RESIZE_LAST_COLUMN")) 
.setVisible(true); 
        (new TableColumnSize(JTable.AUTO_RESIZE_NEXT_COLUMN, 
        "AUTO_RESIZE_NEXT_COLUMN")) 
.setVisible(true); 
        (new TableColumnSize(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS, 
        "AUTO_RESIZE_SUBSEQUENT_COLUMNS")) 
.setVisible(true); 
    } 
} 

Listing 4: Code to test the horizontalScrollBarPolicy and vertical 
Scroll BarPolicy of the JScrollPane.
 
import javax.swing.*; 

class ScrollBarTest extends JFrame { 
    private static int offset = 50; 

    public ScrollBarTest(int horizPolicy, int vertPolicy, String title) { 
        // Dummy data for table. 
        String[][] tableData = { 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}, 
            {"data 1", "data 2", "data 3", "data 4"}}; 
        String[] headerData = {"Header 1", "Header 2", "Header 3", 
            "Header 4"}; 

        // Set the frame's title and position. 
        setTitle(title); 
        offset += 50; 
        setLocation(offset,offset); 
        setSize(300,150); 

        // Create the JTable using the dummy data. 
        JTable table = new JTable(tableData, headerData); 

        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 

        // Create a scroll pane and insert the JTable into the scroll pane. 
        JScrollPane scroll = new JScrollPane(table); 

        // This is the important part of this example: Set the ScrollBar Policies 
        // of the JTable. 
        scroll.setHorizontalScrollBarPolicy(horizPolicy); 
        scroll.setVerticalScrollBarPolicy(vertPolicy); 

        getContentPane().add(scroll); 
    } 

    public static void main(String[] args) { 
        // Create 6 ScrollBarTest frames - each demonstrating a different 
        // value of the horizontalScrollBarPolicy and verticalScrollBarPolicy 
        // properties. 
       (new ScrollBarTest(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS,JScrollPane. 
VERTICAL_SCROLLBAR_NEVER,"HORIZONTAL_SCROLLBAR_ALWAYS")).setVisible(true); 
        (new ScrollBarTest(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED,JScrollPane. 
VERTICAL_SCROLLBAR_NEVER,"HORIZONTAL_SCROLLBAR_AS_NEEDED")).setVisible(true); 
        (new ScrollBarTest(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,JScrollPane. 
VERTICAL_SCROLLBAR_NEVER,"HORIZONTAL_SCROLLBAR_NEVER")).setVisible(true); 
        (new ScrollBarTest(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,JScrollPane. 
VERTICAL_SCROLLBAR_ALWAYS,"VERTICAL_SCROLLBAR_ALWAYS")).setVisible(true); 
        (new ScrollBarTest(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,JScrollPane. 
VERTICAL_SCROLLBAR_AS_NEEDED,"VERTICAL_SCROLLBAR_AS_NEEDED")).setVisible(true); 
        (new ScrollBarTest(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,JScrollPane. 
VERTICAL_SCROLLBAR_NEVER,"VERTICAL_SCROLLBAR_NEVER")).setVisible(true); 
    } 
} 

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.