2

に を配置してJTableJPanelますが、表示すると、テーブルの内容は表示されますが、列名は表示されません。

public class Neww extends JPanel
{
    Connection conn = null;
    Statement st;
    DefaultTableModel model = new DefaultTableModel(new Object[][] {
        { " ", " " },
        { " ", " " },
        { " ", " " },
        { " ", " " },
        { " ", " " },
        { " ", " " }
    }, new Object[] {
        "ItemName",
        "No of items"
    });

    JTable table = new JTable(model);
    TableColumn ItemName = table.getColumnModel().getColumn(0);
    JComboBox comboBox = new JComboBox();
    Neww()
    {
        this.setLayout(new FlowLayout(FlowLayout.CENTER));
        this.add(table);

        comboBox.addItem("Spoon");
        comboBox.addItem("Plate");
        comboBox.addItem("Mixer");
        comboBox.addItem("Glass");
        ItemName.setCellEditor(new DefaultCellEditor(comboBox));
    }
}
4

3 に答える 3

3

2つの方法があります

  • (適切な方法) に配置JTableする必要がありJScrollPane、その後表示されJTableHeaderます

  • get JTableHeaderfrom JTable( JPanelsLayoutManagerを に変更BorderLayout) および putNORTH areaJPanel

于 2012-12-05T10:59:44.050 に答える
2

1) で包みJTableますJScrollPane。このような:

JTable table=...;

JPanel container = new JPanel();

JScrollPane jsp=new JScrollPane(table);

container.add(jsp);

2) 必要に応じて使用getTableHeader()および追加します (通常は北に配置します)。

...

JTableHeader header = table.getTableHeader();

JPanel container = new JPanel(new BorderLayout());

// Add header at NORTH position
container.add(header, BorderLayout.NORTH);

//Add table below header
container.add(table, BorderLayout.CENTER);
于 2012-12-05T11:00:05.923 に答える
1

次のステートメントは、コードに問題を引き起こしています。

 this.add(table);

テーブルの追加中にスクロール ペインを使用します。

 add(new JScrollPane(table));

なぜ使用する必要があるのJScrollPaneですか?

このコメントで@OscarRyzが述べたように。

JScrollPane のトップ コンポーネント (またはトップ ビューなどの名前が付けられたもの) に配置されたテーブル ヘッダー トップ コンポーネントがない場合、JTable はヘッドレスに見えます。これは仕様によるものです。

編集:詳細については、この回答もご覧ください。

于 2012-12-05T12:10:04.197 に答える