JTable 列标题不可见

发布于 2024-11-07 21:24:37 字数 375 浏览 0 评论 0原文

列标题在我的 JTable 中不可见 我创建了一个 JPanel 并将 JTable 添加到 JPanel。

Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3"},
                { "Row2-Column1", "Row2-Column2", "Row2-Column3"} };
            Object columnNames[] = { "Column One", "Column Two", "Column Three"};
            JTable jTable = new JTable(rowData, columnNames); 

column header not visible in my JTable i have created a JPanel and added the JTable to the JPanel.

Object rowData[][] = { { "Row1-Column1", "Row1-Column2", "Row1-Column3"},
                { "Row2-Column1", "Row2-Column2", "Row2-Column3"} };
            Object columnNames[] = { "Column One", "Column Two", "Column Three"};
            JTable jTable = new JTable(rowData, columnNames); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

欢你一世 2024-11-14 21:24:37

JTable 的 API 指出:

“请注意,如果您希望在独立视图(外部
JScrollPane)并希望显示标题,您可以使用
getTableHeader() 并单独显示。”

或者只是将表格添加到滚动窗格,然后将滚动窗格添加到面板...

The API for JTable states:

"Note that if you wish to use a JTable in a standalone view (outside
of a JScrollPane) and want the header displayed, you can get it using
getTableHeader() and display it separately."

Or just add the table to a scrollpane, and add your scrollpane to the panel...

枯叶蝶 2024-11-14 21:24:37

下面介绍了如何在不使用 JScrollPane 的情况下向 JTable 添加标头:

int rows = 21;
int cols = 3;
JTable table = new JTable(rows, cols);
JTableHeader header = table.getTableHeader();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(header, BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);

Here's how to add a header to a JTable without using a JScrollPane:

int rows = 21;
int cols = 3;
JTable table = new JTable(rows, cols);
JTableHeader header = table.getTableHeader();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(header, BorderLayout.NORTH);
panel.add(table, BorderLayout.CENTER);
℡寂寞咖啡 2024-11-14 21:24:37

您可以将以下代码添加到您的代码中

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setPreferredSize(new Dimension(0, 0));
table.getTableHeader().setDefaultRenderer(renderer);

You can add following code into your code

DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setPreferredSize(new Dimension(0, 0));
table.getTableHeader().setDefaultRenderer(renderer);
笑梦风尘 2024-11-14 21:24:37

具有动态数据的最佳 JTable 示例(只需粘贴到编辑器中并作为 Java 应用程序执行即可顺利工作):

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableTest {

private JFrame frame;
private JTable table;

/**
 * Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            TableTest window = new TableTest();
            window.frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}

/**
 * Create the application.
 */
public TableTest() {
initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

table = new JTable();

DefaultTableModel daDefaultTableModel = new DefaultTableModel(0, 0);

String[] columnNames = new String[] {"Column Header1", "Column Header2", "Column 
Header3"};

daDefaultTableModel.setColumnIdentifiers(columnNames);

//Dummy data for Table
daDefaultTableModel.addRow(new Object[] {"Test1","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test2","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test3","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test4","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test5","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test6","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test7","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test8","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test9","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test10","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test11","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test12","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test13","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test14","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test15","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test16","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test17","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test18","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test19","Test2","Test3"});

table.setModel(daDefaultTableModel);

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 38, 414, 212);
frame.getContentPane().add(scrollPane);



  }
}

Best JTable Example (Working smoothly just paste in editor and execute as Java application) with dynamic data :

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class TableTest {

private JFrame frame;
private JTable table;

/**
 * Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
    public void run() {
        try {
            TableTest window = new TableTest();
            window.frame.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});
}

/**
 * Create the application.
 */
public TableTest() {
initialize();
}

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);

table = new JTable();

DefaultTableModel daDefaultTableModel = new DefaultTableModel(0, 0);

String[] columnNames = new String[] {"Column Header1", "Column Header2", "Column 
Header3"};

daDefaultTableModel.setColumnIdentifiers(columnNames);

//Dummy data for Table
daDefaultTableModel.addRow(new Object[] {"Test1","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test2","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test3","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test4","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test5","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test6","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test7","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test8","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test9","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test10","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test11","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test12","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test13","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test14","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test15","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test16","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test17","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test18","Test2","Test3"});
daDefaultTableModel.addRow(new Object[] {"Test19","Test2","Test3"});

table.setModel(daDefaultTableModel);

JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setBounds(10, 38, 414, 212);
frame.getContentPane().add(scrollPane);



  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文