如何使Java Eclipse Jtable Cell / Column作为可滚动的TextField工作

时间:2014-08-04 16:35:38

标签: java eclipse swing jtable tablecellrenderer

对于Java Kepler Eclipse和JTable,我试图将列作为JTextArea,JTextPane或类似的东西。我需要文本从单元格的顶部开始,如果文本不适合单元格,则在使用scrollBar到达单元格边框时更改行。所有单元格都将被填充,我需要它们以这种方式工作,而不仅仅是在它们被单击时,除了可以在单击单元格时出现的scrollBar,但是如果在单击单元格时出现或者从单元格出现时它不重要开始所有细胞,无论更容易。

我的项目实际上是非常不同的,但我用表格写了这个迷你版本,所以你可以复制,粘贴和运行它以确切地看到问题所在。

我将如何添加到我的代码中,或者如何更改它以使其执行此操作?

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class JavaTestOne {
    JFrame frmApp;
    private JTable table;
    DefaultTableModel model;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaTestOne window = new JavaTestOne();
                    window.frmApp.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });  
    }
    public JavaTestOne() {  
        initialize();
    }
    private void initialize() {
        frmApp = new JFrame();
        frmApp.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
        frmApp.setBounds(300, 100, 500, 300);
        frmApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmApp.getContentPane().setLayout(new CardLayout(0, 0));
        frmApp.setTitle("App");
        {
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(50, 50, 100, 100);
            frmApp.add(scrollPane);
            {
                table = new JTable();
                table.setFillsViewportHeight(true);
                Object[][] data = {
                        {"Monday", ""},
                        {"Tuesday", ""},
                        {"Wednesday", ""}};
                String[] cols = {"DAY","PROGRAM"};
                model = new DefaultTableModel(data, cols) {

                    public boolean isCellEditable(int row, int col) {
                        return false;
                    }

                    public Class getColumnClass(int column) {
                        return getValueAt(0, column).getClass();
                    }

                };

                table.setModel(model);
                table.setRowHeight(100);
                table.getColumn("PROGRAM").setCellRenderer(new TextAreaCellRenderer());
                scrollPane.setViewportView(table);
                }
            model.setValueAt("very large text from database (with resultset (I dont know it that makes a differance) ----------more text-------------- ------- more text------------ -----------more text--------- -------more text-------- -------more text----------- -------more text-------- ---------more text--------", 0, 1);
            model.setValueAt("very large text from database (with resultset) ----------more text-------------- ------- more text------------ -----------more text--------- -------more text-------- -------more text----------- -------more text-------- ---------more text--------", 1, 1);
        }
    }

    public class TextAreaCellRenderer extends AbstractCellEditor implements
          TableCellRenderer {
          private JTextPane textPane;
          private JScrollPane scroll;

          public TextAreaCellRenderer() {
              textPane = new JTextPane();
            scroll = new JScrollPane(textPane);
          }
          public JTextPane getTextArea(){
                return textPane;
          }

          public Object getCellEditorValue() {
            return textPane.getText();
          }

          public Component getTableCellRendererComponent(JTable table,
           Object value, boolean isSelected, boolean hasFocus, int row, int column){
              textPane.setText((value != null) ? value.toString() : "");
            return scroll;
          }
    }
}

当填充下一个代码时,它仅在单击单元格并且所有单元格都可编辑时有效。我需要单元格是不可编辑的,这个表只是让用户可以查看他之前输入的数据,而不是能够输入或更改它。

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class JavaTestOne {
    JFrame frmApp;
    private JTable table;
    DefaultTableModel model;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaTestOne window = new JavaTestOne();
                    window.frmApp.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });  
    }
    public JavaTestOne() {  
        initialize();
    }
    private void initialize() {
        frmApp = new JFrame();
        frmApp.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
        frmApp.setBounds(300, 100, 500, 300);
        frmApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmApp.getContentPane().setLayout(new CardLayout(0, 0));
        frmApp.setTitle("App");
        {
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(50, 50, 100, 100);
            frmApp.add(scrollPane);
            {
                table = new JTable();
                table.setFillsViewportHeight(true);
                Object[][] data = {
                        {"Monday", ""},
                        {"Tuesday", ""},
                        {"Wednesday", ""}};
                String[] cols = {"DAY","PROGRAM"};
                model = new DefaultTableModel(data, cols) {

                    boolean[] canEdit = new boolean[]{
                            false, true
                    };
                    public boolean isCellEditable(int row, int col) {
                        return canEdit[col];
                    }

                    public Class getColumnClass(int column) {
                        return getValueAt(0, column).getClass();
                    }

                };

                table.setModel(model);
                table.setRowHeight(100);
                table.getColumn("PROGRAM").setCellEditor(new TextAreaCellRenderer());
                scrollPane.setViewportView(table);
                }
            model.setValueAt("very large text from database (with resultset (I dont know it that makes a differance) ----------more text-------------- ------- more text------------ -----------more text--------- -------more text-------- -------more text----------- -------more text-------- ---------more text--------", 0, 1);
            model.setValueAt("very large text from database (with resultset) ----------more text-------------- ------- more text------------ -----------more text--------- -------more text-------- -------more text----------- -------more text-------- ---------more text--------", 1, 1);
        }
    }

    public class TextAreaCellRenderer extends AbstractCellEditor implements
    TableCellEditor{
          private JTextPane textPane;
          private JScrollPane scroll;

          public TextAreaCellRenderer() {
              textPane = new JTextPane();
            scroll = new JScrollPane(textPane);
          }
          public JTextPane getTextArea(){
                return textPane;
          }

          public Object getCellEditorValue() {
            return textPane.getText();
          }


        public Component getTableCellEditorComponent(JTable table,
                Object value, boolean isSelected, int row, int column) {
            textPane.setText((value != null) ? value.toString() : "");
            return scroll;
        }

    }
}

2 个答案:

答案 0 :(得分:0)

右键单击“设计”部分中的Jtable,然后按下环绕声,然后按javax.swing.JScrollPane

答案 1 :(得分:0)

分享知识...如何使单元格作为JTextPane工作,使用单独的scrollPane,以及如何根据需要使其无法使用。它适用于任何想要复制和运行代码的人。如果您没有Eclipse Kepler,则可能需要添加一些Overrides

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;

public class JavaTestOne {
    JFrame frmApp;
    private JTable table;
    DefaultTableModel model;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    JavaTestOne window = new JavaTestOne();
                    window.frmApp.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });  
    }
    public JavaTestOne() {  
        initialize();
    }
    private void initialize() {
        frmApp = new JFrame();
        frmApp.getContentPane().setFont(new Font("Tahoma", Font.PLAIN, 13));
        frmApp.setBounds(300, 100, 500, 300);
        frmApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmApp.getContentPane().setLayout(new CardLayout(0, 0));
        frmApp.setTitle("App");
        {
            JScrollPane scrollPane = new JScrollPane();
            scrollPane.setBounds(50, 50, 100, 100);
            frmApp.add(scrollPane);
            {
                table = new JTable();
                table.setFillsViewportHeight(true);
                Object[][] data = {
                        {"Monday", ""},
                        {"Tuesday", ""},
                        {"Wednesday", ""}};
                String[] cols = {"DAY","PROGRAM"};
                model = new DefaultTableModel(data, cols) {

                    public boolean isCellEditable(int row, int col) {
                        return true;
                    }

                    public Class getColumnClass(int column) {
                        return getValueAt(0, column).getClass();
                    }

                };

                table.setModel(model);
                    table.setRowHeight(100);
//add getCellEditor for the individual cell, and setCellRenderer for the column
                    table.getColumn("PROGRAM").setCellEditor(new TextAreaCellRenderer());
                    table.getColumn("PROGRAM").setCellRenderer(new TextAreaCellRenderer());
                    scrollPane.setViewportView(table);
                    }
                model.setValueAt("very large text from database ------- more text------------ ------- more text------------ ----------more text-------------- ------- more text------------ -----------more text--------- -------more text-------- -------more text----------- -------more text-------- ---------more text--------", 0, 1);
                model.setValueAt("very large text from database (with resultset) ----------more text-------------- ------- more text------------ -----------more text--------- -------more text-------- -------more text----------- -------more text-------- ---------more text--------", 1, 1);
            }
        }

        public class TextAreaCellRenderer extends AbstractCellEditor implements
              TableCellRenderer,TableCellEditor {
              private JTextPane textPane;
              private JScrollPane scroll;

              public TextAreaCellRenderer() {
                  textPane = new JTextPane();
                scroll = new JScrollPane(textPane);
              }
              public JTextPane getTextArea(){
                    return textPane;
              }

              public Object getCellEditorValue() {
                return textPane.getText();
              }
    //this is what makes it work for the for the column


         public Component getTableCellRendererComponent(JTable table,
                   Object value, boolean isSelected, boolean hasFocus, int row, int column){
                      textPane.setText((value != null) ? value.toString() : "");
                    return scroll;
                  }

    //this is what makes it work for the cell
                public Component getTableCellEditorComponent(JTable table, 
                        Object value, boolean isSelected, int row, int column) {
                    textPane.setText((value != null) ? value.toString() : "");
                    textPane.setEditable(false); //this is what makes it uneditable
                    return scroll;
                }
            }
        }