sapdev logo background
sapdev logo sapdev logo
Comments

Example Swing code to display list of material numbers




Example code which creates a swing application for list of materials. The class takes an array of material numbers as its input (defined in the constructor) and then displays these materials as a drop down list. This class is not executable and must be used in conjunction with a class that creates an instance of it (i.e. CallFunction2 )


//package ws; //Replace with name of your package

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 SwingApplication extends JApplet implements ActionListener, ItemListener {
    private String labelPrefix = "Material:";
    private int numClicks = 0;
    Object type;
	ArrayList gdArrList = new ArrayList();

	public SwingApplication (){
	}
	
	public SwingApplication (ArrayList myArrList){	
//		labelPrefix = myArrList.get(1).toString();
//        if (myArrList != null){
		gdArrList = myArrList;
//      }
	}
	
	public SwingApplication (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);

        /*
         * An easy way to put space between a top-level container
         * and its contents is to put the contents in a JPanel
         * that has an "empty" border.
         */

        pane.add(button);
        pane.add(label);
		label.setText("Material:");
		pane.add(label);
		
		return pane;
    }

	public void itemStateChanged(ItemEvent ie) {

	//X = ie.getStateChange();
	type = ie.getItem();

	}

	public void actionPerformed(ActionEvent ea) {
	}
}





comments powered by Disqus