• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create and display BARCODE  RSS feed

 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello people,

Given my beginner status on java, I can ask you to have your help on a java code that allows to create and display a bar code according to the value entered beforehand.

I started from a developed code on a forum. Unfortunately I have a problem with the code that I can not understand. The code works correctly. However, when the value entered is changed (Example for this code at the origin: nb or entry = "9780130515186") -> the value displayed on the code changes the reading of the bar code via a code reader Bar (for example, a phone application) does not work. However, when you keep the original value of the code, the player is working.

Can you help me find a solution to enter any value and read it through a barcode reader.

Thank you in advance.

Sorry to put the code below. attachments doesn't worked.

---------------------------






 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I added code tags to your post: always use the tags. Doesn't it look better (). We prefer code inside tags to attachments. There are some formatting things: the class name and each import shou‍ld be on its own line. You also appear to have missed out part of the applet class, so what you have shown won't compile. What does the array called T mean? T is a bad name for anything (except a formal type parameter), and it doesn't give us any information about what the array means. You don't appear to be using it anyway.

Why are you overriding paint() in the panel rather than paintComponent()?
There is something wrong with writing applets. Nobody else uses them any more and they are regarded as completely obsolete. Browsers won't open them any more. I would suggest you use a JFrame instead.
It is difficult to be sure because you haven't shown us your action performed method, which I think shou‍ld not be in the panel, but in a class of its own. Not knowing what happens when you click a button (are there any buttons?) we cannot tell how you are changing the barcode and how the display is supposed to know when it has changed. I think we can only be certain if you provide more code.
I don't like having the CodeBarre class mutable. Why shou‍ld it change its state like that? And as I said, if it does change state, is there any way for the panel to know about the changes? Only if the panel “nows” about the change can the new barcode be displayed.
 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell,

Thanks for your quick response and for added code tags.

Concerning applet class, I removed some unnecessary functions from the start code. I don't use the button on this program, just click on the cross of the window to close the application which has been generated.
In spite of all this, the program works correctly and compiles. You can try and tell me what you have as a result. However, the table T was used and I do not understand exactly what it does because I went on a program found on the internet. What I know is that if I remove it from my code I have errors.

What you mean by paintComponent()?

I used JFrame in my CodeBarre class. Please refer the code source.

As I said before I'm a beginner in Java, that's why I want your help. I post you below the complete program or code. Sorry if my english isn't clear, I'm french



















 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chrys Karl wrote:. . . . However, the table T was used and I do not understand exactly what it does because I went on a program found on the internet.
I have now found where it was used, and my apologies for that mistake.
Unfortunately copying programs from the Net which come without explanation is a good way to confuse yourself. You also do not know whether those programs are any good. Please always tell us where such programs come from.
. . . What you mean by paintComponent()?
It is a method in the JPanel class. (Actually JComponent class.)
I used JFrame in my CodeBarre class. Please refer the code source. . . .
So why does CodeBarre extend JApplet? Is that simply because that is what they used on the other website?

Now that we have the complete code, yes it does compile, and it does run, and it does change the barcode on screen.
But not via any mouse listener that I can see. You have added a mouse listener to the panel, but that doesn't signal changes. All it appears to do is to cause the panel to be repainted without changing the barcode.
What you also have is an action listener added to the text field. Since there is no button you will have to find another way to signal changes. A text field will fire an action event if the enter key is pressed. Try putting a new number into the text field and pushing the enter key. It must have at least 13 digits in.

I could say a lot about that code; I suspect it is old code and it has much in which we would regard as bad design. Did you find that code here?
 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand your reluctance on this code and I share your opinion. But I used this code because I started on java and in the meantime I understood a bit the source code that I had found on the internet until I was blocked on the display.

I will start all over again, but I wanted to know if it is possible to have your help. Do you have an idea about a source code?

Because the purpose of my code would be to display a bar code according to a value entered beforehand or a value read on a text file.

Thank you for your help.
 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forget about Roux's code. Write your own and avoid Roux's poor style.
Start by creating a barcode class. Give it a number field. Only that number has to have 13 digits in and might start with 0 so you cannot use an int or a long or a BigInteger. You will have to use a String. Make the field final. That means you will have to set the field via the constructor, so only write one constructor. If you make the class final, you will have an immutable class, which is useful. You are allowed getXXX methods but not setXXX methods in an immutable class. You may hear about defensive copies, which are often a good idea, but they are not necessary for Strings because they are themselves immutable.
Once you have got that done, we can consider how to validate the input and how to convert the number to a barcode. Also whether to override the methods of Object. One step at a time. You have enough there to keep you busy for at least twenty minutes
 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell,

Thanks for your time and for your help.

I could start the code and follow your recommendations. The code below makes it possible to enter any value of 13 digits.
You will find below the source code. What is your opinion on this code? Could I contemplate conversion right now?


 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chrys Karl wrote:. . . Could I contemplate conversion right now?. . .
Afraid not. Forget about entry of data, and about Scanners. Create a barcode class which takes the numbers ready‑made as constructor parameters. I think you will have to create a temporary barcode tester class like this:-You will have to override the toString method in order to get that to work. Make it display something like "BarCode:1234567890123".
Then enhance the testing class to use the command line arguments.In order to test that, you will have to pass command line arguments:-
java BarCodeTester 1234567890123 0987654321987 56789320928374 987654324567 abcdefghijklm etc
Note those entries don't all have 13 characters.

Your code will give error messages if the entry is not 13 characters long, but you will still create an instance. If you really want to validate things you shou‍ld consider throwing an IllegalArgumentException for incorrect input. As well as testing the length of the Strings, you can find out whether they contain all letters by using the technique shown here. You will have to change it slightly; you can add testing for length 13 to the same method.

Implement that and see how you get on with it.
 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell,

Happy New Year, I wish you all the best for this new year. Success in all your businesses.

Concerning my project, I apologize for my delay. That said, I have not advanced so far because I am stuck on the code.

I have doubts about the code below. After executing I have the value of my Barcode which appears as entered in the code. But I have errors while keeping your line of code. : "System.out.println(new BarCode("1234567890123"));". Where is the problem? Please help me to move forward.






 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chrys Karl wrote:Hello Campbell,

Happy New Year, I wish you all the best for this new year. Success in all your businesses.
Thank you Same to you.
. . . But I have errors while keeping your line of code. : "System.out.println(new BarCode("1234567890123"));". Where is the problem? . . .
Don't know. You can pass absolutely anything to System.out.println() without problems, as long as you haven't managed to close System.out. Please tell us more details about the errors, then we can work out what is going wrong.
Also remember that System.out.println(new Barcode(...)); is a temporary bit of code so you can see the program is working at the moment; you will probably remove it later.
 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell,

Here is what I have as error when I add: "System.out.println(new BarCode("1234567890123"));"

ERROR:


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
BarCode cannot be resolved to a type

at BarCodeTester.test(BarCodeTester.java:23)
at BarCodeTester.main(BarCodeTester.java:12)



 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this FAQ. You shou‍ld find that Eclipse has put a red mark against the offending line on the left, and you shou‍ld correct that before you try to run the code. If you click on the red mark, you will probably be given a list of possible corrections. Where is the BarCode class? Have you imported it? Is it spelt exactly like that: BarCode? Have you got a field with the same name as the class? Fields shou‍ld not start with CapitalLetters.
 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Campbell,

I have small problem on my code. I created the BarCode class (for the Test part). Unfortunately, when I run the program I have this which would appear on the consol : BarCode@15db9742
Have I forgotten a few things?

 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is normal if you don't override the toString method. Consider whether you need to override it. Maybe simply something like this:-That way you will be able to see that the text has been accepted correctly when you can't see the lines. Once you are using a GUI, you probably won't call the toString method again.
 
Chrys Karl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

I tried adding your proposal but I still have mistakes.I think being a beginner does not help me.
I'll give you my code. Where is the problem near you?

Thanks for your help.



 
Campbell Ritchie
Sheriff
Posts: 55364
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the toString() method in the BarCode class, and change the name inputText to match the name of your field.
Creating classes shou‍ld be a basic skill. Don't you know how to create a class with methods overridden from Object? Do you have an introductory book for Java®? Find a copy of Head First Java by Sierra and Bates. It might be old, but it is still relevant, and used copies are readily available at good prices.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!