AIM :

To develop a Java Program using Split pane to demonstrate a screen divided into two parts, one part contains the names of planets and another displays the image of planet. When the user selects the planet name from left screen, appropriate image of planet is displayed in right screen.

 Download PDF here

SOFTWARE USED : 

            JAVA JDK 1.7

NetBeans 7.3.1

 DESCRIPTION :

The program uses SplitPane which divides the component into two horizontal parts one contains the list of planets and another contains a label.On selection of the planet from the list corresponding image of the planet should be displayed in label

Classess & methods used            

JSplitPane :

A JSplitPane displays two components, either side by side or one top of the other.

ListSelectionEvent :

An event that characterizes a change in selection. ListSelectionListener will generally query the source of the event for the new selected status of each potentially changed row.

ImageIcon:

An implementation of the Icon interface that paints Icons from Images.

Program

importjavax.swing.JLabel;

importjavax.swing.JList;

importjavax.swing.JFrame;

importjavax.swing.JSplitPane;

importjavax.swing.event.ListSelectionEvent;

importjavax.swing.event.ListSelectionListener;*/

importjava.awt.*;

importjavax.swing.*;

importjavax.swing.event.*;

public class SplitPaneDemo extends JFrame implements ListSelectionListener

{

JLabellblDisplay;

JListlstPlanets;

Container cp;

String []planets = {“Mercury”,”Venus”,”Earth”,”Mars”,”Jupiter”,”Saturn”,”Uranus”,”Neptune”,”Pluto”};

SplitPaneDemo()

{

super();

cp = this.getContentPane();

lstPlanets = new JList(planets);

lblDisplay = new JLabel(“”);

JSplitPanesplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,lstPlanets,lblDisplay);

cp.add(splitPane);

lstPlanets.addListSelectionListener(this);

}

public void valueChanged(ListSelectionEvent e)

{

if(lstPlanets.getSelectedIndex()==-1)

lblDisplay.setText(“No Planet Selected”);

else

{

String str = lstPlanets.getSelectedValue().toString();

lblDisplay.setIcon(new ImageIcon(str+”.jpg”));

}

}

public static void main(String[] args)

{

// TODO code application logic here

SplitPaneDemo s1 = new SplitPaneDemo();

s1.setVisible(true);

s1.setSize(500,300);

s1.setResizable(false);

}

}

 

Output on Top

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

©2023 MYBSCIT.com. An initiative by some failures to make student scholars.

or

Log in with your credentials

Forgot your details?