What improvements should be made to this JDialog snippet?

Go To StackoverFlow.com


I wrote a small function that will display a table inside a dialog and am looking for advice on what to clean up and what is better programming practice when it comes to dealing with swing.

What improvements can be made to my code

//constraints for panel to fill out the frame
GridBagConstraints grid = new java.awt.GridBagConstraints();
grid.fill = java.awt.GridBagConstraints.BOTH;
grid.weightx = 1.0;
grid.weighty = 1.0;

//create jtable based on a table model created from an array
JTable table = new JTable(testModel);       //a method creates my test model
table.add(table.getTableHeader(), BorderLayout.PAGE_START);
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(testModel);

//add scrollpane for visibility
JScrollPane jscrollpane = new JScrollPane(table);

//add the scrollpane to a panel
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.add(jscrollpane, grid);

//create for use with the dialog
JFrame frame = new JFrame();

JDialog dialog = new JDialog(frame, "My Test Dialog", true); 
dialog.setLocationRelativeTo(null);    //added as advice of Stripies

I'm open to all constructive criticism as my goal is to learn the proper techniques for programming with swing.

To clarify, I'm looking whether I can take anything out or improve any of it.

2012-04-03 23:21
by WilliamShatner
To center your JDialog, you can simply use setLocationRelativeTo(null) - Stripies 2012-04-03 23:31
Alternatively, consider setLocationByPlatform(true) - trashgod 2012-04-04 01:01
An sscce is often more dispositive than a fragment, as often seen in the answers of these contributors - trashgod 2012-04-04 01:11
@trashgod "dispositive" Huh. Had to look that one up. My 'learn word' of the day. : - Andrew Thompson 2012-04-04 04:43
@Stripies Thank you for that bit of information. That saves me from using an extra method - WilliamShatner 2012-04-05 14:00
@trashgod Thanks for the advice, could you clear some things up for me though? I'm wondering what the benefits of using setLocationByPlatform(true) are? Do you mind explaining why this may be more useful than centering it each time? (Truly curious, not being sarcastic if thats what it comes off as). As for this example, how would I make it more SSCCE? I would think one could simply copy and paste this example and add in whatever table model and centering. Perhaps I didn't fully understand SSCCE though - WilliamShatner 2012-04-05 14:07
To make your example more SSCCE, it has to be self-contained. In other words, if the people trying to help can just copy and paste your code into a .java file, and compile it, they have much less work than if they need to work out what you intended the "table model and centering" and other work to accomplish.. - Rob I 2012-04-05 18:02


What are the benefits of using setLocationByPlatform(true)?

Using setLocationRelativeTo(null) is a convenient choice for examples, demos and utilities. Quality applications preserve the user's preferred position, possibly recording the most recent setting in an instance of java.util.Preferences. Because a user's experience varies by platform, setLocationByPlatform(true) represents the implementer's best effort to meet that expectation. It's a better choice for the default location when no preference yet exists.

2012-04-05 20:06
by trashgod