Disabling some of the elements in a JComboBox

We wrap a JComboBox element in our ConditionalItem object that includes a boolean variable isEnabled. Now, in order to change the default behavior of the JComboBox, we need to handle two things: first blur out the elements that are disabled using our own renderer and second ensure the disabled elements cannot be selected by adding a ActionListener that selects the old selected element in case a disabled item was selected.

Main.java:

import javax.swing.plaf.basic.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
    
public class Main extends JFrame
{ 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
      
      final JComboBox combobox = 
         new JComboBox(
            new ConditionalItem[] { 
               new ConditionalItem("Item 1"),
               new ConditionalItem("Item 2", false),
               new ConditionalItem("Item 3", false),
               new ConditionalItem("Item 4")
            }
         );
 
      getContentPane().add(combobox);
      combobox.setRenderer(new ConditionalComboBoxRenderer());
      combobox.addActionListener(new ConditionalComboBoxListener(combobox));
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });      
   
      setSize(new Dimension(200, 200));
   } 
 
   public static void main(String[] args) throws Exception {
      Main main = new Main();
      main.setVisible(true);
   }
}
 
class ConditionalComboBoxRenderer extends BasicComboBoxRenderer implements ListCellRenderer
{
   public Component getListCellRendererComponent(JList list, 
           Object value, int index, boolean isSelected, boolean cellHasFocus) {
      if (isSelected) {
        setBackground(list.getSelectionBackground());
        setForeground(list.getSelectionForeground());
      } else {
        setBackground(list.getBackground());
        setForeground(list.getForeground());
      } 
 
      if (!((Conditionable) value).isEnabled()) {
        setBackground(list.getBackground());
        setForeground(UIManager.getColor("Label.disabledForeground"));
      }
 
      setFont(list.getFont());
      setText((value == null) ? "" : value.toString());
      return this;
   }  
}
 
class ConditionalComboBoxListener implements ActionListener {
   JComboBox combobox;
   Object oldItem;
    
   ConditionalComboBoxListener(JComboBox combobox) {
      this.combobox = combobox;
      combobox.setSelectedIndex(0);
      oldItem = combobox.getSelectedItem();
   }
    
   public void actionPerformed(ActionEvent e) {
      Object selectedItem = combobox.getSelectedItem();
      if (!((Conditionable) selectedItem).isEnabled()) {
         combobox.setSelectedItem(oldItem);
      } else {
         oldItem = selectedItem;
      }
   }
}
 
class ConditionalItem implements Conditionable {
   Object  object;
   boolean isEnabled;
    
   ConditionalItem(Object object, boolean isEnabled) {
      this.object = object;
      this.isEnabled = isEnabled;
   }
    
   ConditionalItem(Object object) {
     this(object, true);
   }
   
   public boolean isEnabled() {
      return isEnabled;
   }
    
   public void setEnabled(boolean isEnabled) {
      this.isEnabled = isEnabled;
   }
    
   public String toString() {
      return object.toString();
   }
}
 
interface Conditionable {
   public boolean isEnabled();
   public void setEnabled(boolean enabled);
   public String toString();
}

Disable a JList element

Create your custom CellRenderer that takes care of blurring out the disabled items. But beware, you are still able to select them, even though you don’t see a proof for this visually. Add a custom SelectionListener and unselect the disabled items that were clicked on. Here’s the code:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
  
public class Main extends JFrame {
 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      Vector v = new Vector();
      final JList list = new JList(new ConditionableItem[] { 
                                       new ConditionableItem("Item 1"),
                                       new ConditionableItem("Item 2", false),
                                       new ConditionableItem("Item 3", false),
                                       new ConditionableItem("Item 4")
                                   });

      list.setCellRenderer(new ConditionableCellRenderer());
      list.addListSelectionListener(new ConditionableListSelectionListener());
      getContentPane().add(new JScrollPane(list));    
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.out.println("Selected items were:");
            Object sv[] = list.getSelectedValues();
            for (int i=0; i<sv.length; i++)
               System.out.println(sv[i]);
 
            System.exit(0);   
         }      
      });
 
      setSize(200, 300);
   }
   
   public static void main(String[] args) {
      (new Main()).show();
   }
}
 
class ConditionableCellRenderer extends DefaultListCellRenderer implements ListCellRenderer {
   public Component getListCellRendererComponent(
      JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)    
   {
      setText((value == null) ? "" : value.toString());
      if (isSelected) {
         setBackground(list.getSelectionBackground());
         setForeground(list.getSelectionForeground());
      }
      else {
         setBackground(list.getBackground());
         setForeground(list.getForeground());
      }
 
      if (!((Conditionable) value).isEnabled()) {
         setBackground(list.getBackground());
         setForeground(UIManager.getColor("Label.disabledForeground"));
      }
 
      setEnabled(list.isEnabled());
      setFont(list.getFont());
 
      return this;
   }
}
 
class ConditionableListSelectionListener implements ListSelectionListener {
   public void valueChanged(ListSelectionEvent e) {
      JList list = (JList) e.getSource();
      ListModel lm = list.getModel();
      for (int i=e.getFirstIndex(); i<=e.getLastIndex(); i++) {
         if (list.getSelectionModel().isSelectedIndex(i)) {
            if (!((Conditionable) lm.getElementAt(i)).isEnabled()) {
               list.removeSelectionInterval(i, i);
            }
         }
      }
   }
}
 
class ConditionableItem implements Conditionable {
   Object  object;
   boolean isEnabled;
                            
   public ConditionableItem(Object object, boolean isEnabled) {
      this.object = object;
      this.isEnabled = isEnabled;
   }
                             
   ConditionableItem(Object object) {
      this(object, true);
   }
                           
   public boolean isEnabled() {
      return isEnabled;
   }
                            
   public void setEnabled(boolean isEnabled) {
      this.isEnabled = isEnabled;
   }
                            
   public String toString() {
      return object.toString();
   }
}
                         
interface Conditionable {
   public boolean isEnabled();
   public void setEnabled(boolean enabled);
   public String toString();
}

Displaying the description of a JMenuItem in the statusbar when moving over it

Add a ChangeListener. Here’s an example:

Main.java:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new BorderLayout());
 
      JMenuBar menuBar = new JMenuBar();
      JMenu menu = new JMenu("Sex");
      JMenuItem menuItem1 = new JMenuItem("male");
      JMenuItem menuItem2 = new JMenuItem("female");
      JMenuItem menuItem3 = new JMenuItem("androgyne");
 
      // add the MenuItems to the Menu 
      menu.add(menuItem1);
      menu.add(menuItem2);
      menu.add(menuItem3);
 
      StatusBar statusBar = new StatusBar();
 
      new JMenuItemHelpText(menuItem1, "The weak sex", statusBar);
      new JMenuItemHelpText(menuItem2, "The strong sex", statusBar);
      new JMenuItemHelpText(menuItem3, "The average sex, I guess", statusBar);
 
      menuBar.add(menu);
 
      this.setJMenuBar(menuBar); 
      getContentPane().add(BorderLayout.SOUTH, statusBar);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}
 
class JMenuItemHelpText implements ChangeListener
{
   JMenuItem menuItem;
   String helpText;
   StatusBar statusBar;
 
   JMenuItemHelpText(JMenuItem menuItem, String helpText, StatusBar statusBar) {
      this.menuItem = menuItem;
      this.helpText = helpText;
      this.statusBar = statusBar;
      menuItem.addChangeListener(this);
   } 
 
   public void stateChanged(ChangeEvent evt)
   {
      if (menuItem.isArmed())
         statusBar.setStatus(helpText);
      else
         statusBar.setStatus("");
   }
}
 
class StatusBar extends JPanel
{
   private JLabel statusLabel;
 
   public StatusBar() {
      setLayout(new BorderLayout(2, 2));
      statusLabel = new JLabel("Ready");
      statusLabel.setBorder(BorderFactory.createLoweredBevelBorder());
      statusLabel.setForeground(Color.black);
 
      add(BorderLayout.CENTER, statusLabel);
       
      JLabel dummyLabel = new JLabel("       ");
      dummyLabel.setBorder(BorderFactory.createLoweredBevelBorder());
 
      add(BorderLayout.EAST, dummyLabel);
   }
 
   public void setStatus(String status) {
      if (status.equals("")) 
         statusLabel.setText("Ready");
      else
         statusLabel.setText(status);
   }
 
   public String getStatus() {
      return statusLabel.getText();
   }
}

Using a JSpinner to walk through numbers

Use the SpinnerNumberModel.

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame 
{
   public Main()
   {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
 
      getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
      final SpinnerNumberModel spinnerModel = 
                   new SpinnerNumberModel(10.0, -500.0, 500.0, .5);
      JSpinner spinner = new JSpinner(spinnerModel);
 
      final JTextField textfield = new JTextField(25);
      JButton button = new JButton("Get number");
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) { 
            textfield.setText(""+spinnerModel.getNumber().doubleValue());
         }
      });
 
      getContentPane().add(spinner);
      getContentPane().add(button);
      getContentPane().add(textfield);
   }
  
   public static void main(String args[])
   {
      Main main = new Main();
      main.setSize(300, 150);
      main.setVisible(true);
   }
}

Changing the background of a JLabel

Call the method setBackground on your component but don’t forget to set your component to be opaque. A component that is not opaque (default) lets whatever is behind them show through. Their backgrounds are determined by the component that is shown behind them.

Main.java:

import javax.swing.text.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
   
public class Main extends JFrame {
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      JLabel label = new JLabel("Colored label");
      label.setOpaque(true);
      label.setBackground(Color.white);
 
      getContentPane().add(label);     
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(300, 100);
   }
 
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Creating a vertical JLabel

Code currently works only for 90 or -90 degrees only. It was written to support the answer to the question “How do I create vertically oriented JTable headers”.

Main.java:

import javax.swing.plaf.basic.*;
import javax.swing.border.*;
import javax.swing.text.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
 
public class Main extends JFrame
{
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
 
      getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
 
      JLabel normalLabel = new JLabel(&quot;Normal JLabel&quot;);
      normalLabel.setBorder(LineBorder.createBlackLineBorder());
 
      JLabel verticalLabel1 = new JLabel(&quot;Vertical JLabel, 90 degrees&quot;);
      verticalLabel1.setUI(new VerticalLabelUI(90));
      verticalLabel1.setBorder(LineBorder.createBlackLineBorder());
 
      JLabel verticalLabel2 = new JLabel(&quot;Vertical JLabel, -90 degrees&quot;);
      verticalLabel2.setUI(new VerticalLabelUI(-90));
      verticalLabel2.setBorder(LineBorder.createBlackLineBorder());
 
      getContentPane().add(normalLabel);
      getContentPane().add(verticalLabel1);
      getContentPane().add(verticalLabel2);
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.pack();
      main.setVisible(true);
   }
}
 
class VerticalLabelUI extends BasicLabelUI
{
   protected float degrees;
   private static Rectangle paintIconR = new Rectangle();
   private static Rectangle paintTextR = new Rectangle();
   private static Rectangle paintViewR = new Rectangle();
   private static Insets paintViewInsets = new Insets(0, 0, 0, 0);
 
   VerticalLabelUI(float degrees) {
      this.degrees = degrees;
   }
 
   public Dimension getPreferredSize(JComponent c) {
      Dimension dim = super.getPreferredSize(c);
      return new Dimension( dim.height, dim.width );
   }
 
   public void paint(Graphics g, JComponent c) {
      JLabel label = (JLabel)c;
      String text = label.getText();
      Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
 
      if ((icon == null) &amp;&amp; (text == null)) {
         return;
      }
 
      FontMetrics fm = g.getFontMetrics();
      paintViewInsets = c.getInsets(paintViewInsets);
 
      paintViewR.x = paintViewInsets.left;
      paintViewR.y = paintViewInsets.top;    	
      paintViewR.height = c.getWidth() - (paintViewInsets.left + paintViewInsets.right);
      paintViewR.width = c.getHeight() - (paintViewInsets.top + paintViewInsets.bottom);
 
      paintIconR.x = paintIconR.y = paintIconR.width = paintIconR.height = 0;
      paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
 
      String clippedText = 
            layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
 
      Graphics2D g2d = (Graphics2D) g;
      // save old value
      AffineTransform affineTransformOld = g2d.getTransform();
 
      double radians = degrees * (Math.PI / 180); 
      g2d.rotate(radians);
      Dimension d = getPreferredSize(c);      
      if (degrees == 90) {
         g2d.translate(0,-d.width);
      } 
      else {
         g2d.translate(-d.height, 0);
      }
 
      if (icon != null) {
         icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
      }
 
      if (text != null) {
         View v = (View) c.getClientProperty(BasicHTML.propertyKey);
         if (v != null) {
            v.paint(g, paintTextR);
         }
         else {
            int textX = paintTextR.x;
            int textY = paintTextR.y + fm.getAscent();
 
            if (label.isEnabled()) {
               paintEnabledText(label, g, clippedText, textX, textY);
            }
            else {
               paintDisabledText(label, g, clippedText, textX, textY);
            }
         }
      } 	
    	
      g2d.setTransform(affineTransformOld);
   }
}

Forcing the JColorChooser to display only the RGB tab

Get the panels through the method getChooserPanels and remove the ones that
area not DefaultRGBChooserPanels.

Main.java:

import javax.swing.colorchooser.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      super("JColorChooser Demonstration");
   
      getContentPane().setLayout(new FlowLayout()); 
      JColorChooser colorChooser = new JColorChooser(); 
 
      AbstractColorChooserPanel[] panels = colorChooser.getChooserPanels(); 
      for (int i=0; i<panels.length; i++) {
         if (panels[i].toString().indexOf("DefaultRGBChooserPanel") == -1) {
            colorChooser.removeChooserPanel(panels[i]);
         }
      }
 
      getContentPane().add(colorChooser); 
  
      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);
   }
}

Previewing textfiles in a JFileChooser

Use the setAccessory method!

Main.java:

import javax.swing.filechooser.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
import java.io.*;
  
public class Main extends JFrame {
   public static void main(String[] args) {
      JFileChooser fc = new JFileChooser();
 
      // show only directories and .txt files       
      fc.addChoosableFileFilter(new TextfileFilter());
 
      // set the accessory JComponent to our panel that previews textfiles
      fc.setAccessory(new TextfilePreview(fc));
 
      int returnVal = fc.showDialog(new JFrame(), "Open");
 
      if (returnVal == JFileChooser.APPROVE_OPTION) {
         File file = fc.getSelectedFile();
      }
 
      System.exit(0);
   }
}
 
class TextfileFilter extends javax.swing.filechooser.FileFilter
{
   public boolean accept(File f) {
      if (f.isDirectory()) return true;
 
      return f.getName().toLowerCase().endsWith(".txt");
   }
 
   public String getDescription() {
      return "TextFiles (*.txt)";
   }
}
 
class TextfilePreview extends JPanel
                      implements PropertyChangeListener {
   File textfile;
   JTextArea ta;
 
   public TextfilePreview(JFileChooser fc) {
      fc.addPropertyChangeListener(this);
      ta = new JTextArea();
      JScrollPane sp = new JScrollPane(ta);
      setLayout(new BorderLayout());
      add(BorderLayout.CENTER, sp);
      setPreferredSize(new Dimension(150, 200));
   }
 
   public void loadTextfile() {
      if (textfile == null) return;
 
      try {
         ta.setText("");
         BufferedReader br = new BufferedReader(new FileReader(textfile));
         String line;
         while ((line = br.readLine()) != null) {
            ta.append(line + "n");
         }
         br.close();
      }
      catch(IOException e) {
      }
   } 
 
   public void propertyChange(PropertyChangeEvent e) {
      String prop = e.getPropertyName();
      if (prop.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
         textfile = (File) e.getNewValue();
         if (isShowing()) {
            loadTextfile();
            repaint();
         }
      }
   }
}

Creating a JTable with hideable columns

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



HideColumnTableExample.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
 
/**
 * @version 1.0 05/31/99
 */
public class HideColumnTableExample extends JFrame {

  public HideColumnTableExample(){
    super( "HideColumnTable Example" );
    
    JTable table = new JTable(5, 7);
    ColumnButtonScrollPane pane = new ColumnButtonScrollPane(table);
    getContentPane().add(pane);
  }

  public static void main(String[] args) {
    HideColumnTableExample frame = new HideColumnTableExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.setSize( 400, 100 );
    frame.setVisible(true);
  }
}

ColumnButtonScrollPane.java:

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;
import jp.gr.java_conf.tame.swing.layout.*;
import jp.gr.java_conf.tame.swing.border.*;
import jp.gr.java_conf.tame.swing.icon.*;
 
/**
 * @version 1.0 05/31/99
 */
public class ColumnButtonScrollPane extends JScrollPane {
  Component columnButton;

  public ColumnButtonScrollPane(JTable table) {
    super(table);
    TableColumnModel cm = table.getColumnModel();
    LimitedTableHeader header = new LimitedTableHeader(cm);
    table.setTableHeader(header);
    columnButton = createUpperCorner(header);
    setCorner(UPPER_RIGHT_CORNER, columnButton);
    setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
    ColumnButtonScrollPaneLayout layout = new ColumnButtonScrollPaneLayout();
    setLayout(layout);
    layout.syncWithScrollPane(this);
  }
  
  protected Component createUpperCorner(JTableHeader header) {
    ColumnButton corner = new ColumnButton(header);
    return corner;
  }

  
  public class LimitedTableHeader extends JTableHeader {
    public LimitedTableHeader(TableColumnModel cm) {
      super(cm);
    }
       
    // actually, this is a not complete way. but easy one.
    // you can see last column painted wider, short time :) 
    // If you don't like this kind cheap fake,
    // you have to overwrite the paint method in UI class.
    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      columnButton.repaint();
    }    
  }

  
  public class ColumnButton extends JPanel {    
    JTable table;
    TableColumnModel cm;
    JButton revealButton; 
    JButton hideButton;
    Stack stack;  
    
    public ColumnButton(JTableHeader header) {
      setLayout(new GridLayout(1,2));
      setBorder(new LinesBorder(SystemColor.controlShadow, new Insets(0,1,0,0)));
      
      stack = new Stack();
      table = header.getTable();
      cm    = table.getColumnModel();
      
      revealButton = createButton(header, SwingConstants.WEST);
      hideButton   = createButton(header, SwingConstants.EAST);
      add(revealButton);
      add(hideButton);   
         
      revealButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {      
          TableColumn column = (TableColumn)stack.pop();
          cm.addColumn(column);
          if (stack.empty()) {
            revealButton.setEnabled(false);
          }
          hideButton.setEnabled(true);
          table.sizeColumnsToFit(-1);
        }
      });
      hideButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {      
          int n = cm.getColumnCount();
          TableColumn column = cm.getColumn(n -1);
          stack.push(column);
          cm.removeColumn(column);
          if (n < 3) {
            hideButton.setEnabled(false);
          }
          revealButton.setEnabled(true);
          table.sizeColumnsToFit(-1);
        }
      });
      
      if (1 < cm.getColumnCount()) {
        hideButton.setEnabled(true);
      } else {
        hideButton.setEnabled(false);
      }
      revealButton.setEnabled(false);
    }
    
    protected JButton createButton(JTableHeader header,
                                   int direction) {
      //int iconHeight = header.getPreferredSize().height - 6;
      int iconHeight = 8;
      JButton button = new JButton();
      button.setIcon(new ArrowIcon(iconHeight, direction, true));
      button.setDisabledIcon(new ArrowIcon(iconHeight, direction, false));
      button.setRequestFocusEnabled(false);
      button.setForeground(header.getForeground());
      button.setBackground(header.getBackground());
      button.setBorder(UIManager.getBorder("TableHeader.cellBorder"));
      return button;
    }    
  }
}

ColumnButtonScrollPaneLayout.java:

import java.awt.*;
import javax.swing.*;


/**
 * @version 1.0 05/29/99
 */
public class ColumnButtonScrollPaneLayout extends  ScrollPaneLayout {

  public ColumnButtonScrollPaneLayout() {
    super.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
  }

  public void setVerticalScrollBarPolicy(int x) {
    // VERTICAL_SCROLLBAR_ALWAYS
    super.setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
  }
  
  public void layoutContainer(Container parent) {
    super.layoutContainer(parent);
    
    if ((colHead == null) || (! colHead.isVisible()) ||
        (upperRight == null) || (vsb == null)) {
      return;
    }
    
    Rectangle vsbR = new Rectangle(0, 0, 0, 0);
    vsbR = vsb.getBounds(vsbR);
    
    Rectangle colHeadR = new Rectangle(0, 0, 0, 0);
    colHeadR = colHead.getBounds(colHeadR);
    colHeadR.width -= vsbR.width;
    colHead.getBounds(colHeadR);
    
    Rectangle upperRightR = upperRight.getBounds();
    upperRightR.x     -= vsbR.width;
    upperRightR.width += vsbR.width + 1;
    upperRight.setBounds(upperRightR);
  }  
}

LinesBorder.java:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
 
/**
 * @version 1.0 03/09/99
 */
public class LinesBorder extends AbstractBorder implements SwingConstants { 
  protected int northThickness;
  protected int southThickness;
  protected int eastThickness;
  protected int westThickness;  
  protected Color northColor;
  protected Color southColor;
  protected Color eastColor;
  protected Color westColor;
  
  public LinesBorder(Color color) {
    this(color, 1);
  }

  public LinesBorder(Color color, int thickness)  {
    setColor(color);
    setThickness(thickness);
  }

  public LinesBorder(Color color, Insets insets)  {
    setColor(color);
    setThickness(insets);
  }

  public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Color oldColor = g.getColor();
    
    g.setColor(northColor);
    for (int i = 0; i < northThickness; i++)  {
      g.drawLine(x, y+i, x+width-1, y+i);
    }
    g.setColor(southColor);
    for (int i = 0; i < southThickness; i++)  {
      g.drawLine(x, y+height-i-1, x+width-1, y+height-i-1);
    }
    g.setColor(eastColor);
    for (int i = 0; i < westThickness; i++)  {
      g.drawLine(x+i, y, x+i, y+height-1);
    }
    g.setColor(westColor);
    for (int i = 0; i < eastThickness; i++)  {
      g.drawLine(x+width-i-1, y, x+width-i-1, y+height-1);
    }

    g.setColor(oldColor);
  }

  public Insets getBorderInsets(Component c)       {
    return new Insets(northThickness, westThickness, southThickness, eastThickness);
  }

  public Insets getBorderInsets(Component c, Insets insets) {
    return new Insets(northThickness, westThickness, southThickness, eastThickness);    
  }


  public boolean isBorderOpaque() { return true; }
    
  public void setColor(Color c) {
    northColor = c;
    southColor = c;
    eastColor  = c;
    westColor  = c;
  }
  
  public void setColor(Color c, int direction) {
    switch (direction) {
      case NORTH: northColor = c; break;
      case SOUTH: southColor = c; break;
      case EAST:  eastColor  = c; break;
      case WEST:  westColor  = c; break;
      default: 
    }
  }
    
  public void setThickness(int n) {
    northThickness = n;
    southThickness = n;
    eastThickness  = n;
    westThickness  = n;
  }
    
  public void setThickness(Insets insets) {
    northThickness = insets.top;
    southThickness = insets.bottom;
    eastThickness  = insets.right;
    westThickness  = insets.left;
  }
  
  public void setThickness(int n, int direction) {
    switch (direction) {
      case NORTH: northThickness = n; break;
      case SOUTH: southThickness = n; break;
      case EAST:  eastThickness  = n; break;
      case WEST:  westThickness  = n; break;
      default: 
    }
  }

  public void append(LinesBorder b, boolean isReplace) {
    if (isReplace) {
      northThickness = b.northThickness;
      southThickness = b.southThickness;
      eastThickness  = b.eastThickness;
      westThickness  = b.westThickness;
    } else {
      northThickness = Math.max(northThickness ,b.northThickness);
      southThickness = Math.max(southThickness ,b.southThickness);
      eastThickness  = Math.max(eastThickness  ,b.eastThickness);
      westThickness  = Math.max(westThickness  ,b.westThickness);
    }
  }

  public void append(Insets insets, boolean isReplace) {
    if (isReplace) {
      northThickness = insets.top;
      southThickness = insets.bottom;
      eastThickness  = insets.right;
      westThickness  = insets.left;
    } else {
      northThickness = Math.max(northThickness ,insets.top);
      southThickness = Math.max(southThickness ,insets.bottom);
      eastThickness  = Math.max(eastThickness  ,insets.right);
      westThickness  = Math.max(westThickness  ,insets.left);
    }
  }
}

ArrowIcon.java:

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
  
/**
 * @version 1.0 02/26/99
 */
public class ArrowIcon implements Icon, SwingConstants {
  private static final int DEFAULT_SIZE = 11;
  //private static final int DEFAULT_SIZE = 5;

  private int size;
  private int iconSize;
  private int direction;
  private boolean isEnabled;
  private BasicArrowButton iconRenderer;

  public ArrowIcon(int direction, boolean isPressedView) {
    this(DEFAULT_SIZE, direction, isPressedView);
  }

  public ArrowIcon(int iconSize, int direction, boolean isEnabled) {
    this.size = iconSize / 2;
    this.iconSize = iconSize;
    this.direction = direction;
    this.isEnabled = isEnabled;
    iconRenderer = new BasicArrowButton(direction);
  }

  public void paintIcon(Component c, Graphics g, int x, int y) {
    iconRenderer.paintTriangle(g, x, y, size, direction, isEnabled);
  }

  public int getIconWidth() {
    //int retCode;
    switch (direction) {
      case NORTH:
      case SOUTH: return iconSize;
      case EAST:
      case WEST:  return size;
    }
    return iconSize;
  }

  public int getIconHeight() {
    switch (direction) {
      case NORTH:
      case SOUTH: return size;
      case EAST:
      case WEST:  return iconSize;
    }
    return size;
  }
}

Having a JRadioButton cell in a JTable

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



JRadioButtonTableExample.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
 
class RadioButtonRenderer implements TableCellRenderer {
  public Component getTableCellRendererComponent(JTable table, Object value,
                   boolean isSelected, boolean hasFocus, int row, int column) {
    if (value==null) return null;
    return (Component)value;
  }
}
 
class RadioButtonEditor extends    DefaultCellEditor
                        implements ItemListener {
  private JRadioButton button;
 
  public RadioButtonEditor(JCheckBox checkBox) {
    super(checkBox);
  }
 
  public Component getTableCellEditorComponent(JTable table, Object value,
                   boolean isSelected, int row, int column) {
    if (value==null) return null;
    button = (JRadioButton)value;
    button.addItemListener(this);
    return (Component)value;
  }
 
  public Object getCellEditorValue() {
    button.removeItemListener(this);
    return button;
  }
 
  public void itemStateChanged(ItemEvent e) {
    super.fireEditingStopped();
  }
}
 
public class JRadioButtonTableExample extends JFrame {
 
  public JRadioButtonTableExample(){
    super( "JRadioButtonTable Example" );
    UIDefaults ui = UIManager.getLookAndFeel().getDefaults();    
    UIManager.put("RadioButton.focus", ui.getColor("control"));
 
    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(
      new Object[][]{
        {"Group 1",new JRadioButton("A")},
        {"Group 1",new JRadioButton("B")},
        {"Group 1",new JRadioButton("C")},
        {"Group 2",new JRadioButton("a")},
        {"Group 2",new JRadioButton("b")}},
      new Object[]{"String","JRadioButton"});
                      
    JTable table = new JTable(dm) {
      public void tableChanged(TableModelEvent e) {
        super.tableChanged(e);
        repaint();
      }
    };
    ButtonGroup group1 = new ButtonGroup();
    group1.add((JRadioButton)dm.getValueAt(0,1));
    group1.add((JRadioButton)dm.getValueAt(1,1));
    group1.add((JRadioButton)dm.getValueAt(2,1));
    ButtonGroup group2 = new ButtonGroup();
    group2.add((JRadioButton)dm.getValueAt(3,1));
    group2.add((JRadioButton)dm.getValueAt(4,1));
    table.getColumn("JRadioButton").setCellRenderer(new RadioButtonRenderer());
    table.getColumn("JRadioButton").setCellEditor(new RadioButtonEditor(new JCheckBox()));
    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add( scroll );
    setSize( 200, 140 );
    setVisible(true);
  }
 
  public static void main(String[] args) {
    JRadioButtonTableExample frame = new JRadioButtonTableExample();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
  }
}



JRadioButtonTableExample2.java:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;

/**
 * @version 1.2 08/13/99
 */
public class JRadioButtonTableExample2 extends JFrame {
 
  public JRadioButtonTableExample2(){
    super( "JRadioButtonTable Example" );
 
    DefaultTableModel dm = new DefaultTableModel();
    dm.setDataVector(
      new Object[][]{
        {"1",new Integer(-1)},
        {"2",new Integer(-1)},
        {"3",new Integer(0)},
        {"4",new Integer(1)},
        {"5",new Integer(2)}},
      new Object[]{"Question","Answer"});
                      
    JTable table = new JTable(dm);
    String[] answer = {"A","B","C"};
     
    table.getColumn("Answer").setCellRenderer(
      new RadioButtonRenderer(answer)
    );
    table.getColumn("Answer").setCellEditor(
      new RadioButtonEditor(new JCheckBox(), 
                            new RadioButtonPanel(answer))
    );
    JScrollPane scroll = new JScrollPane(table);
    getContentPane().add( scroll );
  }
   
  // Cell base
  class RadioButtonPanel extends JPanel {
    JRadioButton[] buttons;
     
    RadioButtonPanel(String[] str) {
      setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
      buttons = new JRadioButton[str.length];
      for (int i=0; i<buttons.length; i++) {
        buttons[i] = new JRadioButton(str[i]);
        buttons[i].setFocusPainted(false);
        add(buttons[i]);
      }      
    }
     
    public void setSelectedIndex(int index) {
      for (int i=0;i<buttons.length;i++) {
        buttons[i].setSelected(i == index);
      }
    }
     
    public int getSelectedIndex() {
      for (int i=0; i<buttons.length; i++) {
        if (buttons[i].isSelected()) {
          return i;
        }
      }      
      return -1;      
    }
     
    public JRadioButton[] getButtons() {
      return buttons;
    }
  }
 
 
   
  class RadioButtonRenderer extends    RadioButtonPanel
                            implements TableCellRenderer {    
    RadioButtonRenderer(String[] strs) {
      super(strs);
    }
     
    public Component getTableCellRendererComponent(JTable table, Object value,
                   boolean isSelected, boolean hasFocus, int row, int column) {
      if (value instanceof Integer) {
        setSelectedIndex(((Integer)value).intValue());
      }      
      return this;
    }
  }
 
 
 
  class RadioButtonEditor extends    DefaultCellEditor
                          implements ItemListener {
    RadioButtonPanel panel;
 
    public RadioButtonEditor(JCheckBox checkBox,RadioButtonPanel panel) {
      super(checkBox);
      this.panel = panel;
      ButtonGroup buttonGroup = new ButtonGroup();
      JRadioButton[] buttons  = panel.getButtons();
      for (int i=0; i<buttons.length; i++) {
        buttonGroup.add(buttons[i]);
        buttons[i].addItemListener(this);
      }                  
    }
 
    public Component getTableCellEditorComponent(JTable table, Object value,
                   boolean isSelected, int row, int column) {
      if (value instanceof Integer) {
        panel.setSelectedIndex(((Integer)value).intValue());
      }      
      return panel;
    }
 
    public Object getCellEditorValue() {
      return new Integer(panel.getSelectedIndex());
    }
 
    public void itemStateChanged(ItemEvent e) {
      super.fireEditingStopped();
    }
  }
 
  public static void main(String[] args) {
    JRadioButtonTableExample2 frame = new JRadioButtonTableExample2();
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });
    frame.setSize( 230, 140 );
    frame.setVisible(true);
  }
}