Programmatically selecting a tab of a JTabbedPane

Use the method setSelectedIndex and pass it the index of the tab that you want to be selected. Here’s an example:

import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.net.*;
 
public class Main extends JFrame implements ActionListener {
   JTabbedPane tabbedPane;
   int ntabs = 0;
 
   public Main() {
      getContentPane().setLayout(new BorderLayout());
      tabbedPane = new JTabbedPane();
      createTab();
 
      getContentPane().add(BorderLayout.CENTER, tabbedPane);
      setJMenuBar(createMenuBar());
  
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   
      setTitle("JTabbedPane Orientation Demonstration");
      setSize(new Dimension(400, 400));
   }
 
   protected JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();
 
      JMenu menu1 = new JMenu("JTabbedPane");
      JMenuItem menuItem1 = new JMenuItem("Create new tab");
      menuItem1.addActionListener(this);
      menu1.add(menuItem1);
      JMenu menu2 = new JMenu("Select tab");
      JMenuItem menuItem2 = new JMenuItem("Random tab");
      menuItem2.addActionListener(this);
      menu2.add(menuItem2);
      menuBar.add(menu1);
      menuBar.add(menu2);
 
      return menuBar;
   }
  
   public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Create new tab")) {
         createTab();
      }
      else if (e.getActionCommand().equals("Random tab")) {
         selectRandomTab();
      }
   }
 
   protected void createTab() {
      ntabs++;
      tabbedPane.addTab("Tab #" + ntabs, new JLabel("Tab #" + ntabs));
   }
 
   protected void selectRandomTab() {
      Random r = new Random();
      int tabToSelect = r.nextInt(ntabs);
      tabbedPane.setSelectedIndex(tabToSelect);
   }
  
   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();
   }
}

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