JTable 列名不打印 [英] JTable column names not printing

查看:54
本文介绍了JTable 列名不打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我创建 2 个向量(数据、列名)并在 JTable 中使用它们来显示带有列标题的行时,它只显示行而不是列标题.

When i create 2 vectors (data, columnnames)and use them in a JTable to display rows with a column header it just displays the rows and not the column header.

在 JOptionPane 中,它们的列标题显示良好.我假设粗体是哪里出错了,因为它没有在那里正确获取列名.

In a JOptionPane they the column headers show up fine. I assume where bolded is where its going wrong as its not getting column names properly there.

public void displayLettingProperties() 抛出 SQLException{

public void displayLettingProperties() throws SQLException{

    Connection conn = null;
    try {       
        conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
        System.out.println("Connected to database.");       

         // The Connection is obtained

        Statement Stmt = (Statement) conn.createStatement();
    //  Stmt.execute(createPropertyTable);

        ResultSet rs = Stmt.executeQuery("select * from PropertyLettings ORDER BY propertyBedrooms ASC");

        // It creates and displays the table
        **JTable table = new JTable(buildTableModel(rs));**


     // Set Column Widths

        table.getColumnModel().getColumn(0).setPreferredWidth(100);
        table.getColumnModel().getColumn(1).setPreferredWidth(50);
        table.getColumnModel().getColumn(2).setPreferredWidth(350);
        table.getColumnModel().getColumn(3).setPreferredWidth(100);
        table.getColumnModel().getColumn(4).setPreferredWidth(100);
        table.getColumnModel().getColumn(5).setPreferredWidth(350);
        table.getColumnModel().getColumn(6).setPreferredWidth(100);
        table.getColumnModel().getColumn(7).setPreferredWidth(130);



     //   JOptionPane.showMessageDialog(null, new JScrollPane(table));

        final JPanel panelOne = new JPanel();
        panelOne.setVisible(true);
        panelOne.setBackground(Color.LIGHT_GRAY);
        // JFRAME
        final JFrame topFrame = new JFrame();
        topFrame.setSize(1550, 300);
        topFrame.setLocationRelativeTo ( null );
        topFrame.setVisible(true);
        topFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        // PUT TOGETHER
        topFrame.add(panelOne);

        panelOne.add(table);


        panelOne.revalidate();
        panelOne.repaint();


    } catch (SQLException e) {
        System.err.println("Cannot connect to database." + e);


    } finally {

    if(conn != null){
        conn.close();

      }

    } 

推荐答案

正如您在 previous question 使用此代码,您需要在添加到 UI 之前将表添加到 JScrollPane 中.

As you were advised to do in your previous question with this code, you need to add the table into a JScrollPane before adding to the UI.

panelOne.add(new JScrollPane(table));

查看如何使用滚动窗格如何使用表格了解更多详情...

Take a look at How to use scroll panes and How to use tables for more details...

这篇关于JTable 列名不打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆