Rob Camick

Rancher
+ Follow
since Jun 13, 2009
Cows and Likes
Cows
Total received
18
In last 30 days
0
Total given
0
Likes
Total received
560
Received in last 30 days
3
Total given
16
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

When the structure of the TableModel is changed the JTable will automatically recreate its TableColumnModel based on the new structure of the TableModel.

You can prevent this from happening by using code like:



So now the initial TableColumns for the table are created when the model is set for the table but no further TableColumns will be created for the table when the structure of the TableModel changes.

But, you now need to manually add a TableColumn to the TableColumnModel of the JTable:


6 hours ago

each instance can be assigned to all columns of the corresponding type?



Correct.

I've defined a single editor class that handles signed (accepts leading -)



The default editor already support this. You override the getColumnClass(...) method of the TableModel and the table will choose the Integer editor for the column.

3 days ago

Just integrated it into my code (just the 5 char verification)



Then all you need to do is change the editing code in the stopCellEditing() method to meet your requirements.

can/should I use just one instance of the editor for all columns?



The point of using renderers/editors for a JTable is that they are shared by all cells. This makes efficient use of resources.

The default implementations of each reset the basic state (borders, font ...)  each time they are used (although a renderer doesn't reset the background color if your custom renderer changes it, so you need to manage that yourself).
3 days ago

I have googled around and know the answer probably involves writing a cell editor



Here is an example of a custom editor that specifies you must enter exactly 5 characters:



Another approach might be to limit the characters ( 0 - F ) the user can type into the editor. This would be done by using a DocumentFilter.

The basic code would be:


Read the section from the Swing tutorial on Implementing a DocumentFilter for more information and examples to get you started.
4 days ago
Well I don't know if your problem is your custom TableModel or in your layout code.

So my suggestion is to just use the DefaultTableModel. There is no need to create a custom model.

Check out the "TableFromDatabaseExample" code found in Table From Database.

It is a complete example. All you need to change is the database connection and the SQL query.
2 weeks ago


So you have two variables on that statement. Which variable is null?

Add a System.out.println(...) to display each variable to see which is null and then fix the problem.

This is basic problem solving any time you get a NullPointerException.
2 weeks ago
You have a method that returns "void".

But you are trying to assign it to a variable that is defined  as "Database"

2 weeks ago
We can't tell what you are doing wrong based on the code posted.

Post a SSCCE the demonstrates the problem.

So all you need is a JFrame with a panel and some components add to the BorderLayout.CENTER.

Then you create a button that will replace the panel and its components and add this button to the BorderLayout.PAGE_END.

This will demonstrate the concept of replacing panels.

If it works then you apply the knowledge to your real program.

If it doesn't work then you have simple code to post in the forum.
1 month ago
Don't use the updateUI() method. That is used internally by Swing for a LAF change. You are not changing the LAF.

The basic logic for adding/removing components from a panel is:



There is no need for the setVisible(). Swing components are visible by default.

There is no need to reset the layout manager for the panel. Removing the components does not change the layout manager.
1 month ago

If editability is not relevant for your application, then I would go with Rob: use a BufferedImage to do your drawings (ironically, you must use the bi.getGraphics() for that as well), and in your paintComponent draw that bi.



Yes, and in the link I provided above it shows both approaches:

1. drawing in the paintComponent() method of a JPanel and you need to keep a list of objects to paint
2. drawing directly to a BufferedImage when you do use the getGraphics method to draw directly on the BufferedImage

Which approach you use depends on your requirements and some guidelines are given for each approach.

Again, I repeat you should not be using the getGraphics() method to draw onto a Swing component. You use the Graphics object passed to the paintComponent() method of the component.

1 month ago
I'm not interested in debugging your code. As I told you before you are not doing custom painting correctly.

I've given you working examples to download and test and play with in order to understand the proper way to do custom painting.

Then you make changes to your code.
1 month ago
You should not be using getGraphics() to do custom painting.

In Swing, custom painting is done by overriding the paintComponent() method of a JPanel.

Check out Custom Painting Approaches for working examples of drawing rectangles.
1 month ago
If it doesn't work then you probably aren't reading the Image.

Use ImageIO.read(...) to read the image. You will get an error message if it can't find the image file.
1 month ago

But with this text pane i should have only place for 4 digits. (number from 1 to 100). 



Well you only need room for 3 digits.

Don't use a JTextPane. A JTextPane is used for larger pieces of text.

In gener, for 3 digits use a JTextField:



The text field will then determine its own preferred size. You then use a layout manager that respects the preferred size of the text field.

Then if you want to limit the number of characters the use can type into the text field you can use a DocumentFilter. Read the Swing tutorial on How to Implement a DocumentFilter for a working example.

However, in your case because you only want numbers in the text field you can use a JFormattedText field. This will allow you specify the number of digits to be entered by using a "format mask" and the mask will edit the character as it is typed to make sure it is a digit and not a letter. Read the tutorial on How to Use Formatted Text Fields.
1 month ago
Read the section from the Swing tutorial on How to Use Actions.

You can download and play with the working examples and then restructure your code to be similiar to the demo code.
1 month ago