Reputation: 1351
I'm designing a gui for an application using the NetBeans gui builder, and in one point I want a small JFrame with a progress bar, cancel button and label to pop up. The text for the label (Which is centered) sometimes displays somewhat long filenames that do not fit on the screen, but instead of just not showing the text that overflows, it takes the progress bar and stretches it, so that the right side of it goes off screen. I think it has something to do with NetBeans using GroupLayout, but I'm not sure. Here is the generated constructor code:
progressLabel = new javax.swing.JLabel();
cancelBtn = new javax.swing.JButton();
progressBar = new javax.swing.JProgressBar();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
progressLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
cancelBtn.setText("Cancel");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)
.addComponent(progressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(cancelBtn)
.addGap(18, 18, 18))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(cancelBtn))
.addGroup(layout.createSequentialGroup()
.addGap(12, 12, 12)
.addComponent(progressLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
pack();
Upvotes: 0
Views: 391
Reputation: 9192
As a suggestion (and this may of course be an answer for you) I would compact the path displayed within the progress dialog window to the maximum width (in characters) of the desired max width of your Progress Bar, for example:
C:\Users...\Documents\Helicoil Charts.docx
For the most part displaying the complete path to the User is not really a requirement however if it is then the full path length can be displayed as a Tool-Tip should the User pass his/her mouse over the JLabel, for example:
jLabel1.setToolTip(myFullPathVariable);
To compact a file path you can use this method:
/**
* Compact a file path into a given number of characters. <u>Similar</u> to the
* Windows Win32 API PathCompactPathExA API function.
* @param path (String) The Path to compact.
* @param charLimit (Integer) The maximum number of characters the path is allowed to be.
* @return (String) The compacted Path.
*/
public static String CompactPathBC(String path, int charLimit) {
if (path.length() <= charLimit) { return path; }
char shortPathArray[] = new char [charLimit];
char pathArray [] = path.toCharArray();
char ellipseArray [] = "...".toCharArray();
int pathindex = pathArray.length - 1 ;
int shortpathindex = charLimit - 1;
// fill the array from the end
int i = 0;
for (; i < charLimit ; i++) {
if (pathArray[pathindex - i] != '/' && pathArray[pathindex - i] != '\\') {
shortPathArray[shortpathindex - i] = pathArray[pathindex - i] ;
}
else { break; }
}
// check how much space is left
int free = charLimit - i;
if (free < "...".length()) {
// fill the beginning with ellipse
System.arraycopy(ellipseArray, 0, shortPathArray, 0, ellipseArray.length);
}
else {
// fill the beginning with path and leave room for the ellipse
int j = 0;
for(; j + ellipseArray.length < free; j++) { shortPathArray[j] = pathArray[j] ; }
// ... add the ellipse
for(int k = 0; j + k < free;k++) { shortPathArray[j + k] = ellipseArray[k] ; }
}
return new String(shortPathArray);
}
By using Font Metrics you can also create a method to make your path go to a specific maximum width in pixels.
Upvotes: 2