Rob Camick

Rancher
+ Follow
since Jun 13, 2009
Cows and Likes
Cows
Total received
30
In last 30 days
1
Total given
0
Likes
Total received
664
Received in last 30 days
0
Total given
28
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 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.

12 hours 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.
4 weeks ago

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

4 weeks 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:

1 month 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.

1 month 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.
1 month 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.
2 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.

2 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.
2 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?



2 months ago
Where is your SSCCE?
2 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.
2 months ago
I don't recommend using an IDE to generate your code. You spend time learning the IDE and not learning Swing.

The key to layout design is to logically group components onto a panel and then use an appropriate layout manager to display the components.

The trick is that you can nest panels with different layout managers as necessary.

Maybe start with the working code found in the Swing tutorial on How to Use Lists,

The tutorial also has a section on "Layout Manager" which contains working examples of each layout manager on its own.

I'm not sure why you show the bottom row with 3 columns, since there are no components being displayed.

So assuming you really only need a layout with 3 columns I might suggest you start with:

1. use the default BorderLayout of the frame
2. Add your JScrollPane containing the JList to the BorderLayout.LINE_START
3. Create a JPanel with a GridLayout and add it to the BorderLayout.CENTER
4. Create a panel for the "Add From File" components and add components to the panel and add the panel to the grid layout panel
5. Create another panel for the "Class Name" components and add the panel to the grid layout panel

so the key to to start with a top level logical design for each panel and then repeat the process as you get into more detail for each panel.

You will eventually learn which layout manager to use in a particular situation.
2 months ago
Only valid data should be displayed in the combo box.



2 months ago
There is no way to force the user to select a value.

You can only check if the item returned from getSelectedItem() is null after following Paul's suggestion and provide and do error processing.

You maybe also want to use the approach as demonstrated by the Combo Box Prompt.

The class from the link with allow you to display a prompt in the combo box when no item has been selected.
2 months ago