Question
I'm using the java swing and barbecue library to create a barcode printer that displays the barcode generated on the gui. However, im not sure
I'm using the java swing and barbecue library to create a barcode printer that displays the barcode generated on the gui. However, im not sure why my code isnt working? When i clear the button the new image needs to be displayed but it doesnt however when i check the image folder i see the image does get updated and it clearly prints out that it does the method but is still not displaying ? Can someone help explain and show it how id fix the issue? I've included my code:
import java.awt.BorderLayout; import java.awt.Container;
import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel;
public class Main {
public static void main(String[] args) { // TODO Auto-generated method stub BarcodeFrame frame = new BarcodeFrame(); // JFrame f=new JFrame(); frame.setTitle("Barcode Generator"); frame.setSize(600, 400); frame.setResizable(true); // you can resize the tab frame.setVisible(true); // just repaint whenever you call the caluclate button }
}
import net.sourceforge.barbecue.BarcodeFactory; import net.sourceforge.barbecue.*;
import net.sourceforge.barbecue.BarcodeImageHandler; import javax.swing.*;
import java.io.*;
import java.awt.BorderLayout;
import java.awt.event.*;
public class BarcodeFrame extends JFrame implements ActionListener {
// public static void main (String [] args) throws Exception {
// Get 128B Barcode instance from the Factory
File imgFile = new File("testsize.png");
JLabel Input = new JLabel("User Input:"); JTextField Input1 = new JTextField(10); JRadioButton checkbox = new JRadioButton("Code 128"); JRadioButton checkbox1 = new JRadioButton("Code 39"); JButton calculate = new JButton("Generate barcode"); JButton clear = new JButton("Clear"); ButtonGroup group = new ButtonGroup(); JLabel imgL = new JLabel();
public BarcodeFrame() { System.out.println("crikey"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Image = new ImageIcon(getClass().getResource("")); calculate.addActionListener(this); clear.addActionListener(this); checkbox.addActionListener(this); checkbox1.addActionListener(this); JPanel frame = new JPanel(); frame.add(Input); frame.add(Input1); frame.add(calculate); frame.add(checkbox); frame.add(checkbox1); frame.add(clear); group.add(checkbox); group.add(checkbox1);
add(frame, BorderLayout.NORTH); }
void code128() throws Exception {
try { String hi = Input1.getText().toString(); System.out.println(hi); System.out.println("IM128"); Barcode barcode = BarcodeFactory.createCode128B(Input1.getText()); barcode.setBarHeight(60); barcode.setBarWidth(2); BarcodeImageHandler.savePNG(barcode, imgFile); displayBarCode(imgFile);
} catch (BarcodeException e) { e.printStackTrace(); }
}
void code39() throws Exception { System.out.println("39");
// only seems to work with number and capital letters???? String hi = Input1.getText().toString(); System.out.println(hi); try {
Barcode barcode = BarcodeFactory.createCode39(hi, true); barcode.setBarHeight(60); barcode.setBarWidth(2); BarcodeImageHandler.savePNG(barcode, imgFile); displayBarCode(imgFile); } catch (BarcodeException e) { e.printStackTrace(); }
}
void displayBarCode(File imgFile) {
System.out.println("kinda"); // JLabel imgL = new JLabel(); ImageIcon icon = new ImageIcon(imgFile.getAbsolutePath()); imgL.setIcon(icon); add(imgL); JPanel center = new JPanel(); center.add(imgL); System.out.println(imgL.getIcon()); add(center, BorderLayout.CENTER); System.out.println("kinda1"); // this.removeAll(); this.validate(); this.repaint(); //when i remove System.out.println(imgL.getIcon()); // imgL.setText(null); }
public void clear() { System.out.println("1"); this.removeAll(); System.out.println("2"); this.revalidate(); System.out.println("3"); this.repaint(); System.out.println("4"); }
@Override public void actionPerformed(ActionEvent event) {
if (event.getSource() == clear) { System.out.println("ami"); try { // clear(); Input1.setText(""); System.out.println("jhj"); imgL.setIcon(null); System.out.println(imgL.getIcon()); //repaint();
} catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
} // TODO Auto-generated method stub if (event.getSource() == calculate) {
System.out.println("lll"); String input; // input = Input1.getText(); if (input.isEmpty()) { System.out.println("nnooo"); Input1.setText("Error, can't be empty"); }
else if (checkbox.isSelected()) { System.out.println("HI I WOKED"); checkbox.setSelected(true); try { code128();
} catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (checkbox1.isSelected()) { try { checkbox1.setSelected(true); code39(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
} } } //
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started