반응형

자바 JTable을 사용하다보면 안에 있는 자료길이가 길어 셀에서 ...으로 먹히는 경우가 있는데 최대한 그런 경우가 발생하지 않게 하기 위해 하는 함수를 소개한다.


public void resizeColumnWidth(JTable table) {
    final TableColumnModel columnModel = table.getColumnModel();
    for (int column = 0; column < table.getColumnCount(); column++) {
        int width = 50; // Min width
        for (int row = 0; row < table.getRowCount(); row++) {
            TableCellRenderer renderer = table.getCellRenderer(row, column);
            Component comp = table.prepareRenderer(renderer, row, column);
            width = Math.max(comp.getPreferredSize().width +1 , width);
        }
        columnModel.getColumn(column).setPreferredWidth(width);
    }
}


위 소스를 사용하면 자동으로 내용 길이에 맞춰 JTable column 넓이를 조절해 준다.

반응형

'프로그래밍 > JAVA' 카테고리의 다른 글

Gradle  (0) 2023.09.04
Titleborder 색 변경  (0) 2015.12.27
[팁] JarBundler 이용법.  (0) 2015.12.08
[팁] application bundle 위치  (0) 2015.12.08
JAVA L&F(Look & Feel)  (0) 2015.12.05

+ Recent posts