Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!

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
669
Received in last 30 days
2
Total given
29
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


The default layout manager for a JPanel is a FlowLayout which respects the preferred size of any component added to it.

You can't just use the BorderLayout constraint.

If you want a BorderLayout, then you need to set the layout manager to a BorderLayout:




Now the size of the scroll pane will adjust dynamically as the frame is resized and scrollbars will appear as needed.
2 weeks ago
Don't use setPreferredSize().

Each Swing component is repsonible for determining its own preferred size.

When creating a JTextField you can use:



This will allow the text field to calculated is preferred size to display 10 "W" characters.

If you need more or less change the number.
1 month ago
The layout of the option pane is controlled internally, and there are no methods to directly control the layout of the buttons.

So, the proper solution is to just create a custom modal JDialog that displays the components based on your requirements.

However, if you really want to use the JOptionPane functionality then you would need to:

1. create the JOptionPane as a Swing component and then change the layout manager of the panel containing the buttons.
2. add the JOptionPane to a JDialog and implement the standard option pane functionality manually.

The first step is demonstrated below:


The above code searches for the buttons that have been added to the option pane and then finds the parent container and changes the layout manager of the container to be a GridLayout. You will also need the  SwingUtils class.

To implement the second step you will need to Read the API for the JOptionPane. It contains the code necessary add the code to a JDialog and implement the option pane functionality.

2 months ago
Post your SSCCE demonstrating the problem.

We need minimal code showing the compile error.

So all you need to do is create a JPanel and try and add an entry to the InputMap.

The entire class will be about 5 lines of code.

Get something basic working and then figure out what is wrong with your real code.
3 months ago

The getKeyStroke(...) method requires 2 or 3 parameters. Read the API for more information.

3 months ago

I want a JPanel (or something) in the app that would allow me to display an image at 100%



I believe the scaling factor is defined in the AffineTransform of the Graphics object. So this factor will be applied to all the Swing components when they are painted.

So you should be able to remove the default scaled transform and do your custom painting with code like:

4 months ago
What is the DataHandler?

I don't see that class in the JDK 11 API.

It would be nice if you included that class so others could use it.

4 months ago

Yes, but when I did setVisible(false) it caused the program to exit!


Well, I've never used Linux before but I doubt using setVisible(false) will cause the application to close if the application is coded properly.

Even Paul's example above does not follow Swing coding guidelines.

All Swing components should be created and updated on the Event Dispatch Thread (EDT) or you could have random results.

Will the above code cause a problem? I doubt it, but you never know.

Read the section from the Swing tutorial on Concurrency for more information.

Regarding layout. If you choose not to use layout managers then you will have problems. Layout managers a a main reason the application should be platform independent. They account for different pixels sizes and different component LAF. Using appropriate layout managers will allow components to be aligned in grids easily.
4 months ago
Don't use setPreferredSize() anywhere.

It is the responsibility of each Swing component to know its own preferred size.

If you are doing custom painting then override the getPreferredSize() method of your component.

Then you add the component to the frame and then you pack() the frame.

Now the size of the frame will be the preferred size of the custom component plus the size required for the title bar and borders.
5 months ago
Quit cross posting: https://stackoverflow.com/questions/63929497/java-how-to-differentiate-2d-object

You were given the same answer yesterday.

Its frustrating when we take the time to help and you completely ignore the advice given.

5 months ago

locationToIndex is what I am using. I want it to return -1 if I click an empty space, instead


Then override the method to do what you want.
5 months ago

replicate something I see in popular UIs


Not sure where you see that.

1. How do people know that clicking there will create a new value?
2. What happens when the JList is full and there is no space to click?

Normal UI design is for a click to "select" an item and then a double click will invoke a default Action on the selected item.

Other normal conventions are to right click to display a popup menu to display a list of Actions that can be performed.

Also, a UI should be designed so that is can be used with a mouse or the keyboard.

Advanced users use the keyboard because it is more efficient. Beginners use the mouse.

So how would users use the keyboard to move beyond the last item to invoke the create logic?



5 months ago
Where is your SSCCE?
5 months ago

The main thing holding me back from doing it all through code is the jlist starts off at 0px if its empty.



Not sure why you say that.

The JList has a default size when I run:


I can then control the size by adding:


The above code example is called a SSCCE.

Every question should contain a SSCCE so we can verify the code you are using.
5 months ago