Java JTable 설정 컬럼 폭
컬럼 크기를 다음과 같이 설정하는 JTable이 있습니다.
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.getColumnModel().getColumn(0).setPreferredWidth(27);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(90);
table.getColumnModel().getColumn(4).setPreferredWidth(90);
table.getColumnModel().getColumn(6).setPreferredWidth(120);
table.getColumnModel().getColumn(7).setPreferredWidth(100);
table.getColumnModel().getColumn(8).setPreferredWidth(95);
table.getColumnModel().getColumn(9).setPreferredWidth(40);
table.getColumnModel().getColumn(10).setPreferredWidth(400);
이렇게 하면 잘 작동하지만 테이블이 최대화되면 마지막 열의 오른쪽에 빈 공간이 표시됩니다.크기를 조정할 때 마지막 열의 크기를 창 끝으로 조정할 수 있습니까?
AUTO_RESIZE_LAST_COLUMN
하지 않습니다.
집::JTable
JScrollPane
원하는 크기가 설정됩니다.
요?setMinWidth(400)
대신 마지막 줄에setPreferredWidth(400)
JavaDoc용 :JTable
, 문서를 주의 깊게 읽습니다.선택 가능한 비트는 다음과 같습니다.
둘러싸인 창의 크기를 조정한 결과 메서드가 호출되면 resize Column은 null이 됩니다.즉, 크기 조정은 JTable의 "외부"에서 이루어졌으며 변경 사항(또는 "delta")은 이 JTable의 자동 크기 조정 모드에 관계없이 모든 열에 배포되어야 합니다.
인지도 .AUTO_RESIZE_LAST_COLUMN
도움이 안 됐어요
주의: JTable은 컬럼의 폭을 조정할 때 컬럼의 최소값과 최대값을 절대적으로 존중합니다.
마지막 열을 제외한 모든 열에 대해 최소 == 최대값을 설정한 다음 마지막 열에 최소 = 기본 설정을 하고 Max를 설정하지 않거나 Max에 매우 큰 값을 설정할 수 있습니다.
★★★★★★★★★★★★★★★★ JTable.AUTO_RESIZE_OFF
테이블은 컬럼 크기를 변경하지 않으므로 원하는 설정을 사용합니다.을 채우는 것을 를할 수 .JTable.AUTO_RESIZE_LAST_COLUMN
, autoResizeMode와 함께 일 수 . 단, 와 함께 사용하는 것이 가장 효과적일 수 있습니다.TableColumn.setMaxWidth()
TableColumn.setPreferredWidth()를 선택합니다.
AUTO_RESIZE_LAST_COLUMN
하고 있는 는, 이 해 볼 수 있습니다.여러분은 이 조합으로 실험할 수 있습니다.TableColumn.setMaxWidth()
★★★★★★★★★★★★★★★★★」TableColumn.setMinWidth()
JTable.AUTO_RESIZE_LAST_COLUMN
는 "모든 크기 조정 작업 중 마지막 열에만 조정을 적용"으로 정의됩니다.즉, 코드 끝에 자동 크기 조정 모드를 설정해야 합니다.그렇지 않으면 setPreferredWidth()는 아무런 영향을 주지 않습니다.
따라서 이 방법이 올바른 방법입니다.
table.getColumnModel().getColumn(0).setPreferredWidth(27);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(90);
table.getColumnModel().getColumn(4).setPreferredWidth(90);
table.getColumnModel().getColumn(6).setPreferredWidth(120);
table.getColumnModel().getColumn(7).setPreferredWidth(100);
table.getColumnModel().getColumn(8).setPreferredWidth(95);
table.getColumnModel().getColumn(9).setPreferredWidth(40);
table.getColumnModel().getColumn(10).setPreferredWidth(400);
table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
이 방법 사용
public static void setColumnWidths(JTable table, int... widths) {
TableColumnModel columnModel = table.getColumnModel();
for (int i = 0; i < widths.length; i++) {
if (i < columnModel.getColumnCount()) {
columnModel.getColumn(i).setMaxWidth(widths[i]);
}
else break;
}
}
□□을 확장합니다.JTable
링크:
public class Table extends JTable {
public void setColumnWidths(int... widths) {
for (int i = 0; i < widths.length; i++) {
if (i < columnModel.getColumnCount()) {
columnModel.getColumn(i).setMaxWidth(widths[i]);
}
else break;
}
}
}
그리고 나서.
table.setColumnWidths(30, 150, 100, 100);
클레오파트라(그녀는 두 번째로 javax.swing을 볼 것을 제안했다)의 말을 읽는다.JXTable입니다.처음에는 못 봐서 죄송합니다:) 링크를 클릭해 주세요.
같은 문제에 대한 해결 방법이 있습니다. (위 링크를 따라가는 것이 좋습니다.)테이블 크기를 조정하면 테이블의 열 너비를 현재 테이블의 총 너비로 스케일링합니다.이를 위해 (상대적인) 열 너비에 대해 글로벌 배열의 int를 사용합니다.
private int[] columnWidths=null;
이 기능을 사용하여 표 열 너비를 설정합니다.
public void setColumnWidths(int[] widths){
int nrCols=table.getModel().getColumnCount();
if(nrCols==0||widths==null){
return;
}
this.columnWidths=widths.clone();
//current width of the table:
int totalWidth=table.getWidth();
int totalWidthRequested=0;
int nrRequestedWidths=columnWidths.length;
int defaultWidth=(int)Math.floor((double)totalWidth/(double)nrCols);
for(int col=0;col<nrCols;col++){
int width = 0;
if(columnWidths.length>col){
width=columnWidths[col];
}
totalWidthRequested+=width;
}
//Note: for the not defined columns: use the defaultWidth
if(nrRequestedWidths<nrCols){
log.fine("Setting column widths: nr of columns do not match column widths requested");
totalWidthRequested+=((nrCols-nrRequestedWidths)*defaultWidth);
}
//calculate the scale for the column width
double factor=(double)totalWidth/(double)totalWidthRequested;
for(int col=0;col<nrCols;col++){
int width = defaultWidth;
if(columnWidths.length>col){
//scale the requested width to the current table width
width=(int)Math.floor(factor*(double)columnWidths[col]);
}
table.getColumnModel().getColumn(col).setPreferredWidth(width);
table.getColumnModel().getColumn(col).setWidth(width);
}
}
호출하는 데이터를 설정할 때:
setColumnWidths(this.columnWidths);
변경 시 테이블의 부모에게 ComponentListener 세트를 호출합니다(이 경우 테이블의 컨테이너인 JScrollPane).
public void componentResized(ComponentEvent componentEvent) {
this.setColumnWidths(this.columnWidths);
}
JTable 테이블도 글로벌한 것에 주의해 주세요.
private JTable table;
청취자를 설정합니다.
scrollPane=new JScrollPane(table);
scrollPane.addComponentListener(this);
이 코드는 setAutoResizeModes 없이 사용할 수 있습니다.
TableColumnModel columnModel = jTable1.getColumnModel();
columnModel.getColumn(1).setPreferredWidth(170);
columnModel.getColumn(1).setMaxWidth(170);
columnModel.getColumn(2).setPreferredWidth(150);
columnModel.getColumn(2).setMaxWidth(150);
columnModel.getColumn(3).setPreferredWidth(40);
columnModel.getColumn(3).setMaxWidth(40);
fireTableStructureChanged();
크기 조정 동작을 기본값으로 합니다! 열 크기 조정 속성을 설정한 후 이 메서드가 코드 어딘가에서 호출되면 모든 설정이 재설정됩니다.이 부작용은 간접적으로 발생할 수 있다.예를 들어 링크된 데이터 모델이 변경됨에 따라 특성이 설정된 후 이 방법을 호출한다.
이 옵션은 필요하지 않습니다. 마지막 열의 기본 너비를 최대값으로 설정하기만 하면 추가 공간이 모두 사용됩니다.
table.getColumnModel().getColumn(0).setPreferredWidth(27);
table.getColumnModel().getColumn(1).setPreferredWidth(120);
table.getColumnModel().getColumn(2).setPreferredWidth(100);
table.getColumnModel().getColumn(3).setPreferredWidth(90);
table.getColumnModel().getColumn(4).setPreferredWidth(90);
table.getColumnModel().getColumn(6).setPreferredWidth(120);
table.getColumnModel().getColumn(7).setPreferredWidth(100);
table.getColumnModel().getColumn(8).setPreferredWidth(95);
table.getColumnModel().getColumn(9).setPreferredWidth(40);
table.getColumnModel().getColumn(10).setPreferredWidth(Integer.MAX_INT);
이 코드를 사용합니다.그것은 나에게 효과가 있었다.나는 3컬럼을 고려했다.코드의 루프 값을 변경합니다.
TableColumn column = null;
for (int i = 0; i < 3; i++) {
column = table.getColumnModel().getColumn(i);
if (i == 0)
column.setMaxWidth(10);
if (i == 2)
column.setMaxWidth(50);
}
언급URL : https://stackoverflow.com/questions/953972/java-jtable-setting-column-width
'source' 카테고리의 다른 글
Perl DBI 모듈을 사용하여 계정 호스트를 변경하는 방법 (0) | 2023.01.29 |
---|---|
ON DUPLICE KEY를 사용하여 삽입하려는 모든 키를 업데이트하는 방법이 있습니까? (0) | 2023.01.29 |
같은 클래스 내의 메서드에 의한 Spring @Transaction 메서드 호출이 기능하지 않습니까? (0) | 2023.01.29 |
InnoDB 쓰기 로그 효율이 100% 이상(1953.15%)입니까? (0) | 2023.01.19 |
vuex에서 '정의되지 않은' 값을 수정하는 방법 (0) | 2023.01.19 |