This week's book giveaway is in the Spring forum.
We're giving away four copies of Spring in Action (5th edition) and have Craig Walls on-line!
See this thread for details.
Win a copy of Spring in Action (5th edition) this week in the Spring forum!

Rob Camick

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

Recent posts by Rob Camick

For now though, I just need to figure out how to identify if the click is on Line or Rectangle.



So you need to add an instance variable to your class, lets say "drawRectangle".

Then in the ActionListener for the Rectangle menu item you set the value to true.

In the ActionListener for the Line menu item you set the value to false.

Then if your relevant painting code you do something like:



You would need to update the other relevant painting code with a similar test.

Also, in your mouseDragged() method you should not be using the getGraphics() method  of a component to do painting.

Instead you update instance variables to reflect the current state of the dragging based on the latest MouseEvent. Then you invoke repaint() and the panel should paint the state.

For an example of this approach check out the DrawOnComponent example found in Custom Painting Approaches.. It shows the proper way to dynamically draw and save a Rectangle shape on a panel.
1 week ago

And is this what you mean about the SSCCE?



More or less. Yes I can copy/paste/compile and test.

But the point of the SSCCE is to demonstrate the "concept" you are trying to test.

Does the HTML need to be that complex to display a link? With such complex HTML you don't know if the problem is with the HTML or with some other part of your code.

Also, where is your debug code? Did you attempt to verify if the HyperlinkListener is ever invoked?

I added a:



to the listener and saw events were indeed being generated, but I couldn't tell which event was being generated.

So then I changed the code to the following:


And I noticed I was always getting the "Unknown" event.

When I looked closer at the code I saw you were using the "getSource()" method instead of the "getEventType()" method.

Now that the code was simplified into a SSCCE it was easier to spot the error.
1 week ago

Also as a side problem, the scroll bar that I have set for the JEditorPane appears, but it doesn't do any scrolling.



A Swing component can only have a single parent:


You create the scroll pane and add the editor pane to its viewport and then add the scroll pane to the panel which is correct.

But then you add the editor pane to the panel, which removes it from the viewport of the scroll pane, which is incorrect.

Regarding your main question I don't see any obvious errors but the code you posted is not executable.

Post a proper SSCCE that demonstrates the problem.

So that means create a simple example with a JFrame and your editor pane. The HTML text should be a hardcoded string so we can see exactly what is added to the editor pane.

The point of a SSCCE is to get a basic example working first. If it works then you compare the SSCCE code with your real code to see what the difference is. If it doesn't work then you have simple code to post on the forum. Then maybe someone will test your code to see if they can find the problem.
1 week ago
Swing is double buffered by default. There is no need to override the paint() method.

The custom painting is only required in the paintComponent() method.

Read the Swing tutorial on Custom Painting for more information and working examples.
1 week ago
Your basic approach is wrong.

The benefit of using an Action is that it can be shared by any component that supports Actions.

So instead of trying to implement multiple annonymous inner classes, you should be creating a single Action. Then you can create your JMenuItem with this Action.

Another benefit of using the Action is that you can assign the accelerator and mnemonic keys directly to the Action. Swing will then do the Keystroke bindings for you automatically when you create the menu item.

Read the section from the Swing tutorial on How to Use Actions for more information. There is also a section on Text Component Features that demos  working undo/redo Actions.
2 weeks ago

I have a deadline in 2 hours


That is irrelevant. People answer questions when they have time.

You need to manage your time more effectively

at D.Swing.main(Swing.java:33)


The code you posted is not the code you are executing because line 33 is a blank line, so we can't tell exactly what the problem is.


One of the fundamental rules of Swing is that components should be created and modified on the Event Dispatch Thread (EDT). So the creation of your  WriteYourName object should be done in the invokeLater() code.

And why do you have 2 setVisible() statements?
2 weeks ago

Thank you for responding,



You don't appear to have listened to the suggestions.

The first answer showed you how to alternate square colors using a simple if statement.

The second answer suggested you needed a different if statement to set the squares of the first row and column to black.

Where is that if statement?

Why are you setting the color of 4 squares each time your iterate the loop? This means you are setting and resetting the color of certain squares multiple times.

When using looping code, only set the Color of the current component being processed as determined by the relevant indexes.
2 weeks ago

What can I do to make the squares in the first row and first column black, while leaving the rest of the squares white?





Well you can't just always make each button white, you need an if condition.

How about checking if x or y = 0 you set the color black otherwise set the color white?
2 weeks ago

The problem that I have is that when I type a character that is not a digit the JTable stops receiving data in that cell.



So what do the butons and ActionListeners have to do with that problem?

I can't copy/paste/compile/test the code you posted.

A SSCCE should be the code that is directly related to the problem. In this case its sounds like a frame with a JTable.

I would expect the SSCCE to be about 10 lines of code based on your description of the problem. A few lines to create the frame, a few more for the table and a main() method to create the frame and display it.
2 weeks ago
I have no idea what you are attempting to do. We need to know your requirement (not your attempted solution) if you want more help.

You have a table model with 0 rows.

What is the point of the KeyListener? You don't need to use a KeyListener to edit data in a cell.

Post a proper SSCCE that demonstrates what you are trying to do.
2 weeks ago

The problem is that the method is called only the first time.



I believe that when you type a character on a cell the editor is invoked (if the cell is editable) and all future KeyStrokes are passed to the editor, not the table.

The table should start responding again once the cell is finished editing. That is if you tab or click on another cell.
2 weeks ago
If you are using the DefaultTableModel then you don't need to do anything special. If you edit a cell the data will be saved to the TableModel and the getValueAt(...) method will access the data in the model.

Post a proper SSCCE that demonstrates your problem.

That is create a JFrame with a JTable and  JButton. When you click the button you invoke the code you posted above.

The basic code to create the table would be:


If the code works then you figure out what you are doing differently in your real application.

If it doesn't work, then you have something simple to post in the forum.
2 weeks ago
Don't generate random numbers in a painting method.

You can't control when Swing will repaint the component. For example if you resize the frame the component is repainted and you will regenerate the random color.

The Color should be a property of the class and you should invoke a method to generate a new random color.

You would then invoke this method in the constructor of your class to initially set the color.
1 month ago

Yes but when I use clear() or remove on if text.equal(Delete){code}, it does not work.



If you want to test the clear() method of the ArrayList then you write some simple test code.


Does the above do what you expect?

If no, then you have simple code to post in the forum asking a question why it doesn't work.

If yes, then you have proved it does work, so that means that statement is not executed in your real application, so the question is why is it not executed. Time for you to do basic debugging and check your logic flow.


One place to start is your ActionListener.

If it poor design to have an ActionListener do multiple things. Any time you see a bunch of if statements in the listener you know you should refactor the code.

Instead each menu item should have its own separate Action defined in your class. It is easier to maintain and debug and check your logic flow.




Also, class names should start with an upper case character. Learn and follow Java conventions.
1 month ago
Cross posted: https://stackoverflow.com/questions/53212247/get-data-from-database-and-show-each-by-each-data-in-a-jlabel-for-5-seconds/53214385#53214385

The OP was given an answer to this question yesterday.

The Op made no effort to solve the problem on their own and simply cut and pasted the exact question again in a different forum.
1 month ago