代码之家  ›  专栏  ›  技术社区  ›  nix

eclipse中的JTable不显示列名

  •  1
  • nix  · 技术社区  · 10 年前

    当我手动添加数据时,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);
    
                    }
                });
    

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

    picture

    3 回复  |  直到 10 年前
        1
  •  1
  •   Vishal Gajera    10 年前

    对代码进行以下更改:,

    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出售:

    enter image description here

        2
  •  1
  •   f1sh    10 年前

    您不是将JTable添加到滚动窗格,而是添加到 contentpane 。请尝试以下操作:

    contentPane.add(scroll);
    scroll.add(table); //instead of contentPane.add(table)
    
        3
  •  1
  •   Community Mohan Dere    9 年前

    这个 JTable 列标题为 displayed JScrollPane ,因此这是正确的:

    contentPane.add(scroll);
    

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

    //contentPane.add(table);
    

    而不是 setBounds() 推翻 getPreferredScrollableViewportSize() ,如建议 here 。因为默认布局 JPanel FlowLayout ,表格将保持固定大小。考虑 GridLayout ,这将允许显示在调整包围框的大小时反映更改。

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