Setting the top-left icon of a JDialog

You can’t directly set the top-left icon of a JDialog. It inherits its icon from the owner JFrame. What you could do is pass a dummy JFrame as the owner when you create the JDialog. This example shows you:

import javax.swing.event.*; 
import java.awt.event.*; 
import javax.swing.*; 
import java.awt.*; 
 
public class Main extends JFrame 
{ 
   private AboutBox about; 
   
   public Main() { 
      super("Main test"); 
 
      setSize(450, 350); 
 
      ImageIcon icon = new ImageIcon("first.gif"); 
      setIconImage(icon.getImage()); 
    
      JButton button = new JButton("Open dialog");
      getContentPane().setLayout(new FlowLayout());
      getContentPane().add(button);
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            about = new AboutBox(new JFrame()); 
            about.setVisible(true);
         }
      });
 
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(0);
         }
      });
   } 
 
   public static void main(String []args) {
      Main main = new Main();
      main.setVisible(true);
   }
}
 
class AboutBox extends JDialog 
{ 
   public AboutBox(JFrame owner) { 
      super(owner, "About Swing Menu", true); 
   
      ImageIcon icon = new ImageIcon("second.gif"); 
      owner.setIconImage(icon.getImage()); 

      JButton button = new JButton("Close");
      getContentPane().setLayout(new FlowLayout());
      getContentPane().add(button);
      button.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
            dispose();
         }
      });
 
      setSize(250, 150); 
   }
}

Setting the JSplitPane divider location

Use the method setDividerLocation to set the divider. You can either set the new position by pixel or specify a percentage:

   // sets the divider at pixel 100
   splitPane.setDividerLocation(100);
   // sets the divider in the middle
   splitPane.setDividerLocation(.5);

The JSplitPane MUST be visible invoking this method
otherwise it will not have the desired effect. Here’s an example on how to set the divider location the moment when the JFrame becomes visible:

import java.awt.event.*;
import javax.swing.*;
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());
 
      createMainMenuBar();
 
      getContentPane().add(splitPane);
 
      addComponentListener(new ComponentAdapter() {
         public void componentShown(ComponentEvent event) {
            splitPane.setDividerLocation(0.5); 
                
            removeComponentListener(this);
         }
      });
   }
 
   public void createMainMenuBar() {
      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);
      setJMenuBar(mainBar);
 
      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);
         }
      });
   }
 
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(300, 300);
      main.setVisible(true);
   }
}

Number of rows the JTabbedPane is using to display its tabs

According to the javax.swing.JTabbedPane.getTabRunCount() API documentation:

Returns the number of tab runs currently used to display the tabs.

Returns:

an integer giving the number of rows if the tabPlacement is TOP or BOTTOM and the number of columns if tabPlacement is LEFT or RIGHT, or 0 if there is no UI set on this tabbedpane

This is basically a convenience method for javax.swing.plaf.TabbedPaneUI.getTabRunCount(), which has no API documentation.

The code for TabRunTest takes the following command line params:

  1. TOP (default) | BOTTOM (1) | LEFT (2) | RIGHT (3)

  • creates the specified number of tabs or 10 (if not specified or negative).

    There is a Get Info button at the bottom of the window to display the tab count and tab run count

    TabRowTest.java:

    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
     
    /**
     *@author     Administrator
     *@created    February 4, 2002
     */
    public class TabRowTest extends JPanel {
     
       public static void main(String[] args) {
          JFrame frame = new JFrame("Multiple Colors...");
     
          java.awt.Container contentPane = frame.getContentPane();
          contentPane.setLayout(new BorderLayout());
     
          int tabPlacement = JTabbedPane.TOP;
          if (args.length > 0) {
             try {
                int temp = Integer.parseInt(args[0]);
                switch (temp) {
                   case 1:
                      tabPlacement = JTabbedPane.BOTTOM;
                      break;
                   case 2:
                      tabPlacement = JTabbedPane.LEFT;
                      break;
                   case 3:
                      tabPlacement = JTabbedPane.RIGHT;
                      break;
                }
             } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println();
                System.out.println("------------------CONTINUING--------------------");
                System.out.println();
             }
          }
     
          int numberOfTabs = 10;
          if (args.length > 1) {
             try {
                numberOfTabs = Integer.parseInt(args[1]);
                if (numberOfTabs < 0) {
                   numberOfTabs = 10;
                }
             } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println();
                System.out.println("------------------CONTINUING--------------------");
                System.out.println();
             }
          }
     
          contentPane.add(new TabRowTest(tabPlacement, numberOfTabs));
     
          frame.setSize(800, 600);
     
          frame.addWindowListener(
             new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent ev) {
                   System.exit(0);
                }
             });
     
          frame.show();
       }
       // main
     
       public TabRowTest(int tabPlacement, int numberOfTabs) {
          super(new BorderLayout());
     
          final JTabbedPane tabbedPane = new JTabbedPane(tabPlacement);
          add(tabbedPane, BorderLayout.CENTER);
     
          for (int idx = 1; idx < (numberOfTabs + 1); idx++) {
             tabbedPane.addTab("Tab #" + idx, new JLabel("Tab #" + idx + " stuff"));
          }
     
          JPanel infopanel = new JPanel(new GridLayout(1, 0));
          add(infopanel, BorderLayout.SOUTH);
     
          final JLabel tabCountLabel = new JLabel("<tab count>");
          final JLabel runCountLabel = new JLabel("<run count>");
     
          JButton button = new JButton("Get Info");
          infopanel.add(button);
          infopanel.add(new JLabel("# Tabs:"));
          infopanel.add(tabCountLabel);
          infopanel.add(new JLabel("Run Count:"));
          infopanel.add(runCountLabel);
     
          button.addActionListener(
             new ActionListener() {
                public void actionPerformed(ActionEvent ev) {
                   int tabCount = tabbedPane.getTabCount();
                   int runCount = tabbedPane.getTabRunCount();
                   tabCountLabel.setText("" + tabCount);
                   runCountLabel.setText("" + runCount);
                }
             });
       }
       // TabRowTest
    }
    

  • Programmatically iconify a JInternalFrame

    Use the following line of code:

       frame.getDesktopPane().getDesktopManager().iconifyFrame(frame);
    

    Main.java:

    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    import java.net.*;
     
    public class Main extends JFrame {
       JDesktopPane desktop;
       int nframes = 0;
       Hashtable frames = new Hashtable();
     
       JMenu internalFrameMenu;
       Action iconifyAction = new IconifyAction();
    
       public Main() {
          desktop = new JDesktopPane(); 
          setContentPane(desktop);
          setJMenuBar(createMenuBar());
          createInternalFrame(); 
       }
     
       protected JMenuBar createMenuBar() {
          JMenuBar menuBar = new JMenuBar();
     
          JMenu createMenu = new JMenu("Create");
          createMenu.setMnemonic(KeyEvent.VK_C);
          JMenuItem newMenuItem = new JMenuItem("New");
          newMenuItem.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                createInternalFrame();
             }
          }); 
          newMenuItem.setMnemonic(KeyEvent.VK_N);
          createMenu.add(newMenuItem);
          menuBar.add(createMenu);
     
          internalFrameMenu = new JMenu("Iconify");
          internalFrameMenu.setMnemonic(KeyEvent.VK_I);
      
          menuBar.add(internalFrameMenu);     
     
          return menuBar;
       }
     
       protected void createInternalFrame() {
          nframes++;
          String title = "JInternalFrame #" + nframes;
          JInternalFrame frame = new JInternalFrame(title,
             true,    // resizable
             true,    // closable
             true,    // maximizable
             true);   // iconifiable
          frame.setVisible(true); 
          desktop.add(frame);
          frame.setSize(200, 200);
          frame.setLocation(30*nframes, 30*nframes);
          try {
             frame.setSelected(true);
          } catch (java.beans.PropertyVetoException e) {}
     
          JMenuItem menuItem = new JMenuItem(title);
          internalFrameMenu.add(menuItem);
          menuItem.addActionListener(iconifyAction);
          frames.put(title, frame);
       }
     
       public static void main(String []args) {
          Main main = new Main();
          main.setSize(500, 300);
          main.setVisible(true);
       }
     
       public class IconifyAction extends AbstractAction
       {
          public IconifyAction() {
             super("Iconify Action");
          }
     
          public void actionPerformed(ActionEvent ae) {
             JInternalFrame frame = (JInternalFrame) frames.get(((JMenuItem) ae.getSource()).getText());
             frame.getDesktopPane().getDesktopManager().iconifyFrame(frame);
          }
       }
    }
    

    Handling a JButton event

    Register a class that implements the ActionListener interface to your JButton and handle the event in the method actionPerformed. If you register the same ActionListener class to a number of JButtons, you can distinguish what JButton was clicked by querying the component source (getSource) or by querying the actionCommand (getActionCommand). Here’s an example:

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
    public class Main extends JFrame
    {
       JTextField textfield;
       JButton button;
       JLabel label;
     
       public Main() {
          button = new JButton("Greet me!");
     
          button.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
     
                if (ae.getSource() == button)
                   label.setText("Hello " + textfield.getText());
     
                /*  OR:
                if (ae.getActionCommand().equals("Greet me!"))
                   label.setText("Hello " + textfield.getText());
                */
             }
          });
     
          JPanel panel = new JPanel(new GridLayout(1, 2));
          textfield = new JTextField(20);
          panel.add(textfield);
          panel.add(button);
          getContentPane().add(BorderLayout.NORTH, panel);
          label = new JLabel();
          getContentPane().add(BorderLayout.CENTER, label);
     
          addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent we) {
                System.exit(0);
             }
          });
     
          setSize(300, 100);
       }
     
       public static void main(String []args) {
          Main main = new Main();
          main.setVisible(true);
       }
    }
    

    The previous example creates a no-name class of the interface ActionListener and directly adds the implementation of all its defined methods (actionPerformed). Note that you do not have to test for what JButton was pressed as you add the no-name ActionListener class only to this JButton.
    Another option is to provide an object of a class that implements the ActionListener interface. However, in this case you would have to test for what button was clicked, that is if you have more than one button.

    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
     
    public class Main extends JFrame implements ActionListener
    {
       JTextField textfield;
       JButton button;
       JLabel label;
     
       public Main() {
          button = new JButton("Greet me!");
     
          button.addActionListener(this);
     
          JPanel panel = new JPanel(new GridLayout(1, 2));
          textfield = new JTextField(20);
          panel.add(textfield);
          panel.add(button);
          getContentPane().add(BorderLayout.NORTH, panel);
          label = new JLabel();
          getContentPane().add(BorderLayout.CENTER, label);
     
          addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent we) {
                System.exit(0);
             }
          });
     
          setSize(300, 100);
       }
     
       public void actionPerformed(ActionEvent ae) {
          if (ae.getSource() == button)
             label.setText("Hello " + textfield.getText());
       }
      
       public static void main(String []args) {
          Main main = new Main();
          main.setVisible(true);
       }
    }
    

    Changing the orientation of a JSlider

    You can either specify the orientation when you create your JSlider object or afterwards with the method setOrientation.

    Main.java:

    import javax.swing.event.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
       
    public class Main extends JFrame implements ActionListener {
       private JSlider slider;
     
       public Main() {
          getContentPane().setLayout(new FlowLayout());
     
          JMenuBar menuBar = new JMenuBar();
          JMenu menu = new JMenu("JSlider Orientation");
          JMenuItem menuItem1 = new JMenuItem("HORIZONTAL");
          JMenuItem menuItem2 = new JMenuItem("VERTICAL");
          menu.add(menuItem1);
          menu.add(menuItem2);
          menuItem1.addActionListener(this);
          menuItem2.addActionListener(this);
          menuBar.add(menu); 
          setJMenuBar(menuBar);
          
          slider = new JSlider(JSlider.HORIZONTAL, 0, 30, 15);
     
          getContentPane().add(slider);
    
          addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent event) {
                System.exit(0);   
             }      
          });
     
          setSize(400, 400);
       }
     
       public static void main(String[] args) {
          (new Main()).show();
       }
     
       public void actionPerformed(ActionEvent ae) {
          if (ae.getActionCommand().equals("HORIZONTAL"))
             slider.setOrientation(JSlider.HORIZONTAL);
          else if (ae.getActionCommand().equals("VERTICAL"))
             slider.setOrientation(JSlider.VERTICAL);
       }
    }