Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!

Rob Camick

+ Follow
since Jun 13, 2009
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 Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rob Camick

If the other forum where you posted this question you have been asked to post an "MRE" to demonstrate the problem.

If this forum you are asked to post an SSCCE.

Whether you call it an "MRE" or an "SSCCE" doesn't really matter.

The code you posted is NOT the problem.

Without a simple demo to see exactly what your code is doing we won't be able to help.
12 hours ago

Tim Holloway wrote:
I'm glad that there was a pop-up explanation of what "SSCCE" meant. Because it meant nothing to me.

Yes, the pop-up has been a feature of the forum for many moons

Now if only we can get Roger to read it. He was also asked in his last question to post a SSCCE:

The last question took 15 replies to resolve the issue. I'm not about to go down that path again.

A good question should have all the information we need to solve the problem in the original question which is why I believe a a "proper" SSCCE should be included with every question so we know the exact context of how the code is used and the problem is replicated.
15 hours ago

I thought I did

Actually you gave us very little details.

1. we don't know the context of how that code is used
2. we don't know where the value of the variables are used.
3. we don't know if you are dynamically adding the label to a visible GUI

The only way to give us all the details is to post a proper SSCCE.

So you create a JFrame with a JLabel and set the text of the label.

Then if is still doesn't work you have a complete 15 line program to post in the forum.
16 hours ago

I found another tutorial that helped me understand.

Not sure what you didn't understand about the working code presented in the tutorial.

It showed you how to identify the selected row when a row is clicked. Then you just use getValueAt(...) to get the data.

If there something you don't understand, then you ask a follow up question citing the text from the tutorial or the code from the demo that you don't understand.

I would like for it to select all of the rows in that column

Not really sure what you are asking. Your original question was about selecting a row. Now it seems to have changed to be about selecting a column.

Not sure why you have two for loops. If you click on a column then you should get an event.

Then you just loop through all the rows getting the value for the row and selected column. There is no need to know the selected rows, since you said you want the data from all the rows.

Also, if you simply assign the value for each row to a String, you will replace the data from the first row, with the data from the second row.

Post a proper SSCCE demonstrating the problem.

If you want both values then you would need to use a StringBuilder and append the data for each row. Or use an ArrayList to get the value for each row.
1 week ago

So here is where I am starting one line of code at a time.

I have no idea why you are attempting to add that line of code there. Where in the tutorial do they extend the JTable to add that line of code?

You are going about things backwards.

Download the working demo code from the tutorial and test the demo code.

Then use the demo code as your starting point and change one line of code at a time to implement your requirement.

That is learn by example and customize the example.
1 week ago
Read the section from the Swing tutorial on How to Write a ListSelectionListener for working examples.

1 week ago

well because i decided to do so on an image of a button instead of a jButton

Well, the computer doesn't know what you are thinking.

You got the label added when you clicked on the ImagePanel because you added the MouseListener to the ImagePanel.

So again I will ask, why should clicking on the Image do anything?

I suggest you read the Swing tutorial on How to Write a MouseListener for some basics.

The tutorial demonstrates how to generate mouse events on a components and then display the event in a text area.

So your code will be slightly different. You click on a component and add another component to a panel instead of displaying the text.
1 week ago

code works when I click at random, the second image looks good but does not work when I click on the first image button

So why should clicking on the first image button do anything?

1 week ago
And what component does "this" refer to?
1 week ago
There are multiple problems with the code:

1) You don't add the MouseListener to any component so your code is never executed.

2) You create an image, but you don't add it to the frame.

3) Your MouseListener doesn't have a reference to the "frame" or the "panel" so you have no way to add the image to the frame.

The coded needs to be restructured to make the "panel" an instance variable instead of a local variable, then your code in the MouseListener will be able to add the image to the panel.

4) The frame.setVisible(true)  should be invoked AFTER you add all the components to the frame.

5) There is no need to extend JFrame. The code in your main() method creates a JFrame instance.

Your code might look something like:

1 week ago

I don't know what it means 10 pix 10 char?

Did you use `getPreferredWidth()` to see what the default value is?

Based on the default value you should be able to determine what the integer value represents.

How can you say it doesn't work in you don't know what the value represents?
1 week ago

Why are you using "10"? What does that value mean to you?
1 week ago

I am on Linux Ubuntu, if that makes a difference.

It appears it does make a difference. I am using JDK11 on Windows 10.

It doesn't appear to be jerky or lag when painting. When I use 300 as the delay. The painting is at a constant interval and the entire frame refreshes at once.

I suggested a faster rate because I thought you might to trying to get more of a "flowing" effect from one generation to another which can only be achieved by a faster animation.

Is there an idiom in Swing for pausing the event loop?

You should be able to use stop/start for your pause/resume functionality.

I believe all the restart does is allow you to have an initial delay that is different from the ongoing delay. So yes you would use this if you want to clear the board and wait a short time before starting a new generation of events.

I remove that line, almost all the grid line disappear.

That is interesting. I have no idea why that would happen. The painting should be incremental. That is the grid lines should be drawn and then the live cells should be filled in.

So another suggestion is to keep the code that either draws the filled rectangle or empty rectangle and get rid of the code that draws the grid lines.

That is if you always paint every cell in a filled or empty state then there is no need for grid lines. That was only needed for your original approach where you painted background grid lines and then painted visible components on top of the grid.
2 weeks ago

there are noticeable pauses between each time the screen repaints)

That is because you have the "time to generations" set to 300. Try something like 15 to increase the animation.

You would also want to increase the "number of generation" to something like 200 to have the same elapsed time.

A couple other suggestions.

Even though you stop updating the panel after a fixed number of generations the Timer still fires events.

You can stop the Timer with code like:

Also in your painting code I question if you need:

since the drawing of the gridlines should paint the outline of each cell.

At least the painting looks the same to me when I remove the code.
2 weeks ago