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

时间:2014-10-19 13:00:44

标签: java mysql swing jtable row-height

我从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 个答案:

答案 0 :(得分:2)

默认渲染器/编辑器不支持多行。您可以尝试使用自定义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;
        }

    }

}

enter image description here

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;
        }

    }

}