Rob Camick

Rancher
+ Follow
since Jun 13, 2009
Cows and Likes
Cows
Total received
30
In last 30 days
0
Total given
0
Likes
Total received
680
Received in last 30 days
0
Total given
31
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Rancher Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Rob Camick

Is it possible in Java to create an empty JTable


Use the DefaultTableModel to create an empty JTable.

Then you use the addRow(...) method to add rows of data as required.

After I entered all of the employee data it has to export to a .CSV file


Then you iterate through the TableModel using the getValueAt(....) method and write the data to a file in any format you desire.
3 months ago


Swing components have a parent / child relationship.

If you want components to overlap then you add the child component to the parent component.


Yes I want background then in the center, I put a panel.



The parent component should use a layout manager to position the child are required. In this case the easiest way is to use a GridBagLayout.

So the basic code might be something like:




See: Background Panel for a more flexible background component.
3 months ago

what am i missing from this code?


We don't know.

The trace tells you the exact statement causing the problem. We don't know what statement that is.

So find the statement, find the null variable and fix the problem.

This is standard debugging.
4 months ago
The ActionListener can be shared by both buttons.
4 months ago
And what is your issue?

Displaying two tables is no different that displaying two labels or two buttons or two text fields.

Read the Swing tutorial on How to Use Tables
4 months ago
For each batch I would create a JPanel with:

1. a JLabel for the batch number
2 a JList (in a JScrollPane) for the details.

Then you can add a MouseListener to the panel to enable dragging of the panel around the screen.

Check out the Basic Dragging example code found in Moving Windows
4 months ago
Code seems to work fine for me.

However a few comments:

1. Don't use setPreferredSize(...). It is the job of the layout manager to determine the size of the panel and it is the job of the component to determine its own preferred size

2. Don't use: new JTextField(). Instead use new JTextField(10). Change the number as required. This will allow the text field to size itself to display 10 "W" characters at one time.

5 months ago

I asked for an easy fix


You have 4 issues.

I gave you multiple easy fixes to fix most problems.


The layout manager is the last issue to fix and was really a comment for the future.


I am not sure how to refactor my code to fix that


You don't need to refactor your code to:

a) try a different image size
b) read the JLabel API to align the text to the right
5 months ago

but could you give me a quick reply on how to easily fix this issue?


I did.
5 months ago

Namely static abuse, but I don't know how to fix it


Get rid of all the static variables and method.

The only method that needs to be static is the main() method.

reddit icon not resizing


Looks reasonable to me. I would say the image is 120x120.

Try playing with the size to see the difference.

text getting cut off by said icon


Don't use a null layout. This is the biggest problem beginners have. I have no idea where you learn to use a null layout.

Swing was designed to be used with layout mangers. Every tutorial I have seen uses layout manangers.

Read the Swing Tutorial.

There are working example for all Swing components. Every tutorial demo code will use layout managers.

There is also a separate section on Layout Managers with basic example of using each layout manager.

You can use the GridBagLayout for displaying components is row/columns.

text not centered


Read the JLabel API. There are methods to control the positioning of the text.

Also, typically numbers like that would be right aligned.
5 months ago
Note the code order should be:


The resizable method can affect the size of the decorations of the frame which in turn will affect the pack() calculation.
5 months ago
1. Post a SSCCE demonstrating your problem

2. Don't use a TimerTask. The creation and updates to the model of a component should be done on the Event Dispatch Thread (EDT). Instead you should be using a Swing Timer.

3. Check out the JComponent.setAutoscrolls(...) method. It has an example of a MouseListener that allows scrolling to continue once the mouse is dragged outside of the bounds of the scroll pane. Maybe look at the source code to see what this method does and see if you can replicate the behaviour for your requirement.
5 months ago
You last question had 100 replies. This is an indication of a poorly communicated question.

This question is now up to 10 replies. Its time you start to learn to communicate better.

You made the following comment:

but no sound for now



So what does that mean?

You first stated you were getting: Exception in thread "main" java.io.FileNotFoundException: 60s rock beat.wav

Does this mean you have made changes and you no longer get this message?

If so, then you should:

1. tell us the "original" problem has been solved.
2. ask a new question stating your new problem.

This way it keeps the thread concentrated on a single concept.

In any case if you hear no sound, then as I stated in my previous answer you need to add a Thread.sleep(...) otherwise the JVM closes before the wav file can be played. You added your code in the wrong place.

To get the code working first do:

1. forget about the "sounds" folder. Put the .wav files in the same directory as the class file
2. the Thread.sleep(...) needs to be execute AFTER you start the music playing

That is try to solve one problem at a time to keep the logic simple. Once the above two steps work, then you worry about placing the .wav file in a separate folder.

The following example shows how to play 5 second of a song:


Once the above works you move the .wav files to your "sound" directory and then retest.

Something like:


Give you variables meaningful names so you can understand how to use the variable. Why would you have a static variable and local variable both use the name "file"?
6 months ago
What don't you understand about the Exception?

It is telling you the file is not found. We can't verify if you entered the file names correctly.

Did you:

1. try another file?
2. make sure the file exists in the directory where the class file is located?

Note, even if you find the file it won't play the file because:

1. the filename in the HashMap is different than the filename in your get(...) method. Instead of typing the filename twice try using



2. The class will finish executing before the clip is played. You can add a Thread.sleep(1000) statement at the end of your class.
6 months ago
Cross posted: https://stackoverflow.com/questions/66735468/how-to-update-a-window-that-draw-a-png-with-paintcomponent

The question still doesn't make any sense and none of the suggestion have been implemented.

1. Why are you still reading the image in the paintComponent() method.
2. You state the image is updated in the folder, but there is no logic in your code that does that so why would you expect to get a new image.
7 months ago