Displaying a barcode on a form

The dBarcodeXX classes extend the Canvas, and so can be placed on other visible elements, such as a frame. For example, the following simple program creates a frame containing a label and a barcode, and displays the barcode using the data “123456”.

Because the barcode has been added to a visible element it automatically receives “Paint” messages, and displays itself.

 

Public class test implements Printable{

JFrame pane;

JLabel dlabel;

dBarcodeU barcode=new dBarcodeU();

 

public testU(){

pane= new JFrame(" Frame title - Example");

pane.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pane.setSize(new Dimension(640,480));

pane.getContentPane().setLayout(null);

// place a text label on the form

dlabel = new JLabel("This is a barcode");

size=dlabel.getPreferredSize();

pane.getContentPane().add(dlabel);

dlabel.setBounds(50,50,size.width,size.height);

// place a barcode on the form

pane.getContentPane().add(barcode);

bcard.setLocation(100,100);

pane.setVisible(true);

// draw the barcode

doBarcode();

 }

 

private static void createAndShowGUI() {

//Make sure we have nice window decorations.

JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.

test w=new test();

}

 

public static void main(String[] args) {

//Schedule a job for the event-dispatching thread:

//creating and showing this application's GUI.

javax.swing.SwingUtilities.invokeLater(new Runnable() {

      public void run() {

      createAndShowGUI();

      }

   });

}

 

    public void doBarcode(){

// this function sets the barcode properties and then draws it

        Font dFont=new Font(“Arial”, Font.PLAIN, 10);

        bcard.setFont(dFont);

        bcard.setCodeType(0);

        bcard.setData(“123456”);

        bcard.setUnits(“mm”);

        bcard.setdWidth(40.0);

        bcard.setdHeight(20.0);

        bcard.setShowText(1);

        bcard.setIndicators(1);

        bcard.setScreenScale(96);

    }

   

    // Exit the Application

    private void exitForm(java.awt.event.WindowEvent evt) {

        System.exit(0);

    }

}

 

More:

Printing a barcode image