Creating a JFrame that cannot be resized

There is no good way to do this. The closest thing is to create a regular Frame (it doesn’t really matter whether it’s JFrame or Frame for this issue), register a ComponentListener on it and whenever it is resized, resize it back to its original size.

Most times you don’t need to do this though, you can almost always use a Window to achieve what you want.

Adding a menu bar to a JSplitPane

Main.java:

 
import javax.swing.plaf.basic.*;
import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
import java.awt.*;
 
public class Main extends JFrame
{
   JSplitPane splitPane;
  
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
 
      splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
                                 new JPanel(), new JPanel());
      splitPane.setDividerSize(20);
  
      splitPane.setUI(new MenuDividerUI(getMainMenuBar()));
 
      getContentPane().add(splitPane);
 
      addComponentListener(new ComponentAdapter() {
         public void componentShown(ComponentEvent event) {
            splitPane.setDividerLocation(0.5);  
                
            removeComponentListener(this);
         }
      });
   }
 
   public JMenuBar getMainMenuBar() {
      JMenuBar mainBar = new JMenuBar();
      JMenu menu = new JMenu("JSplitPane");
      JMenuItem item1 = new JMenuItem("HORIZONTAL_SPLIT");
      JMenuItem item2 = new JMenuItem("VERTICAL_SPLIT");
      menu.add(item1);
      menu.add(item2);
      mainBar.add(menu);
  
      item1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            splitPane.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
         }
      });
 
      item2.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
         }
      });
 
      return mainBar;
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}
 
class MenuDividerUI extends BasicSplitPaneUI
{
   protected JMenuBar menuBar;
 
   public MenuDividerUI(JMenuBar menuBar) {
      this.menuBar = menuBar;
   }
 
   public BasicSplitPaneDivider createDefaultDivider() {
      BasicSplitPaneDivider divider = new BasicSplitPaneDivider(this) {
         public int getDividerSize() { return menuBar.getPreferredSize().height; }
      };
      divider.setLayout(new BorderLayout());
      divider.add(BorderLayout.NORTH, menuBar);
 
      return divider;
   }
}

Setting the background color of the tabs of a JTabbedPane

You can set the background color using the method setBackgroundAt. However, this will only change the background of the tabs that are not selected. You can’t change the background of the selected tab unless you change the look and feel “TabbedPane.selected” property. This would change all JTabbedPanes that you have instantiated. A bug was filed for this: http://developer.java.sun.com/developer/bugParade/bugs/4230649.html.

import javax.swing.plaf.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
 
public class Main extends JPanel {                     
  public Main() {
    setLayout(new BorderLayout());
    
    UIManager.put("TabbedPane.selected", Color.green);
    
    JTabbedPane tabbedPane = new JTabbedPane();
    for (int i=0;i<10;i++) {
      tabbedPane.addTab("Tab #" + i, new JLabel("Tab #" + i));
      tabbedPane.setBackgroundAt(i, new Color(25*i, 25*i, 25*i));
    }
    add(tabbedPane, BorderLayout.CENTER);
  }
  
  JPanel createPane(String s) {
    JPanel p = new JPanel();
    p.add(new JLabel(s));
    return p;
  }
  
  public static void main(String[] args) {
    JFrame frame = new JFrame("JTabbedPane Selected Color Demonstration");
    frame.addWindowListener( new WindowAdapter() {
      public void windowClosing( WindowEvent e ) {
        System.exit(0);
      }
    });
    frame.getContentPane().add(new Main());
    frame.setSize(200, 100);
    frame.setVisible(true);
  }
}

Making JInternalFrame dragging perform faster

You can set the client property JDesktopPane.dragMode to specify how internal frames are shown when dragged.

null (default): the entire JInternalFrame is shown
outline: only a rectangle is shown
faster: a more efficient implementation of the default

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() {
      desktop = new JDesktopPane(); 
      createInternalFrame(); 
      setContentPane(desktop);
      setJMenuBar(createMenuBar());
   }

   protected JMenuBar createMenuBar() {
      JMenuBar menuBar = new JMenuBar();

      JMenu menu1 = new JMenu("Create");
      menu1.setMnemonic(KeyEvent.VK_C);
      JMenuItem menuItem1 = new JMenuItem("New");
      menuItem1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            createInternalFrame();
         }
      }); 
      menuItem1.setMnemonic(KeyEvent.VK_N);
      menu1.add(menuItem1);
      menuBar.add(menu1);
 
      JMenu menu2 = new JMenu("Dragging");
      menu2.setMnemonic(KeyEvent.VK_D);

      final JRadioButtonMenuItem defaultDraggingItem = new JRadioButtonMenuItem("Default");
      final JRadioButtonMenuItem outlineDraggingItem = new JRadioButtonMenuItem("Outline");
      final JRadioButtonMenuItem fasterDraggingItem = new JRadioButtonMenuItem("Faster");
      Action draggingAction = new AbstractAction() {
         public void actionPerformed(ActionEvent ae) {
            if (defaultDraggingItem.isSelected())
               desktop.putClientProperty("JDesktopPane.dragMode", null);
            else if (outlineDraggingItem.isSelected())
               desktop.putClientProperty("JDesktopPane.dragMode", "outline");
            else if (fasterDraggingItem.isSelected())
               desktop.putClientProperty("JDesktopPane.dragMode", "faster");
         }
      };
      defaultDraggingItem.addActionListener(draggingAction);
      outlineDraggingItem.addActionListener(draggingAction);
      fasterDraggingItem.addActionListener(draggingAction);
      menu2.add(defaultDraggingItem);
      menu2.add(outlineDraggingItem);
      menu2.add(fasterDraggingItem);
      ButtonGroup group = new ButtonGroup();
      group.add(defaultDraggingItem);
      group.add(outlineDraggingItem);
      group.add(fasterDraggingItem);

      menuBar.add(menu2);     

      return menuBar;
   }

   public void actionPerformed(ActionEvent e) {
      System.out.println("Now creating internal frame!");
   }
 
   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++;
      try {
         frame.setSelected(true);
      } catch (java.beans.PropertyVetoException e) {}
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(500, 300);
      main.setVisible(true);
   }
}

Creating a JButton without a border


JButtonBorder.java:
import javax.swing.*;
import java.awt.event.*;
 
public class JButtonBorder extends JFrame {
 
   private JButton b1, b2;
   private JPanel p;
 
   public JButtonBorder() {
      super("JButtonBorder");
      p = new JPanel();
      b1 = new JButton("button with border");
      b2 = new JButton("button without border");
	
      b2.setBorderPainted(false);
 
      setContentPane(p);
      p.add(b1);
      p.add(b2);
 	
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            dispose();
            System.exit(0);
         }
      });
 
      pack();
      setVisible(true);
   }
 
   public static void main(String args[]) {
      JButtonBorder j = new JButtonBorder();
   }
}

Removing all the elements of a JList

The quickest way to find an answer to these kind of questions is to look at the API. You can call either removeAllElements or clear on the list model. clear is preferred.

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());
 
      final DefaultListModel listModel = new DefaultListModel();   
 
      // populate listmodel
      for (int i=0; i&lt;5; i++) {
         listModel.addElement(&quot;list item #&quot; + i);
      }
 
      final JList list = new JList(listModel); 
 
      getContentPane().add(new JScrollPane(list));    
      JButton removeAllButton = new JButton(&quot;Remove all elements&quot;);
      removeAllButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
 
            listModel.clear();
 
         }
      });
      getContentPane().add(removeAllButton);
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
   
   public static void main(String[] args) {
      (new Main()).show();
   }
}

Showing a popup-menu when a user right-clicks on an element in a JList

This example creates a JList and shows a popupmenu only when the user clicked on the selected item.
Main.java:

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());
 
      final DefaultListModel listModel = new DefaultListModel();   
 
      // populate listmodel
      for (int i=0; i<5; i++) {
         listModel.addElement("list item #" + i);
      }
 
      final JList list = new JList(listModel); 
      getContentPane().add(new JScrollPane(list));    
 
      final JPopupMenu popupMenu = new JPopupMenu();
      popupMenu.add(new JMenuItem("PopupItem 1"));
      popupMenu.add(new JMenuItem("PopupItem 2"));
      popupMenu.add(new JPopupMenu.Separator());
      popupMenu.add(new JMenuItem("PopupItem 3"));
 
      list.addMouseListener(new MouseAdapter() {
         public void mouseClicked(MouseEvent me) {
            if (SwingUtilities.isRightMouseButton(me)    // if right mouse button clicked
                  && !list.isSelectionEmpty()            // and list selection is not empty
                  && list.locationToIndex(me.getPoint()) // and clicked point is
                     == list.getSelectedIndex()) {       //   inside selected item bounds
               popupMenu.show(list, me.getX(), me.getY());
            }
         }
      });
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent event) {
            System.exit(0);   
         }      
      });
 
      pack();
   }
   
   public static void main(String[] args) {
      (new Main()).show();
   }
}