使用多行在 jtable 单元格中显示结果集

Posted

技术标签:

【中文标题】使用多行在 jtable 单元格中显示结果集【英文标题】:display resultset in jtable cell with multiline 【发布时间】:2014-10-19 13:00:43 【问题描述】:

我正在从mysql 中提取数据库记录,我可以使用以下方法完成此操作

private void txt_clientNameKeyReleased(java.awt.event.KeyEvent evt)                                            

    String sql = "Select client_id as 'Client ID', name_lastName as 'Full Name', case_number as 'Case #', hearing_date as 'Hearing Date', rtc_mtc as 'RTC/MTC', place as 'Place', status as 'Status', note as 'Notes', billing as 'Billed' from hearing_history where name_lastName like '%"+ txt_clientName.getText() +"%' ";

    try
        if(conn==null)

            conn = ConnectDB.ConDB();
        else
            conn = ConnectDB.ConDB();
        

        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();

        jTable1.setModel(DbUtils.resultSetToTableModel(rs));            

    catch(Exception e)
        JOptionPane.showMessageDialog(null, e);
    finally 
        close();

    
 

它工作正常,但我有一列应该有多行或换行以增加单元格的高度。

我现在使用 java swing 4 个月了,我还没有看到/遇到过高级技巧来操纵jtable,因为我希望它执行。

我搜索了这个网站,并从这个链接找到了最短的方法

Auto adjust the height of rows in a JTable

来自 camickr

private void updateRowHeights()

try 

    for (int row = 0; row < table.getRowCount(); row++)
    
        int rowHeight = table.getRowHeight();

        for (int column = 0; column < table.getColumnCount(); column++)
        
            Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
            rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
        

        table.setRowHeight(row, rowHeight);
    

catch(ClassCastException e) 

我认为这确实是一个不错的代码,但我试了一下,发现高度从16(默认行高)增加到20,所以单元格中还有一行。

我在这行之后调用了这个方法

        jTable1.setModel(DbUtils.resultSetToTableModel(rs));            
        updateRowHeights();

我将非常感谢任何能让我走上正轨以使这项工作为我工作的帮助..

提前谢谢..

【问题讨论】:

【参考方案1】:

默认渲染器/编辑器不支持多行。您可以尝试使用自定义TableCellEditor/TableCellRenderer。在表格单元格中检查带有JTextArea 的下一个示例:

import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class TestFrame extends JFrame 

    public TestFrame() 
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    

    private void init() 
        JTable t = new JTable(new Object[][]"a\n\rb","1","a\n\rb","2",new Object[]1,2);

        add(new JScrollPane(t));
        t.getColumnModel().getColumn(0).setCellRenderer(new JTextAreaColumn());
        t.getColumnModel().getColumn(0).setCellEditor(new JTextAreaColumn());
        t.setRowHeight(50);
    


    public static void main(String args[]) 
        new TestFrame();
    


    private class JTextAreaColumn extends AbstractCellEditor implements TableCellRenderer,TableCellEditor 

        private JTextArea area = new JTextArea();
        private JScrollPane pane = new JScrollPane(area);

        @Override
        public Object getCellEditorValue() 
            return area.getText();
        

        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) 
            area.setText(value == null ? "" : value.toString());        
            return pane;
        

        private void setColor(boolean isSelected, JTable table) 
            area.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
            area.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
        

        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) 
            area.setText(value == null ? "" : value.toString());
            setColor(isSelected,table);
            return pane;
        

    


阅读更多tutorial

编辑:

接下来试试,看来是你想要的了;

import java.awt.Component;

import javax.swing.AbstractCellEditor;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.CellEditorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

public class TestFrame extends JFrame 

    private JTable t;


    public TestFrame() 
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    

    private void init() 
        t = new JTable(new Object[][]"a\n\rb","1","a\n\rb\n\rb\n\rb\n\rb","2",new Object[]1,2);

        add(new JScrollPane(t));
        t.getColumnModel().getColumn(0).setCellRenderer(new JTextAreaColumn());
        JTextAreaColumn editor = new JTextAreaColumn();
        editor.addCellEditorListener(getEditorListener());
        t.getColumnModel().getColumn(0).setCellEditor(editor);
        updateRowHeights(t);
    

    private CellEditorListener getEditorListener() 
        return new CellEditorListener() 

            @Override
            public void editingStopped(ChangeEvent e) 
                updateRowHeights(t);
            

            @Override
            public void editingCanceled(ChangeEvent e) 
                updateRowHeights(t);
            
        ;
    

    private void updateRowHeights(JTable table) 
        try 
            for (int row = 0; row < table.getRowCount(); row++) 
                int rowHeight = table.getRowHeight();

                for (int column = 0; column < table.getColumnCount(); column++) 
                    Component comp = table.prepareRenderer(
                            table.getCellRenderer(row, column), row, column);
                    rowHeight = Math.max(rowHeight,
                            comp.getPreferredSize().height);
                

                table.setRowHeight(row, rowHeight);
            
         catch (ClassCastException e) 
        
    

    public static void main(String args[]) 
        new TestFrame();
    


    private class JTextAreaColumn extends AbstractCellEditor implements TableCellRenderer,TableCellEditor 

        private JTextArea area = new JTextArea();
        private JScrollPane pane = new JScrollPane(area);

        @Override
        public Object getCellEditorValue() 
            return area.getText();
        

        @Override
        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) 
            area.setText(value == null ? "" : value.toString());        
            return pane;
        

        private void setColor(boolean isSelected, JTable table) 
            area.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
            area.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground());
        

        @Override
        public Component getTableCellRendererComponent(JTable table,
                Object value, boolean isSelected, boolean hasFocus, int row,
                int column) 
            area.setText(value == null ? "" : value.toString());
            setColor(isSelected,table);
            return area;
        

    


【讨论】:

感谢 alex2410 但我需要的代码应该自动提供行的高度。行高将取决于字符串的长度。有些会有拖线,有些会有 3 条或更多,有些只有 1 条(单)线。我仍在努力解决这个问题,如果有人能提出一种方法,我将不胜感激:) 你可以这样做,只需在getTableCellEditorComponent/getTableCellRendererComponent 中返回area,但你需要使用JScrollPaneJTeatArea

以上是关于使用多行在 jtable 单元格中显示结果集的主要内容,如果未能解决你的问题,请参考以下文章

什么是JTable的任何单元格中的更改的正确事件?

在 JTable 的单元格中添加按钮以及数据?

如何使用自定义 JTable 单元格编辑器和单元格渲染器

JTable-在单元格中绘制内容(文本)

JTable排序不是单元格中的好值

更改单元格颜色而不更改其他单元格中的颜色(Jtable)