繁体   English   中英

eclipse中的JTable没有显示列名

[英]JTable in eclipse not showing column names

当我手动添加数据时,JTable都没有将列名显示为标题,也没有滚动条。

JTable和JScrollPane的代码:

 private JPanel contentPane = new JPanel();
    Object[] title = {"ISBN", "Name", "Author", "Shelf No", "Row No", "Col No"};
    DefaultTableModel dtm = new DefaultTableModel();
    dtm.setColumnIdentifiers(title);
    table = new JTable(dtm);
    table.setBounds(23, 55, 435, 217);
    table.setModel(dtm);
    JScrollPane scroll = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    table.setForeground(Color.gray);
    table.setRowHeight(30);
    contentPane.add(scroll);
    contentPane.add(table);
    Object[] row = {"hi", "2", "3", "5", "r", "we" };

ActionListener在表中添加数据:

    btnSearch.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {

                    dtm.addRow(row);

                }
            });

如下图所示,我没有这些列(标题)和滚动条。

图片

您不添加您的JTable到滚动,而是你contentpane 试试这个:

contentPane.add(scroll);
scroll.add(table); //instead of contentPane.add(table)

JTable列标题显示JScrollPane ,所以这是正确的:

contentPane.add(scroll);

不要立即用表替换滚动窗格:

//contentPane.add(table);

而不是setBounds() ,覆盖getPreferredScrollableViewportSize() ,如此处所示 由于JPanel的默认布局是FlowLayout ,因此该表将保持固定大小。 考虑使用GridLayout ,它将允许显示在调整封闭框架大小时反映更改。

JPanel contentPane = new JPanel(new GridLayout());

对您的代码进行以下更改:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.my.classes.Validation;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

public class SearchBooks extends JFrame {

    private JPanel contentPane;
    private JTextField txtIsbn;
    private Validation v = new Validation();
    private JTable table;
    DefaultTableModel dtm ;
    Object[]  row = {"hi", "2", "3", "5", "r", "we" };


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

    /**
     * Create the frame.
     */
    public SearchBooks() {
        setTitle("Search Books from Database");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //setBounds(100, 100, 502, 341); instead of it use pack() see below it's uses at right place not here...
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        //contentPane.setLayout(null);

        JLabel lblBookName = new JLabel("Book Name: ");
        lblBookName.setFont(new Font("Tahoma", Font.BOLD, 11));
        lblBookName.setBounds(23, 11, 80, 14);
        contentPane.add(lblBookName);

        txtIsbn = new JTextField();
        txtIsbn.setBounds(113, 8, 202, 20);
        contentPane.add(txtIsbn);
        txtIsbn.setColumns(10);

        JButton btnSearch = new JButton("Search");
        btnSearch.setFont(new Font("Tahoma", Font.BOLD, 11));
        btnSearch.setBounds(338, 7, 103, 23);
        contentPane.add(btnSearch);

        Object[] title = {"ISBN", "Name", "Author", "Shelf No", "Row No", "Col No"};
        dtm = new DefaultTableModel(); 
        dtm.setColumnIdentifiers(title);
        table = new JTable(dtm);
        table.setBounds(23, 55, 435, 217);
        table.setModel(dtm);
        //JScrollPane scroll = new JScrollPane(table, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        JScrollPane scroll = new JScrollPane(table);
        table.setFillsViewportHeight(true);
        table.setForeground(Color.RED);
        table.setRowHeight(30);
        contentPane.add(scroll);

        // contentPane.add(table); //comment it...

        //dtm.addRow(title); doesn't required because already header is there...
        pack();

        txtIsbn.addKeyListener(
                new KeyAdapter() {
                    public void keyPressed(KeyEvent ev) {
                        if(ev.getKeyCode() == KeyEvent.VK_ENTER) {

                            if(v.validateIsbn(txtIsbn.getText())  || v.validateAddress(txtIsbn.getText())) {

                            }else {
                                JOptionPane.showMessageDialog(null, "Error! Please check your input");
                            }
                        }
                    }
                });

        btnSearch.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        table.setForeground(Color.BLACK);
                        dtm.addRow(row);

                    }
                });
    }
}

Otu-Put:

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM