문제

이것은 존재하지 않거나 늦었 기 때문에 올바르게 생각/검색하지 않습니다 ...

특정 열에 존재할 것으로 예상되는 가장 큰 문자열의 프로토 타입 값을 기반으로 스윙에서 JTable 열 너비를 설정하고 싶습니다. 컴파일 타임에서 글꼴을 반드시 알지 못하므로 픽셀의 #을 모릅니다.

열 높이 목적으로 열 너비 목적으로 프로토 타입 값을 설정하는 방법이 있습니까? 그렇다면 어떻게?

도움이 되었습니까?

해결책

실행 시간에 JLabel을 만들고 크기를 사용하여 테이블 크기를 조정 해 보셨습니까?

// create a label that will be using the run-time font
JLabel prototypeLabel = new JLabel("Not Applicable")

// get the labels preferred sizes
int preferredWidth = prototypeLabel.getPreferredSize().getWidth();
int preferredHeight = prototypeLabel.getPreferredSize().getHeight();

// set the sizes of the table's row and columns
myTable.setRowHeight(preferredHeight);

for(TableColumn column : myTable.getColumnModel.getColumns()){
   column.setPreferredWidth(preferredWidth);        
}

다른 팁

다음 코드를 시도 할 수 있습니다.

/**
 * Sets the preferred width of the columns of a table from prototypes
 * @param table the target table
 * @param prototypes an array of prototypes, {@code null} values will be ignored
 * @param setMaxWidth {@code true} if the maximum column width should also be set
 */
public static void setWidthFromPrototype(JTable table, Object[] prototypes, boolean setMaxWidth) {
if (prototypes.length != table.getColumnCount())
  throw new IllegalArgumentException("The prototypes array should contain exactly one element per table column");
for (int i = 0; i < prototypes.length; i++) {
    if (prototypes[i] != null) {
        Component proto = table.getCellRenderer(0,i)
                .getTableCellRendererComponent(table, prototypes[i], false, false, 0, i);
        int prefWidth = (int) proto.getPreferredSize().getWidth() + 1;
        table.getColumnModel().getColumn(i).setPreferredWidth(prefWidth);
        if (setMaxWidth)
            table.getColumnModel().getColumn(i).setMaxWidth(prefWidth);
    }
}
}

스윙 렉스 확장 된 jxtable/열 지원 설정 초기 열 너비 크기에 대한 프로토 타입. 열이 생성 된 후에도 그렇게 할 수 있습니다.

for(int col = 0; ....) {
    table.getColumnExt(col).setPrototypeValue(myPrototype[col]
}

또는 생성시 열을 구성하는 사용자 정의 열 factory를 구현하여

ColumnFactory factory = new ColumnFactory() {
    @Override
    protected void configureTableColumn(TableModel model, TableColumnExt columnExt) {
        super(...);
        columnExt.setPrototypeValue(myPrototype[columnExt.getModelIndex()];
    }
}
table.setColumnFactory(factory);
table.setModel(myModel);

레이블을 만드는 대신 Tablecellrenderer에서 실제 구성 요소를 가져 와서 크기를 테스트하십시오.

// create the component that will be used to render the cell
Comp prototype = table.getDefaultRenderer(model.getColumnClass(i)).getTableCellRendererComponent(table, "Not Applicable", false, false, 0, i);

// get the labels preferred sizes
int preferredWidth = comp.getPreferredSize().getWidth();
int preferredHeight = comp.getPreferredSize().getHeight();

이것은 단일 열 예제입니다. 각 열의 크기를 얻으려면이를 반복해야합니다 (또한 설정). 보다 http://www.exampledepot.com/egs/javax.swing.table/packcol.html 이것의 예를 위해.

컴파일 시간에 글꼴을 모른다면 JTable 열의 너비는 항상 알려지지 않을 것입니다. 정신 점검으로 텍스트 문서를 열고 포인트 크기를 일정하게 유지하면서 다른 글꼴로 재생하십시오. 쓰여진 것의 길이는 글꼴별로 다르지만 높이는 그렇지 않습니다.

jtable 행의 높이는 모든 글꼴 크기 (포인트)에 대해 결정할 수 있어야합니다. 정의 된 표준. 그러나 Jtable이 아마도 세포 사이의 간격을 제공한다는 점을 감안할 때 약간의 실험이 필요할 수 있습니다.

컴파일 타임에 글꼴 크기 나 글꼴 자체를 보장 할 수 없다면 다른 사람들이 나오는 대답에 관심이 있습니다 :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top