Java class to create Swing Application from Array of data

Java Class code to create Swing applet.


package ws;   //Comment out or replace with own package declaration
import javax.swing.*;          //This is the final package name.
//import com.sun.java.swing.*; //Used by JDK 1.2 Beta 4 and all
                               //Swing releases before Swing 1.1 Beta 3.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class CallSapSwing extends JApplet implements ActionListener, ItemListener {
    private String labelPrefix = "Material:";
    private int numClicks = 0;
    Object type;
	ArrayList gdArrList = new ArrayList();
	public CallSapSwing (){
	}
	
	public CallSapSwing (ArrayList myArrList){	
		gdArrList = myArrList;
	}
	
	public CallSapSwing (String val){	
		labelPrefix = val;
	}
	
    public Component createComponents() {
        final JLabel label = new JLabel(labelPrefix + "0    ");
		final JLabel label2 = new JLabel(labelPrefix + "1    ");
		String text;
		JPanel pane = new JPanel();
		pane.setBorder(BorderFactory.createEmptyBorder(
							30, //top
							30, //left
							10, //bottom
							30) //right
								);
		pane.setLayout(new GridLayout(0, 1));
		
		type = gdArrList.get(0).toString();  //Set default
		Choice conversion = new Choice();
		{
			for (int loop = 0; loop < gdArrList.size(); loop++){
				conversion.add(gdArrList.get(loop).toString());
				conversion.addItemListener(this);
			}
		}
				
		pane.add(conversion);
	
		JButton button = new JButton("Display Material");
        button.setMnemonic(KeyEvent.VK_I);
        button.addActionListener(new ActionListener() {
			
            public void actionPerformed(ActionEvent e) {
                numClicks++;
               label.setText( labelPrefix + type);
            }
        });
        label.setLabelFor(button);
        pane.add(button);
        pane.add(label);
		label.setText("Material:");
		pane.add(label);
		
		return pane;
    }
	public void itemStateChanged(ItemEvent ie) {
	type = ie.getItem();
	}
	public void actionPerformed(ActionEvent ea) {
	}
}


Related Articles

Using the Java connector (JCo) to retrieve data from an SAP system
Example JCo code to call SAP function module
Call an SAP function module and display data in a swing application
Applet to retrieve data from SAP(using JCo) and display it using Swing
Application to retrieve data from SAP(using JCo) and display it using Swing
Example Java code to create Applet
Example JCo code to call SAP function module
Example JCo code to call SAP function module
Example Swing code to display list of material numbers