Share |

Call an SAP function module and display data in a swing application


Example code which calls an SAP function module 'BAPI_MATERIAL_GETLIST' with inport parameters and retrieves the resultant data. It then displays the list of materials using a swing application, the code for which will also be required (Swing code ).


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

import com.sap.mw.jco.*;

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 CallFunction2 extends Object {
	public ArrayList myArrList = new ArrayList();   //List is super class could use ArrayList		
	//
	int count;
	String[] SAPInterfaces;
	JCO.Client mConnection;
	JCO.Repository mRepository;
	public CallFunction2(){
		try {
			// Logon info
				mConnection = JCO.createClient("500", // SAP client
	 	                                    "username", // userid
		                                     "password", // password
		                                        null, // language
		                               "server name", // application server host name
	                                           "00"); // system number
			mConnection.connect();
			mRepository = new JCO.Repository("ARAsoft", mConnection);
		} catch (Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}

		JCO.Function function = null;
		JCO.Table codes = null;
		JCO.Table codes2 = null;

		try {
			//			function = this.createFunction("BAPI_COMPANYCODE_GETLIST");
			function = this.createFunction("BAPI_MATERIAL_GETLIST");
			//			function = this.createFunction("BAPI_MATERIAL_GET_DETAIL");

			if (function == null) {
				System.out.println(
					"BAPI_MATERIAL_GETLIST" + " not found in SAP.");
				System.exit(1);
			}

			codes = function.getTableParameterList().getTable("MATNRSELECTION");

			codes.appendRows(2);   // Add two rows to internal table
			codes.setValue("I", "SIGN");
			codes.setValue("EQ", "OPTION");
			codes.setValue("P1001087", "MATNR_LOW");			

			codes.nextRow();      // Move onto next row
			codes.setValue("I", "SIGN");
			codes.setValue("BT", "OPTION");
			codes.setValue("P1001087", "MATNR_LOW");	
			codes.setValue("P1009999 ", "MATNR_HIGH");			

			mConnection.execute(function);

//          Output result Material list table
			System.out.println(
				function.getTableParameterList().getValue("MATNRLIST"));
			System.out.println(
				function.getTableParameterList().getString("MATNRLIST"));

			codes2 = function.getTableParameterList().getTable("MATNRLIST");
			
			for (int loop = 0; loop < codes2.getNumRows(); loop++){
				myArrList.add(codes2.getValue("MATERIAL"));
				codes2.nextRow();
			}

			System.out.println(	myArrList);
			if (myArrList.isEmpty()) {
				System.out.println("No data retirved for selection criteria");	
				System.exit(1);
			}

//          Output Selection table
			System.out.println(
				function.getTableParameterList().getValue("MATNRSELECTION"));
			System.out.println(
				function.getTableParameterList().getString("MATNRSELECTION"));
			
		} catch (Exception ex) {
			ex.printStackTrace();
			System.exit(1);
		}
	}

	public JCO.Function createFunction(String name) throws Exception {
		try {
			IFunctionTemplate ft =
				mRepository.getFunctionTemplate(name.toUpperCase());
			if (ft == null)
				return null;
			return ft.getFunction();
		} catch (Exception ex) {
			throw new Exception("Problem retrieving JCO.Function object.");
		}
	}

	public static void main(String args[]) {
		String labelPrefix = "Number of button clicks: ";
		
 		CallFunction2 app = new CallFunction2();
		
		//Create the top-level container and add contents to it.
		try {
//      Set Look and feel of gui box 			
		UIManager.setLookAndFeel(
//		UIManager.getCrossPlatformLookAndFeelClassName());      // Default???
		"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");	// Windows look
//		"javax.swing.plaf.metal.MetalLookAndFeel");     		// Java look
	} catch (Exception e) { }

//  Create the top-level container and add contents to it.
	JFrame frame = new JFrame("SwingApplication");

//  Create instance of SwingApplication (Passes Array of materials)
	SwingApplication swingapp = new SwingApplication(app.myArrList);
	Component contents = swingapp.createComponents();
	frame.getContentPane().add(contents, BorderLayout.CENTER);

//  Finish setting up the frame, and show it.
	frame.pack();
	frame.setVisible(true);
	}
}
}