आपको स्वत: रीसाइज को बंद करने के लिए सेट करना होगा (setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
), लेकिन आपको अपने कॉलम का आकार बदलने के लिए एक सहायक विधि की भी आवश्यकता है।
यह है कि JTable फैली एक कस्टम वर्ग के अंदर है, लेकिन आप बस के रूप में आसानी से एक मौजूदा JTable देख सकते हैं:
public void resizeColumnWidth() {
int cumulativeActual = 0;
int padding = 15;
for (int columnIndex = 0; columnIndex < getColumnCount(); columnIndex++) {
int width = 50; // Min width
TableColumn column = columnModel.getColumn(columnIndex);
for (int row = 0; row < getRowCount(); row++) {
TableCellRenderer renderer = getCellRenderer(row, columnIndex);
Component comp = prepareRenderer(renderer, row, columnIndex);
width = Math.max(comp.getPreferredSize().width + padding, width);
}
if (columnIndex < getColumnCount() - 1) {
column.setPreferredWidth(width);
cumulativeActual += column.getWidth();
} else { //LAST COLUMN
//Use the parent's (viewPort) width and subtract the previous columbs actual widths.
column.setPreferredWidth((int) getParent().getSize().getWidth() - cumulativeActual);
}
}
}
कॉल resizeColumnWidth() जब भी आप एक पंक्ति जोड़ें।
वैकल्पिक रूप से मेज पर एक श्रोता जोड़ सकते हैं ताकि कॉलम भी जब तालिका ही आकार दिया जाता है आकार दिया गया है: [तालिका स्तंभ समायोजक] (http://tips4java.wordpress.com:
public MyCustomJTable() {
super();
addHierarchyBoundsListener(new HierarchyBoundsAdapter() {
@Override
public void ancestorResized(HierarchyEvent e) {
super.ancestorResized(e);
resizeColumnWidth();
}
});
setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}
स्रोत
2016-01-09 17:57:12
इस ट्यूटोरियल को देखने के/2008/11/10/तालिका स्तंभ-समायोजक /) –