Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Manipulating the Grid

To make it easy for users to distinguish among cells, the table component draws horizontal and vertical lines at regular intervals. Collectively, those lines form a grid. The table component's JTable class provides methods for manipulating a grid's color and showing/hiding grid lines. Table 3 describes the grid-manipulation methods.

Table 3 JTable Grid-Manipulation Methods

Method

Description

setGridColor(Color c)

Establishes the object referenced by c as the color of grid lines. Redisplays the grid.

setShowGrid(boolean isShown)

Shows horizontal and vertical grid lines if isShown is true. Otherwise, hides horizontal and vertical grid lines.

setShowHorizontalLines(boolean isShown)

Shows horizontal grid lines if isShown is true. Otherwise, hides horizontal grid lines.

setShowVerticalLines(boolean isShown)

Shows vertical grid lines if isShown is true. Otherwise, hides vertical grid lines.


As an example, assuming that you have a JTable variable jt that references a JTable object, jt.setGridColor (Color.cyan); establishes Color.cyan as the color of grid lines and redisplays the grid in the new color. Also, jt.setShowHorizontalLines (false); hides the grid's horizontal lines. Figure 3 shows the resulting grid.

Figure 3 A table component's grid-manipulation methods make it easy to change a grid's color and show/hide the grid's horizontal and vertical lines.

Listing 3 presents source code to a TableDemo3 application that allows you to experiment with changing the grid's colors and hiding/showing horizontal and vertical grid lines. The only grid-manipulation method not present is setShowGrid(boolean isShown). I leave it as an exercise for you to incorporate that method into the source code.

Listing 3: TableDemo3.java

// TableDemo3.java

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

class TableDemo3 extends JFrame implements ActionListener
{
  JTable jt;

  boolean horizFlag;
  boolean vertFlag;

  TableDemo3 (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a table with a default table model that specifies 10
   // rows by 10 columns dimensions.

   jt = new JTable (new DefaultTableModel (10, 10));

   // Add the table to the center portion of the frame window's
   // content pane.

   getContentPane ().add (jt);

   // Create a panel for positioning buttons.

   JPanel jp = new JPanel ();

   // Create a "Change Grid Color" button, register the current
   // TableDemo3 object as a listener to that button's action 
   // events, and add that button to the panel.

   JButton jb = new JButton ("Change Grid Color");
   jb.addActionListener (this);
   jp.add (jb);

   // Create a "Show/Hide Horizontal Lines" button, register the
   // current TableDemo3 object as a listener to that button's 
   // action events, and add that button to the panel.

   jb = new JButton ("Show/Hide Horizontal Lines");
   jb.addActionListener (this);
   jp.add (jb);

   // Create a "Show/Hide Vertical Lines" button, register the
   // current TableDemo3 object as a listener to that button's 
   // action events, and add that button to the panel.

   jb = new JButton ("Show/Hide Vertical Lines");
   jb.addActionListener (this);
   jp.add (jb);

   // Add the panel to the south portion of the frame window's
   // content pane.

   getContentPane ().add (jp, BorderLayout.SOUTH);

   // Establish the frame's initial size as 600x250 pixels.

   setSize (600, 250);

   // Display the frame window and all contained components.

   setVisible (true);
  }

  public void actionPerformed (ActionEvent e)
  {
   // Obtain a reference to the JButton object associated with the
   // peer window from where an action event was fired.

   JButton jb = (JButton) e.getSource ();

   // Obtain the JButton's text label.

   String label = jb.getText ();

   // If the label equals "Change Grid Color," then change the grid
   // color. If the label equals "Show/Hide Horizontal Lines," then
   // show or hide those lines by calling setShowHorizontalLines()
   // with the horizFlag Boolean argument value. Then toggle the
   // state of horizFlag so the next call to that method undoes the
   // current call. If label does not match the previous two 
   // strings,
   // then show or hide vertical lines by calling
   // setShowVerticalLines() with the vertFlag Boolean argument
   // value. Then toggle the state of vertFlag so the next call to
   // that method undoes the current call.

   if (label.equals ("Change Grid Color"))
     jt.setGridColor (new Color (rnd (256), rnd (256), 
                   rnd (256)));
   else
   if (label.equals ("Show/Hide Horizontal Lines"))
   {
     jt.setShowHorizontalLines (horizFlag);
     horizFlag = !horizFlag;
   }
   else
   {
     jt.setShowVerticalLines (vertFlag);
     vertFlag = !vertFlag;
   }
  }

  public static int rnd (int limit)
  {
   // Return a random number greater than or equal to 0 and less
   // than limit.

   return (int) (Math.random () * limit);
  }

  public static void main (String [] args)
  {
   // Create a TableDemo3 object, which creates the GUI.

   new TableDemo3 ("Table Demo #3");
  }
}
  • + Share This
  • 🔖 Save To Your Account