반응형
JTable은 열 머리글을 표시하지 않습니다.
JTable을 인스턴스화하는 다음 코드가 있습니다. 테이블에 올바른 수의 행과 열이 표시되지만 열 위에 제목 표시가 없습니다.
public Panel1()
{
int nmbrRows;
setLayout(null);
setBackground(Color.magenta);
Vector colHdrs;
//create column headers
colHdrs = new Vector(10);
colHdrs.addElement(new String("Ticker"));
// more statements like the above to establish all col. titles
nmbrRows = 25;
DefaultTableModel tblModel = new DefaultTableModel(nmbrRows, colHdrs.size());
tblModel.setColumnIdentifiers(colHdrs);
scrTbl = new JTable(tblModel);
scrTbl.setBounds(25, 50, 950, 600);
scrTbl.setBackground(Color.gray);
scrTbl.setRowHeight(23);
add(scrTbl);
//rest of constructor
...
}
이것을 다른 테이블 작성 코드와 비교하면 누락 된 단계가 표시되지 않지만 뭔가 누락 된 것 같습니다.
당신 넣어 JTable내부를 JScrollPane. 이 시도:
add(new JScrollPane(scrTbl));
이 답변과 허용되는 답변의 주요 차이점은 setViewportView()대신을add() 사용 한다는 것입니다 .
Eclipse IDE JTable를 JScrollPane사용하여 입력하는 방법 :
JScrollPane디자인 탭을 통해 컨테이너를 만듭니다 .- 스트레치
JScrollPane원하는 크기 (절대 레이아웃에 적용)에. - (뷰포트 영역)
JTable위에 컴포넌트를 드래그 앤 드롭 합니다JScrollPane.
구조> 요소는 table의 자식이어야한다 scrollPane.
생성 된 코드는 다음과 같습니다.
JScrollPane scrollPane = new JScrollPane();
...
JTable table = new JTable();
scrollPane.setViewportView(table);
이전 답변에서 말했듯이 '정상적인'방법은 JScrollPane에 추가하는 것이지만 때로는 스크롤하지 않기를 원합니다 (언제 묻지 마십시오 :)). 그런 다음 직접 TableHeader를 추가 할 수 있습니다. 이렇게 :
JPanel tablePanel = new JPanel(new BorderLayout());
JTable table = new JTable();
tablePanel.add(table, BorderLayout.CENTER);
tablePanel.add(table.getTableHeader(), BorderLayout.NORTH);
public table2() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 485, 218);
setTitle("jtable");
getContentPane().setLayout(null);
String data[][] = { { "Row1/1", "Row1/2", "Row1/3" },
{ "Row2/1", "Row2/2", "Row2/3" },
{ "Row3/1", "Row3/2", "Row3/3" },
{ "Row4/1", "Row4/2", "Row4/3" }, };
String header[] = { "Column 1", "Column 2", "Column 3" };
// Table
JTable table = new JTable(data,header);
// ScrollPane
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(36, 37, 407, 79);
getContentPane().add(scrollPane);
}
}
이 시도!!
참고 URL : https://stackoverflow.com/questions/2320812/jtable-wont-show-column-headers
반응형
'Program Club' 카테고리의 다른 글
| URL (ASP.NET MVC)에서보기 내 현재 경로 ID를 얻는 방법 (0) | 2020.10.15 |
|---|---|
| Devise의 token_authenticatable은 안전한가요? (0) | 2020.10.15 |
| 한 ArrayList의 내용을 다른 ArrayList에 어떻게 복사합니까? (0) | 2020.10.15 |
| Android Studio의 XML 속성 순서 (0) | 2020.10.15 |
| Kibana는 "검색"탭에 결과를 표시하지 않습니다. (0) | 2020.10.15 |