• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Java

JLayeredPane in Java Swing Example

By Dinesh Thakur

A layered pane is a Swing container which is used to hold the various components using the concept of layers. The components present in the upper layer overlaps the components present in the lower layer. The layered pane is created using the JLayeredPane class. the only constructor of this class is JLayeredPane ().

 

import java.awt.*; 
import javax.swing.*;
public class JavaExampleLayeredPaneInJApplet extends JApplet
  {
      JLayeredPane LyrdPn=new JLayeredPane();
      JLabel Lbls[];
      public void init()
        {
           setContentPane(LyrdPn);
           LyrdPn.setLayout(null);
           Lbls = new JLabel[6];
           Lbls[0] = new JLabel("Content's Layer");
           Lbls[0].setOpaque(true);
           Lbls[0].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[0], LyrdPn.FRAME_CONTENT_LAYER.intValue());
           LyrdPn.add(Lbls[0]);
           Lbls[1] = new JLabel("By Default Layer");
           Lbls[1].setOpaque(true);
           Lbls[1].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[1], LyrdPn.DEFAULT_LAYER.intValue());
           LyrdPn.add(Lbls[1]);
           Lbls[2] = new JLabel("Paletter Layer");
           Lbls[2].setOpaque(true);
           Lbls[2].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[2],LyrdPn.PALETTE_LAYER.intValue());
           LyrdPn.add(Lbls[2]);
           Lbls[3] = new JLabel("Modal Layer");
           Lbls[3].setOpaque(true);
           Lbls[3].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[3], LyrdPn.MODAL_LAYER.intValue());
           LyrdPn.add(Lbls[3]);
           Lbls[4] = new JLabel("Popup Layer");
           Lbls[4].setOpaque(true);
           Lbls[4].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[4], LyrdPn.POPUP_LAYER.intValue());
           LyrdPn.add(Lbls[4]);
           Lbls[5] = new JLabel("Drag Layer");
           Lbls[5].setOpaque(true);
           Lbls[5].setBorder(BorderFactory.createEtchedBorder());
           LyrdPn.setLayer(Lbls[5], LyrdPn.DRAG_LAYER.intValue());
           LyrdPn.add(Lbls[5]);
           for(int loop_indx=0;loop_indx<6;loop_indx++)
                {
                    Lbls[loop_indx].setBounds(40 * loop_indx, 40 * loop_indx, 100, 60);       
                }
        }
   }
/*<APPLET CODE=JavaExampleLayeredPaneInJApplet.class WIDTH=360 HEIGHT=290></APPLET>*/

JLayeredPane in Java Swing Example

Java – JLabel and Image within JApplet

By Dinesh Thakur

A label is a simple control which is used to display text (non-editable) on the window. Since it does not possess any user interactive feature, it is considered as a passive control. A label is an object of

JLabel class.

Some of the constructors defined by JLabel class are as follows.

JLabel ()

JLabel(String string)

JLabel(String string, int align)

where,

string is the text used for the label

align specifies the horizontal alignment of the text contained in the label, and can have one of the following values: LEFT,RIGHT,CENTER,LEADING or TRAILING.

import java.awt.*;
import javax.swing.*;
public class JavaExampleLabelImageInJApplet extends JApplet
 {
   public void init()
    {
       Container Cntnr = getContentPane();
       JLabel Lbl = new JLabel("Label",new ImageIcon("Koala.jpg"),JLabel.CENTER);
       Lbl.setVerticalTextPosition(JLabel.BOTTOM);
       Lbl.setHorizontalTextPosition(JLabel.CENTER);
       Cntnr.add(Lbl);
  }
}
/*<APPLET CODE=JavaExampleLabelImageInJApplet.class WIDTH=510 HEIGHT=210></APPLET>*/

java - JLabel and Image within JApplet

Java Example for Join Thread

By Dinesh Thakur

[Read more…] about Java Example for Join Thread

SAXParser Example in Java

By Dinesh Thakur

[Read more…] about SAXParser Example in Java

xml parsers Java Example

By Dinesh Thakur

[Read more…] about xml parsers Java Example

Iterator Java Example

By Dinesh Thakur

[Read more…] about Iterator Java Example

Java isAlive() Example

By Dinesh Thakur

[Read more…] about Java isAlive() Example

Draw Image and ImageObserver in Java Applet

By Dinesh Thakur

 The Image class is used to load and display images. To load an image the getimage ()method of the Image class is used and to display the image the draw Image ()method of the Graphics class is used.

The general form of the getImage () method is

Image getimage(URL pathname, String filename)

Image getimage(URL pathname)

where,

pathname is the address of the image file on web. When the image file and the source file are in the same directory, getCodeBase ()method is used as first parameter to the method.

filename is the name of the image file

The general form of the drawimage ()method is

boolean drawimage(Image image, int startx, int starty,

int width, int height, ImageObserver img_obj)

where,

image is the image to be loaded in the applet.

startx is the pixels space from the left comer of the screen.

starty is the pixels space from the upper comer of the screen.

width is the width of the image.

height is the height of the image.

img_obj is object of the class that implements ImageObserver interface.

import java.awt.*; 
import java.applet.*;
/*<APPLET CODE=JavaExampleIObserverInApplet.class WIDTH=610 HEIGHT=160></APPLET>*/
public class JavaExampleIObserverInApplet extends Applet
 {
    Image img;
    public void init()
    {
        img = getImage(getDocumentBase(),"Koala.jpg");
    }
        public void paint(Graphics gr)
         {
           gr.drawImage(img,10,10,this);
         }
           public boolean imageUpdate(Image img1,int flags, int xAxis, int yAxis, int width, int height)
          {
            if ((flags & ALLBITS) != 0)
               {
                  repaint(xAxis,yAxis,width,height);      
                } 
                  return (flags & ALLBITS) == 0;
          }
 }

Draw Image and ImageObserver in Java Applet

JDesktopPane in Java Swing Example

By Dinesh Thakur

Many of today’s applications provide a facility wherein the users can open multiple documents without having to close current document. They can then switch between the documents and update them. For this purpose, applications use the multiple-document interface to manage these multiple open documents being processed in parallel (This is a main window, often called parent window, which contains several other windows, which also called child window.) JDesktopPane and JlnternalFrame classes of the Swing package help to create these multiple-document interfaces.

                       Constructors and methods in the JDesktopPane class.

 

Constructors and Methods

Description

JDesktopPane()

Constructs a new JDesktopPane object.

JlnternalFrame()

Constructs a new JlnternalFrame object, which is a non-resizable, non-closable, non-maximizable, non iconifiable internal frame.

JlnternalFrame(String title)

Constructs a new JlnternalFrame object with the specified title, which is non-resizable, non-maximizable, non- closable, non-iconifiable internal frame.

JInternalFrame(String title, boolean resizable)

Constructs a new JlnternalFrame object with the specified title and if resizable is set true, then it is resizable internal frame, which is non-maximizable, non-closable and non iconifiable.

JInternalFrame(String title, boolean resizeable, boolean closable)

Constructs a new JlnternalFrame object with the specified title and if resizable and closable are set true, then this internal frame is both resizable and closable but non maximizable and non-iconifiable.

JInternalFrame(String title, boolean resizable, boolean dosable, boolean maximizable)

Constructs a new JlnternalFrame object with the specified title and if resizable closable and maximizable are set true, then this internal frame is resizable, closable and maximizable but non-iconifiable.

JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)

Constructs a new JlnternalFrame object with the specified title and if resizable, closable, maximizable and iconifiable are set true, then this internal frame is resizable, closable, maximizable and iconifiable.

Container getContentPane()

Returns the content pane of this internal frame.

JMenuBar getJMenuBar()

Returns the current jMenuBar object for this internal frame or returns null if no menu bar is set.

String get Title()

Returns the title of this internal frame.

protected void paintComponent(Graphics g)

Paints the current component in internal frame.

Void remove(Component component)

Removes the specified component from this internal frame.

void reshape(int x, int y, int width, int height)

Moves and resizes the internal frame to the size specified by the parameters.

void show()

Displays the internal frame as well as brings it to front.

                                 

Program demonstrates the use of jDesktopPane.

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;
public class JavaExampleInternalFrameInAppletSwing extends JApplet implements ActionListener
 {
    JDesktopPane Dsktppn = new JDesktopPane();
    static int FrmNmbr = 1;
    public void init()
    {
        JPanel Pnl = new JPanel();
        Container Cntnr = getContentPane();
        JButton BtnFrme = new JButton("Click For Internal Frame");
        Pnl.add(BtnFrme);
        Cntnr.add(Pnl,BorderLayout.SOUTH);
        Cntnr.add(Dsktppn,BorderLayout.CENTER);
        BtnFrme.addActionListener(this);
    }
        public void actionPerformed(ActionEvent Evnt)
         {
            JInternalFrame Intrnlfrm = new JInternalFrame();
            Container Cntnr = Intrnlfrm.getContentPane();
            Intrnlfrm.setLocation(5,5);
            Intrnlfrm.setTitle("Internal Frame"+FrmNmbr);
            FrmNmbr++;
            Intrnlfrm.setClosable(true);
            Intrnlfrm.setResizable(true);
            Intrnlfrm.setMaximizable(true);
            Intrnlfrm.setIconifiable(true);
            Intrnlfrm.setVisible(true);
            Cntnr.setLayout(new FlowLayout());
            Cntnr.add(new JTextArea(5,10),"Center");
            Intrnlfrm.pack();
            Dsktppn.add(Intrnlfrm,2); 
        }
 }
/*<APPLET CODE =JavaExampleInternalFrameInAppletSwing.class WIDTH=370 HEIGHT=300></APPLET>*/

 JDesktopPane in Java Swing Example

Add Image to Button GUI Java

By Dinesh Thakur

[Read more…] about Add Image to Button GUI Java

Image Use in Applet Java Example

By Dinesh Thakur

Usually java.awt.Image class can be used to display GIF (Graphics Interchange Format) or JPEG (Joint Photographic Experts Group.) One way to get a image is using the getImage method available in the Toolkit object that encapsulates  specific methods of the platform used, as below:

Yet this class introduces some methods of interest, as listed below:

Method

Description

flush ()

Releases all resources used by a picture.

getGraphics ()

obtained a graphic context for off-screen rendering.

getHeight (ImageObserver)

obtained the image height is known.

getWidth (ImageObserver)

obtained the width of the image is known.

import java.awt.*; 
import java.applet.*;
public class JavaExampleImageUseInApplet extends Applet
{
    Image img;
    public void init()
    {
        img = getImage(getDocumentBase(),"Koala.jpg");
    }
        public void paint(Graphics gr)
         {
           gr.drawImage(img,45,15,this);
         }
}
/*<APPLET CODE=JavaExampleImageUseInApplet.class WIDTH=520 HEIGHT=170 ></APPLET>*/

Image Use in Applet Java Example

HashTable Java Example

By Dinesh Thakur

[Read more…] about HashTable Java Example

HashSet Java Example

By Dinesh Thakur

[Read more…] about HashSet Java Example

HashMap Java Example

By Dinesh Thakur

[Read more…] about HashMap Java Example

GrayScale in Java Example

By Dinesh Thakur

[Read more…] about GrayScale in Java Example

Find in Java Applet Example

By Dinesh Thakur

[Read more…] about Find in Java Applet Example

FileFilter Java Swing Example

By Dinesh Thakur

[Read more…] about FileFilter Java Swing Example

JFileChooser Java Swing Example

By Dinesh Thakur

Some operating systems have some special dialog boxes including selection to handle things like fonts, colors, printers and the like. Virtually all operating systems support the opening and storage of files, however, and thus the Java JFileChooser encapsulates this for easy use. JFileChooser, Opens a dialog box to ask for a filename.

For a dialog “open file”, called showOpenDialog (), and a dialogue of “save file” is called showSaveDialog (). these commands do not return until the dialog box is closed. the object JFileChooser still exists, so you can read the data. the getSelectedFile () method and GetCurrentDirectory () are two ways in which can examine the results of the operation. If these return null means that the user canceled the dialog.

Here is the example of JFileChooser

import java.awt.*;
import java.io.File;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
public class JavaExampleFileChooserInJavaSwing extends JFrame implements ActionListener
{
    JFileChooser Chsr = new JFileChooser();
    JButton BtnFileChsr = new JButton("Show File Chooser");
    JTextField Txt = new JTextField(25);
    public JavaExampleFileChooserInJavaSwing()
      {
           super("Example Of File Chooser In Java Swing");
           Container Cntnr = getContentPane();
           Cntnr.setLayout(new FlowLayout());
           Cntnr.add(BtnFileChsr);       
           Cntnr.add(Txt);       
           BtnFileChsr.addActionListener(this);
     }
          public void actionPerformed(ActionEvent e)
           {
              int Rslt = Chsr.showOpenDialog(null);
              File Flobj1 = Chsr.getSelectedFile();
              if(Rslt == JFileChooser.APPROVE_OPTION)
               {
                    Txt.setText("You Select " + Flobj1.getPath());
                   }
                    else if(Rslt == JFileChooser.CANCEL_OPTION)
                         {
                                 Txt.setText("You Pressed Cancel");
                             }
          }
                       public static void main(String ag[])
                        {
                             JFrame Frm = new JavaExampleFileChooserInJavaSwing();
                             Frm.setBounds(210,210,410,210);
                             Frm.setVisible(true);
                             Frm.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                             Frm.addWindowListener(new WindowAdapter()
                           {
                                      public void windowClosing(WindowEvent e1)
                                         {
                                             System.exit(0);   
                                         }
                                   });
                      }
 }

JFileChooser Java Swing Example

Emboss Java Applet Example

By Dinesh Thakur

[Read more…] about Emboss Java Applet Example

Java JEditorPane URL Example

By Dinesh Thakur

[Read more…] about Java JEditorPane URL Example

Java JEditorPane RTF Example

By Dinesh Thakur

[Read more…] about Java JEditorPane RTF Example

Java JEditorPane HTML Form Example

By Dinesh Thakur

[Read more…] about Java JEditorPane HTML Form Example

JEditorPane Example in Java Swing

By Dinesh Thakur

[Read more…] about JEditorPane Example in Java Swing

showInputDialog Java Swing Example

By Dinesh Thakur

An input dialog box is used to accept data from the user. It appears with a component like text field, combo box or list which lets the user to input the data. It is created by using the static method

showinputDialog () of JOptionPane.The general form of showinputDialog () method is

public static String showinputDialog(Component parentComponent,Object message)

where,

parentComponent is the parent component of the dialog

message is the message to be displayed

 

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;
/*<APPLET CODE =JavaExampleDialogTextInJApplet.class WIDTH = 350 HEIGHT = 280> </APPLET>*/
public class JavaExampleDialogTextInJApplet extends JApplet implements ActionListener
  {
      JButton BtnShw = new JButton("Show Dialog");
      String Msg = "Enter the Words";
      public void init()
       {
         Container Cntnr = getContentPane();
         Cntnr.setLayout(new FlowLayout());
         Cntnr.add(BtnShw);
         BtnShw.addActionListener(this);
       }
        public void actionPerformed(ActionEvent e1)
         {
           String Rslt = JOptionPane.showInputDialog(Msg);
           if(Rslt == null)
           showStatus("You Pressed Cancel");
           else
           showStatus("You typed : " + Rslt);
         }
   }

showInputDialog Java Swing Example

showMessageDialog Java Swing Example

By Dinesh Thakur

showMessageDialog Displays a modal dialog with a button labeled “OK”. It can be easily specify the message, icon, and title that will display the dialogue.

A message dialog box displays plain messages to the user and is created by using the static method showMessageDialog () of JOptionPane. The general form of showMessageDialog () method is:

 

public static void showMessageDialog(Component parent, Object message)

 

where,

 

parent is the parent frame to which dialog box is attached

 

message is the message to be displayed

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<APPLET CODE = JavaExampleDialogMessageInJApplet.class WIDTH = 370 HEIGHT = 300 ></APPLET>*/
public class JavaExampleDialogMessageInJApplet extends JApplet implements ActionListener
 {
     JButton BtnInfo = new JButton("Show Information Dialog");
     JButton BtnErr = new JButton("Show Error Dialog");
     JButton BtnWarn = new JButton("Show Warning Dialog");
      JButton BtnQues = new JButton("Show Question Dialog");
     JButton BtnPln = new JButton("Show Plain Dialog");
     public void init()
       {
         Container Cntnr = getContentPane();
         Cntnr.setLayout(new FlowLayout());
         Cntnr.add(BtnInfo);
         Cntnr.add(BtnErr);
         Cntnr.add(BtnWarn);
         Cntnr.add(BtnQues);
         Cntnr.add(BtnPln);
         BtnInfo.addActionListener(this);
         BtnErr.addActionListener(this);
         BtnWarn.addActionListener(this);
         BtnQues.addActionListener(this);
         BtnPln.addActionListener(this);
       }   
          public void actionPerformed(ActionEvent e1)
           {
                String DlgTitle = "Alert Dialogs";
                String DlgMsg = "Hi from Java!";
                int DlgType = JOptionPane.PLAIN_MESSAGE;
                if(e1.getSource() == BtnInfo)
                {
                      DlgType=JOptionPane.INFORMATION_MESSAGE;
                 }     
                 else if(e1.getSource() == BtnErr)
                             {
                                DlgType = JOptionPane.ERROR_MESSAGE;
                             }
                            else if(e1.getSource() == BtnWarn)
                                 {
                                        DlgType = JOptionPane.WARNING_MESSAGE;
                                     }
                                else if(e1.getSource() == BtnQues)
                                       {
                                                    DlgType = JOptionPane.QUESTION_MESSAGE;
                                               }
                                       else if(e1.getSource() == BtnPln)
                                            {
                                                             DlgType = JOptionPane.PLAIN_MESSAGE;
                                                        }  
                                                            JOptionPane.showMessageDialog((Component) null,DlgMsg,DlgTitle,DlgType);
           }
  }

showMessageDialog Java Swing Example

Java JDialog Box Input Example

By Dinesh Thakur

JDialog : Dialog boxes are JFrame restricted dependent on a main JFrame. The general JDialog dialog boxes are normally used for data requests.

A dialog box is a window that folds out another window. It is supposed to deal with a specific issue without cluttering the original window with these details. Dialog boxes are widely used in windowed programming environments, but less used in applets.
To create a dialog box, you inherit from JDialog, which is simply other window, like a JFrame. A JDialog has a handler path (which defaults to BorderLayout) and listener added events to treat events. A significant difference when is called windowClosing () is that you do not want to close the application. in Instead, the resources used by the dialog are released calling dispose ().
Once JDialog is created, the show () method must be called to deploy and activate it. For the dialog box is closed to Call dispose ().

The default layout for a dialog box is border layout. A dialog box is an object of the JDialog class which extends the AWT dialog java.awt.Dialog.JDialog uses a JRootPane as its container.

Some of the constructors defined by JDialog class are as follows:

JDialog ()

JDialog(Frame owner)

JDialog(Frame owner, boolean mode)

JDialog(Frame owner, String title)

JDialog(Frame owner, String title, boolean mode)

where,

owner is the parent frame of the dialog box created

mode specifies the mode of the dialog box as modal or non-modal

title is the title of the dialog box

A dialog box uses a content pane as container to add all the components. The general form of adding a component to the content pane is:

dialogBox.getContentPane().add(component);

There are two types of dialog box, namely, modal and non-modal.

• Modal dialog box: It is the dialog box that does not allow accessing other parts of application until it is closed. It is mainly used for displaying important messages, such as, “Do you really want to close the file?”, etc.

• Non-modal dialog box: It is the dialog box that allows accessing other parts of the application even when it is active. Another window can also be activated while this dialog box is being displayed.

Here’s a simple example:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<APPLET CODE = JavaExampleDialogInputInJApplet.class WIDTH = 370 HEIGHT = 300> </APPLET>*/
public class JavaExampleDialogInputInJApplet extends JApplet implements ActionListener
   {
      JLabel Lbl = new JLabel("Enter the Words");
      JTextField Txt = new JTextField(17);
      JButton BtnShw = new JButton("Show Dialog"),
      BtnOk = new JButton("OK"),
      BtnCncl = new JButton("Cancel");
      private JDialog Dlg = new JDialog((Frame) null,"Dialog", true);
      public void init()
          {
             Container cntnr = getContentPane();
             Container DlgCntnr = Dlg.getContentPane();
             cntnr.setLayout(new FlowLayout());
             cntnr.add(BtnShw);
             DlgCntnr.setLayout(new FlowLayout());
             DlgCntnr.add(Lbl);
             DlgCntnr.add(Txt);
             DlgCntnr.add(BtnOk);
             DlgCntnr.add(BtnCncl);
             BtnShw.addActionListener(this);
             BtnOk.addActionListener(this);
             BtnShw.addActionListener(this);
        }
            public void actionPerformed(ActionEvent e1)
            {
               if(e1.getSource() == BtnShw)
               {
                      Dlg.setBounds(220, 220, 220, 170);
                      Dlg.show();
                   }
                 else if(e1.getSource() == BtnOk)
                         {
                                showStatus(Txt.getText());
                                Dlg.dispose();
                             }
                                else if(e1.getSource() == BtnCncl)
                                 {
                                             showStatus("You Pressed Cancel");
                                             Dlg.dispose();
                                         }
         }
  }

Java Dialog Box Input Example

Confirm Dialog Box Java Swing Example

By Dinesh Thakur

Displays a modal dialog with two buttons labeled “Yes” and “No”.  These labels are not always terribly descriptive  program-specific actions that cause. 

import java.awt.*; 
import javax.swing.*;
import java.awt.event.*;
/*<APPLET CODE =JavaExampleDialogConfirmInApplet.class WIDTH = 370 HEIGHT = 300></APPLET>*/
public class JavaExampleDialogConfirmInApplet extends JApplet
  {
     JWindow Wndw = new JWindow();
     public void init()
      {
         final Container cntnr = getContentPane();
         JButton BtnShw = new JButton("Show The Dialog ");
         cntnr.setLayout(new FlowLayout());
         cntnr.add(BtnShw);
         BtnShw.addActionListener(new ActionListener()
           {
              public void actionPerformed(ActionEvent e1)
               {
                  int reslt = JOptionPane.showConfirmDialog((Component)null, "Press Yes or No", "Press Yes or No",JOptionPane.YES_NO_OPTION);               
                  if (reslt == JOptionPane.YES_OPTION)
                 {
                         showStatus("You Press Yes.");
                     }
                   else
                       {
                              showStatus("You Press No.");
                           }
                }
          });
       }
   }

Confirm Dialog Box Java Swing Example

PixelGrabber in Java Awt Example

By Dinesh Thakur

[Read more…] about PixelGrabber in Java Awt Example

Image in JCombox Java Swing Example

By Dinesh Thakur

[Read more…] about Image in JCombox Java Swing Example

Java – JComboBox Selection Change Listener Example

By Dinesh Thakur

A combo box is a combination of a list component and text field component. It can consist of more than one item, however, displays only one item at any point of time. It also allows user to type their selection. Unlike list component, combo box allows user to select only one item at a time. A combo box is an object of JComboBox class.

Some of the constructors defined by JComboBox class are as follows.

JComboBox ()

JComboBox(Object combodata[])

where,

combodata represents the array of Object type that displays the elements

 

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<APPLET CODE = ComboBoxEventsInJavaAppletSwing.class WIDTH = 320 HEIGHT = 220 ></APPLET>*/
public class ComboBoxEventsInJavaAppletSwing extends JApplet implements ItemListener
  {
    JComboBox CmbBx = new JComboBox();
    String OutStrng = " ";
    public void init()
      {
        Container cntnr = getContentPane();
        CmbBx.addItem("First Item");
        CmbBx.addItem("Second Item");
        CmbBx.addItem("Third Item");
        CmbBx.addItem("Fourth Item");
        CmbBx.addItem("Fifth Item");
        cntnr.setLayout(new FlowLayout());
        cntnr.add(CmbBx);
        CmbBx.addItemListener(this);
      }
        public void itemStateChanged(ItemEvent e1)
         {
            if(e1.getStateChange() == ItemEvent.SELECTED)
            OutStrng += "Selected: " + (String)e1.getItem();
            else
            OutStrng += "DeSelected: " + (String)e1.getItem();
            showStatus(OutStrng);
         }
   }

Java - JComboBox Selection Change Listener Example

« Previous Page
Next Page »

Primary Sidebar

Java Tutorials

Java Tutorials

  • Java - Home
  • Java - IDE
  • Java - Features
  • Java - History
  • Java - this Keyword
  • Java - Tokens
  • Java - Jump Statements
  • Java - Control Statements
  • Java - Literals
  • Java - Data Types
  • Java - Type Casting
  • Java - Constant
  • Java - Differences
  • Java - Keyword
  • Java - Static Keyword
  • Java - Variable Scope
  • Java - Identifiers
  • Java - Nested For Loop
  • Java - Vector
  • Java - Type Conversion Vs Casting
  • Java - Access Protection
  • Java - Implicit Type Conversion
  • Java - Type Casting
  • Java - Call by Value Vs Reference
  • Java - Collections
  • Java - Garbage Collection
  • Java - Scanner Class
  • Java - this Keyword
  • Java - Final Keyword
  • Java - Access Modifiers
  • Java - Design Patterns in Java

OOPS Concepts

  • Java - OOPS Concepts
  • Java - Characteristics of OOP
  • Java - OOPS Benefits
  • Java - Procedural Vs OOP's
  • Java - Polymorphism
  • Java - Encapsulation
  • Java - Multithreading
  • Java - Serialization

Java Operator & Types

  • Java - Operator
  • Java - Logical Operators
  • Java - Conditional Operator
  • Java - Assignment Operator
  • Java - Shift Operators
  • Java - Bitwise Complement Operator

Java Constructor & Types

  • Java - Constructor
  • Java - Copy Constructor
  • Java - String Constructors
  • Java - Parameterized Constructor

Java Array

  • Java - Array
  • Java - Accessing Array Elements
  • Java - ArrayList
  • Java - Passing Arrays to Methods
  • Java - Wrapper Class
  • Java - Singleton Class
  • Java - Access Specifiers
  • Java - Substring

Java Inheritance & Interfaces

  • Java - Inheritance
  • Java - Multilevel Inheritance
  • Java - Single Inheritance
  • Java - Abstract Class
  • Java - Abstraction
  • Java - Interfaces
  • Java - Extending Interfaces
  • Java - Method Overriding
  • Java - Method Overloading
  • Java - Super Keyword
  • Java - Multiple Inheritance

Exception Handling Tutorials

  • Java - Exception Handling
  • Java - Exception-Handling Advantages
  • Java - Final, Finally and Finalize

Data Structures

  • Java - Data Structures
  • Java - Bubble Sort

Advance Java

  • Java - Applet Life Cycle
  • Java - Applet Explaination
  • Java - Thread Model
  • Java - RMI Architecture
  • Java - Applet
  • Java - Swing Features
  • Java - Choice and list Control
  • Java - JFrame with Multiple JPanels
  • Java - Java Adapter Classes
  • Java - AWT Vs Swing
  • Java - Checkbox
  • Java - Byte Stream Classes
  • Java - Character Stream Classes
  • Java - Change Color of Applet
  • Java - Passing Parameters
  • Java - Html Applet Tag
  • Java - JComboBox
  • Java - CardLayout
  • Java - Keyboard Events
  • Java - Applet Run From CLI
  • Java - Applet Update Method
  • Java - Applet Display Methods
  • Java - Event Handling
  • Java - Scrollbar
  • Java - JFrame ContentPane Layout
  • Java - Class Rectangle
  • Java - Event Handling Model

Java programs

  • Java - Armstrong Number
  • Java - Program Structure
  • Java - Java Programs Types
  • Java - Font Class
  • Java - repaint()
  • Java - Thread Priority
  • Java - 1D Array
  • Java - 3x3 Matrix
  • Java - drawline()
  • Java - Prime Number Program
  • Java - Copy Data
  • Java - Calculate Area of Rectangle
  • Java - Strong Number Program
  • Java - Swap Elements of an Array
  • Java - Parameterized Constructor
  • Java - ActionListener
  • Java - Print Number
  • Java - Find Average Program
  • Java - Simple and Compound Interest
  • Java - Area of Rectangle
  • Java - Default Constructor Program
  • Java - Single Inheritance Program
  • Java - Array of Objects
  • Java - Passing 2D Array
  • Java - Compute the Bill
  • Java - BufferedReader Example
  • Java - Sum of First N Number
  • Java - Check Number
  • Java - Sum of Two 3x3 Matrices
  • Java - Calculate Circumference
  • Java - Perfect Number Program
  • Java - Factorial Program
  • Java - Reverse a String

Other Links

  • Java - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW