Practical 6: Write a program using swing to display a ScrollPane and JcomboBox in an Japplet with the items - English, Marathi, Hindi, Sanskrit.

1) Write a program code to generate the following output
Jcombobox in Swing

Ans:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class JComboBoxDemo extends JApplet implements ItemListener
{
    JLabel JLabelObj ;

    public void init()
    {
        setLayout(new FlowLayout());
        setSize(400, 400);
        setVisible(true);
        JComboBox JComboBoxObj = new JComboBox();
    
        JComboBoxObj.addItem("Solapur");
        JComboBoxObj.addItem("Pune");
        JComboBoxObj.addItem("Banglore");
        JComboBoxObj.addItem("Mumbai");
        JComboBoxObj.addItemListener(this);

        JLabelObj = new JLabel();
   
        add(JComboBoxObj);
        add(JLabelObj);
    }

    public void itemStateChanged(ItemEvent ie)
    {
        String stateName = (String) ie.getItem();

        JLabelObj.setText("You are in "+stateName);
    }

}
/*   */



2) Write a program to develop a frame to select the different states of India using JComboBox
Ans:
import javax.swing.*;
import java.awt.*;

public class DifferentState 
{
    public static void main(String args[])
    {
        JFrame JFrameMain = new JFrame();
        
        JFrameMain.setVisible(true);
        JFrameMain.setSize(400,400);
        JFrameMain.setLayout(new GridLayout(3,3));
        String states[] = {"Solapur","Pune","Mumbai","Banglore"};
        JComboBox JComboBoxStates = new JComboBox(states); 

        JFrameMain.add(JComboBoxStates);
    }

}


Output:
Different State using JComboBox

3) Develop a program to demonstrate the use of ScrollPane in Swings
Ans:
import javax.swing.*;
import java.awt.*;

public class JScrollPaneDemo 
{
        public static void main(String[] args)
        {
           /* Container contentPane = getContentPane();
            contentPane.setLayout(new BorderLayout());
            */
            
            JFrame JFrameMain = new JFrame();
            JFrameMain.setVisible(true);
            JFrameMain.setSize(400,400);
        

            JPanel JPanelButton = new JPanel();
            JPanelButton.setLayout(new GridLayout(20,10));
            for( int i = 1 ; i <= 200 ; i++ )
            {
                String s = "";
                s = s.valueOf(i);
                JPanelButton.add(new JButton( s ) );
            }

            int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
            int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED ;

            JScrollPane JScrollPaneObj = new JScrollPane(JPanelButton , v,h);
            

            JFrameMain.add(JScrollPaneObj , BorderLayout.CENTER);
            
        }
}

Output:
JScrollPane in Java Swing

Previous
Next Post »