Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link

Vincent Hendriks

+ Follow
since Oct 19, 2010
The Netherlands
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Vincent Hendriks

Thanks Henry, I'll look into that
11 years ago
Hi guys,

I'm puzzled...
I made my java programme, put it in a .jar and it works.
Took it to my work, put it on the computer (network drive) and it works on my work-PC.
Wanted to show it to my colleagues on their computer and it says "Could not find main class. Program will exit"???

What am I doing wrong??? (I checked for JRE: same version as my work-PC).
11 years ago
Thanks both Jason and Lester for your reply.

Jason's suggestion is a bit out of my league at the moment (much stuff unknown to me). I'll have a look at it though: I'l consider it an opportunity to expand my API knowledge
I'll also start on Lester's suggestion. I'm looking for a format to import and export, but not necessarily .csv. Maybe there are better options :-)

So, it's back to the drawing board ( head hurts enough for today )

11 years ago
Hi Guys,

I have a problem. Hope you can help me.
I am building an faq program with an arraylist of objects (each with variables Question, Answer and Type).
I'm exporting this arraylist to a .csv-file, wich goes OK.

This is the problem:

Since Questions and Answers can contain semicolons (separators for the .csv), I have had to place the Questions and Answers in quotes (' " ' in lines 34 and 35) to make sure everything is displayed correct in Excel.
These quotes don't appear in the excel-sheet , but when I import the same file again, two problems arise:

1 the quotes are made part of the Questions/Answers and
2 when I have semicolons in the Questions/Answers, they screw up the split-method and for instance my Question is half in the question field/half in the answer field and my answer is nowhere to be seen

Is there a way to solve these problems inside my import-/exportcode (I don't want to mess with the windows-separator value in controlpanel)?

These are my import and export methods. Any help/tips are appreciated!

11 years ago
Mohamed, I succeeded with setting the different ActionListeners too

Thanks again!!!
11 years ago
Hi Mohamed,

For- Number 2- You can remove all the buttons from the panel (If the panel contains only buttons, you can remove all the components from the panel) and then add the buttons- You are looping through each FAQ and adding the corresponding button?

Exactly! I added and now it works like a charm
validate() didn't do what I thought it would do...

For Number-3- If you want to access the FAQ Class- You can create a Custom Action listener which Implements ActionListener. Along with that you can have the reference to the current FAQ Class for which the instance of this was created and you can access in the ActionPerformed.

Ahh... so instead of customizing the button itself, I have to customize the ActionListener...
That's usefull
I'll start working on that.

Thanks Mohamed!
11 years ago

I'm trying to build a little program to manage an FAQ, that can basically:

1 store a question (text1) and an answer (text2)
2 make a button in my gui that has the question on it
3 copy the answer to the clipboard when I click the button

Number 1 goes OK. I have a question about number 2 and 3:

Number 2: when I add a new question and answer combinaton (fiche), I validate the panel in which the buttons are displayed to make the new button visible.
However, it add's not only the new button, but also the buttons that were already there!
So when I have one button A displayed and add a new one B, it doesn't refresh to A B, but to A AB. After adding a third button, it's not A B C, but A AB ABC. So it add's them up!
Clearly I'm making a mistake here, but I can't find it

Number 3: this is more of a general question: while building, I came to the conclusion that I can't copy the answer to the clipboard because the answer variable is in another class (Fiche) than the one the code is running in (TestFrame/ActionListener class in TestFrame). How do I solve this?
Is it possible to include the ActionListener in the constructor of the (J)Button so that I can attach the answer as a parameter while creating a new button (like it does with the question now)?
Or does the answer lie in another direction?
(I already have a working code to copy the content of a JTextArea to the clipboard: I haven't displayed it in my code yet because of the problem: I can't access the right variable yet)

Thanks for any help !

This is my code (it compiles and runs):

11 years ago
OK. Good to know that stuff like that is around

I'll try and find something that works for me.

11 years ago

Since I recently started learning Java, I'm compiling and running my programs with two batchfiles (because actually working from the command line is not optimal).

I need the compiling batch-file because the window closes so fast after compiling, I can't read the compiler errors (hence the pause!):

@echo off

I need the running batch-file because just double-clicking my class file doesn't work:

@echo off
java fileName

Although this works, it's a bit annoying that I have to change the batch-files everytime I want to compile or run another class...

I'm a bit hesitant though about downloading programming software without knowing what it actually can do...
I don't want to use a big "suite-like" program because the fun for me is not so much in making a big fancy program but in writing the code itself!

Therefore the question: is there another way/a program to do this?
What I could really use is just a code-editor with a compile and a run button and a frame to display compiling- (and yes, I'm a newbie, runtime-) errors (and without fancy extra's, options, etc.)
11 years ago

Yes, I do see one problem and have a few suggestions:
1) Your MyDrawPanel class's paintComponent method should probably not have that fillRect method call but instead should have a call to super.paintComponent(g). This will allow the underlying JPanel code to repaint itself and erase any previously rendered images.
2) If you are going to want your geometric images to look nice, in the paintCompnent method create a Graphics2D object by casting the Graphics object to Graphics2D and then call setRenderingHints(...) to turn on anti-aliasing. The constants to use for this are in the RenderingHints API, and the method definition is in the Graphics2D API. This will smooth out the "jaggies" in your oval.
3) I avoid calling setSize(...) or doing anything that sets absolute size or position of a JFrame or component. Rather, consider using setPreferredSize and call pack on the JFrame before setting it visible. I usually call pack, then setLocationRelativeTo(null), then setVisible(true)

Those are all sort of API related tips. As a beginner, I think I have to gradually expand my knowledge of the API, which is limited to only a few classes/methods right now.
I've only had a few glimpses in it until now but from what I've seen I think in a certain way it performs the function that a dictionary has in a "normal" language (well, not exactly, but sort of ).
I trust that knowledge of the API will come with experience.
For now, I'm starting Head First chapter 14 (about Swing). If there are Graphics in the code that is treated, I'll remember your tips

Thanks Pete!

@ Campbell Ritchie: I'll try to avoid the long stuff. Thanks for correcting.
11 years ago

Yep, you are trying to call the method from the class when you should be trying to call the method from an object of the class. And in fact, your TwoButtons class needs to have a MyDrawPanel variable that is initialized, displayed in the GUI, and used in the actionPerformed method for the method call that we're talking about.

Pete, I succeeded in writing a code that works!!!

I did what you said (I think) and to proove I actually understand it, I also randomized the text on the label.

Then, when that also worked (took me about an hour and a half, but definitely an hour and a half well spent), I compiled and ran my programme about 10 times and kept clicking the buttons.

(where's a proud-as-a-peacock Emoticon when you need one )

Would you mind taking a last look to see if I accidentally violated any coding conventions or best practices?

11 years ago

No you didn't. You made a static variable which you shouldn't do as it is also forcing you to make the changeColor method static. Do you understand why it is important for these to be true instance variables? For one thing, there's no "this" in a static method, so there's no way you can tell the JPanel to repaint from within the method as I have suggested.

Actually, I had both variable and method not static first, but then the compiler told me that in line 42...

...the non-static method changeColor() could not be referenced from a static context...
I guess I reacted in the opposite direction by making the method (and therefore also the variable) static to please the compiler

...but a basic OOPs programming issue.

Haha, yep, but I'll just look at it from a positive point of view: the more OO mistakes I make in the beginning, the steeper the learning curve should become
I'm calling it a day now, tomorrow I'll have a fresh look.


11 years ago
I'm sorry Pete... I'm afraid, I'm gonna need a little more help on this one (it's a big leap from reading and understanding code to actually writing code from scratch).

I've taken the random part out of paintComponent and created changeColor, which I'm calling under the "change-the-color-of-the-circle-button".
Because the circleColor should be available to both methods, I made an instance variable of it.

It's not working though and I keep struggling with assigning a circleColor in paintComponent when it's painted initially and not when it's only changed because of the button event.

Can you point out the flaw in lines 48-64 (apart from the fact that this code is not doing the trick )?

11 years ago

Suhrid Karthik: Alternatively you could use a flag variable to indicate that the panel must be repainted only when the user clicks on the change color button.

Unfortunately, that didn't work, since the thing was repainted everytime because the size of the WEST part of the panel changed due to the change of length of the text on the label. I didn't realise that in the beginning.

pete stein: The problem as I see it is with the code you're not showing us. I have a feeling that your MyDrawPanel component changes a circle's color whenever it is redrawn -- possibly whenever it's paintComponent method is called, and by changing the size of the JLabel, you're making the JFrame and its components repaint themselves. The solution is not to change color in paintComponent as you do not have full control on when this is called, but rather elsewhere. Give your MyDrawPanel a public method called changeColor() where it changes the color of the circle and then call repaint() on the MyDrawPanel.

I've tried a bunch of things and it didn't work out. I'm not familiar enough yet with the properties of JPanel, paintComponent(), changeColor(), Graphics etc. etc.
I'm still learning though, so hopefully one day ...

In the end I smuggled a bit and switched the SOUTH and WEST parts so that the size of the parts would not be affected by the text on the label
Both buttons worked correct then!
So, I didn't exactly solve the problem, but I understand where, why and how it went wrong. With that, my question is answered, so thanks to both of you!
11 years ago
Hi Pete,

I see what you mean and you are indeed correct. I'm not sure on how I should proceed though. This is the missing code (sorry about that...):

I don't think I can just toss out the paintComponent() because the program wouldn't make a circle to change the color of
As you said, I can add a changeColor(), but when I call repaint() on the MyDrawPanel, how does it know that it should execute the changeColor() instead of the paintComponent()?
11 years ago