Detecting JScrollPane events

Add a AdjustmentListener to the horizontal or vertical scrollbar of the JScrollPane (you can get its reference with the methods getHorizontalScrollBar and getVerticalScrollBar).

Note that the AdjustmentType will always be AdjustmentEvent.TRACK.
(From the API: “Note that the AdjustmentEvents type property will always have a placeholder value of AdjustmentEvent.TRACK because all changes to a BoundedRangeModels value are considered equivalent. To change the value of a BoundedRangeModel one just sets its value property, i.e. model.setValue(123). No information about the origin of the change, e.g. it’s a block decrement, is provided. We don’t try fabricate the origin of the change here.”)

Main.java:

import javax.swing.table.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame
{
   public Main() throws Exception {
      getContentPane().setLayout(new BorderLayout());
 
      JPanel panel = createPanel();
 
      JScrollPane scrollpane = new JScrollPane(panel);
      JScrollBar scrollbar = scrollpane.getVerticalScrollBar();
      scrollbar.addAdjustmentListener(new AdjustmentListener() {
         public void adjustmentValueChanged(AdjustmentEvent ae) {
            System.out.println(translateAdjustmentEvent(ae));
         }
      });
 
      getContentPane().add(BorderLayout.CENTER, scrollpane);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   }
 
   public String translateAdjustmentEvent(AdjustmentEvent ae) {
      String s = "type = ";
      switch(ae.getAdjustmentType()) {
         case AdjustmentEvent.UNIT_INCREMENT:
            s += "UNIT_INCREMENT"; break;
         case AdjustmentEvent.UNIT_DECREMENT:
            s += "UNIT_DECREMENT"; break;
         case AdjustmentEvent.BLOCK_INCREMENT:
            s += "BLOCK_INCREMENT"; break;
         case AdjustmentEvent.BLOCK_DECREMENT:
            s += "BLOCK_DECREMENT"; break;
         case AdjustmentEvent.TRACK:
            s += "TRACK"; break;
         default: break;
      }
      s += ", current value = " + ae.getValue();
 
      return s;
   }
 
   public static JPanel createPanel() throws Exception {
      JPanel panel = new JPanel();
      panel.setLayout(new GridLayout(50, 20, 10, 10));
 
      for (int i=0; i<50; i++) {
         for (int j=0; j<20; j++) {
            JLabel label = new JLabel("label " + i + ", " + j);
            panel.add(label);    
         }
      }
 
      return panel;
   }
 
   public static void main(String [] args) throws Exception  {
      Main main = new Main();
      main.setSize(600, 600);
      main.setVisible(true);
   } 
} 

Preventing a JTabbedPane tab from being selected

Override the setSelectedIndex in the JTabbedPane object, as this is called before the change takes place. Here’s an example that only allows you to select tabs with even numbers:

import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JFrame implements ActionListener {
   JTabbedPane tabbedPane;
   int ntabs = 0;
 
   public Main() {
      getContentPane().setLayout(new BorderLayout());
      tabbedPane = new JTabbedPane() {
         public void setSelectedIndex(int index) {
            System.out.println("index selected: " + index);
            if (index % 2 == 0) {
               System.out.println("allowed");
               super.setSelectedIndex(index);
            }
            else {
               System.out.println("not allowed");
            }
         }
      };
      createTab();
 
      getContentPane().add(BorderLayout.CENTER, tabbedPane);
      setJMenuBar(createMenuBar());
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
 
      setTitle("JTabbedPane Selection Demonstration");
      setSize(new Dimension(400, 400));
   }
 
   protected JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();
 
      JMenu menu = new JMenu("JTabbedPane");
      JMenuItem menuItem1 = new JMenuItem("Create new tab");
      menuItem1.addActionListener(this);
      menu.add(menuItem1);
      menuBar.add(menu);
 
      return menuBar;
   }
  
   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Create new tab")) {
         createTab();
      }
   }
 
   protected void createTab() {
      tabbedPane.addTab("Tab #" + ntabs, new JLabel("Tab #" + ntabs));
      ntabs++;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.show();
   }
}

Detecting when JToolBar has been made floatable

Main.java:

import javax.swing.plaf.basic.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
 
public class Main extends JFrame
{
   public Main()
   {
      final JToolBar toolBar = new JToolBar();
      JButton gifButton = new JButton("GIF");
      JButton jpgButton = new JButton("JPG");
      JButton tiffButton = new JButton("TIFF");
      toolBar.add(gifButton);
      toolBar.add(jpgButton);
      toolBar.add(tiffButton);
 
      getContentPane().add(BorderLayout.NORTH, toolBar);
 
      toolBar.addPropertyChangeListener(new PropertyChangeListener() {
         boolean wasFloating = false;
 
         public void propertyChange(PropertyChangeEvent evt) {
            if (toolBar.getUI() instanceof BasicToolBarUI) {
               BasicToolBarUI bt = (BasicToolBarUI) toolBar.getUI();
               if (wasFloating != bt.isFloating()) {
                  wasFloating = bt.isFloating();
                  System.out.println("Floating: " + wasFloating);
               }
            }
         }
      });            
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   }
      
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}

Setting the background image for a JInternalFrame

Main.java:

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
 
public class Main extends JFrame {
   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("Transparent JInternalFrame Demonstration");
      setSize(new Dimension(400, 400));
   }
 
   protected JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();
 
      JMenu menu = new JMenu("JDesktopPane");
      JMenuItem menuItem1 = new JMenuItem("Create new JInternalFrame");
      menuItem1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            createInternalFrame();
         }
      });
      menu.add(menuItem1);
      menuBar.add(menu);
 
      return menuBar;
   }
 
   protected void createInternalFrame() {
      JInternalFrame frame = new JInternalFrame("InternalFrame", 
         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++;
      frame.setContentPane(new ImageBackgroundPanel(
                             new ImageIcon("mong.jpg")));
 
      frame.getContentPane().add(new JLabel("Your name"));
      frame.getContentPane().add(new JTextField(10));
 
      try {
         frame.setSelected(true);
      } catch (java.beans.PropertyVetoException e) {}
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.show();
   }
}
 
class ImageBackgroundPanel extends JPanel
{
   private ImageIcon bgImage;
 
   public ImageBackgroundPanel(ImageIcon bgImage) {
      this.bgImage = bgImage;
   }
 
   public void paintComponent(Graphics g) {    
      Dimension d = getSize();      
      g.drawImage(bgImage.getImage(), 0, 0, (int)d.getWidth(), (int)d.getHeight() , this);          
   }
}

Creating a rounded JButton

Extend the JButton class and override the paintComponent and paintBorder methods. To ensure that only mouse-clicks are accepted within the round shape, override the contains method too.
Main.java:

import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.*;
   
public class Main extends JFrame implements ActionListener {
   JList list; 
 
   public Main() {
      getContentPane().setLayout(new FlowLayout());
 
      RoundButton rbutton = new RoundButton("Click me!");
      rbutton.addActionListener(this);
      getContentPane().add(BorderLayout.CENTER, rbutton);
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      setSize(400, 400);
   }
 
   public void actionPerformed(ActionEvent ae) {
      System.out.println("Your pressed the "" + ae.getActionCommand() + "" button");
   }
   
   public static void main(String[] args) {
      (new Main()).show();
   }
}
 
class RoundButton extends JButton
{
   // for mouse detection purposes
   Shape shape;

   public RoundButton(String label) {
      super(label);
      // allows us to paint a round background
      // if true, it would be rectangular
      setContentAreaFilled(false);
   }
 
   protected void paintComponent(Graphics g) {
      // if the button is pressed and ready to be released
      if (getModel().isArmed()) {
         g.setColor(Color.lightGray);
      } else {
         g.setColor(getBackground());
      }
 
      g.fillOval(0, 0, getSize().width-1, getSize().height-1);
 
      super.paintComponent(g);
   }
 
   // paint a round border as opposed to a rectangular one
   protected void paintBorder(Graphics g) {
      g.setColor(getForeground());
      g.drawOval(0, 0, getSize().width-1, getSize().height-1);
   }
 
   // only clicks within the round shape should be accepted
   public boolean contains(int x, int y) {
      if (shape == null || !shape.getBounds().equals(getBounds())) {
         shape = new Ellipse2D.Float(0, 0, getWidth(), getHeight());
      }
 
      return shape.contains(x, y);
   }
}

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();
   }
}