0

For Java Kepler Eclipse and JTable, I am trying to make a column work as JTextArea, JTextPane or something like that. I need the text to start at the top of the cell and change line when it reaches the border of the cell with a scrollBar if the text doesnt fit in the cell. All cells will be filled and I need them to work that way not just when they are clicked, except maybe for the scrollBar which can appear when cell is clicked but it is not important if it appears when cell is clicked or if it appears from the begining for all cells, whatever is easier.

My project is actualy very different but I wrote this mini one with the table so you can copy, paste and run it to see exactly what the problem is.

What do I add to my code or how do I alter it to make it do this?

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

When implamenting the next code it only works when cell is clicked and all cells are editable. I need cells to be uneditable, this table is just so user can view data that he has previously entered, not to be able to type or change it.

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

    }
}
Vicki K
  • 23
  • 8
  • Also consider the alternate approach suggested [here](http://stackoverflow.com/a/11782366/230513). – trashgod Aug 04 '14 at 16:54
  • thank you for replying @trashgod, but your answer was not helpful – Vicki K Aug 04 '14 at 18:39
  • @alex2410 you were very helpful in another question I had for jTable, I tried altering the code from your answer from this question http://stackoverflow.com/questions/25015808/java-eclipse-jtable-cell-column?noredirect=1#comment38900155_25015808 to make it do what I need for this table but it I couldnt do it. I was hoping you could help again. – Vicki K Aug 04 '14 at 18:45
  • how is this related with Eclipse? are you using SWT? – fortran Aug 04 '14 at 19:31
  • @fortran I am using Swing, I mention eclipse Kepler because from what I know it works a bit differantly. Since I am guessing you know a bit more about it than me, if I am wrong please remove the eclipse tag or tell me to remove it. – Vicki K Aug 04 '14 at 20:45
  • I added `table.getColumn("PROGRAM").setCellEditor(new TextAreaCellRenderer());` and adjusted `TextAreaCellRenderer` to implement `TableCellEditor` (just return `scroll` in `getTableCellEditorComponent` like with the renderer) and things improved (also let `isCellEditable` return true for column 1). Is that what you were looking for? – vanOekel Aug 05 '14 at 20:42
  • @vanOekel thank you for replying! I did what you suggesed and I added the new code to my question above as another program. But I need it to work for all cells and not only when they are clicked -accept for the scroll, that can work when cell is clicked, but text should show (like it shows when clicked) at all times-. Also I need them to be uneditable. – Vicki K Aug 05 '14 at 22:54
  • @VickiK And now combine: have `TextAreaCellRenderer` implement both `TableCellRenderer` and `TableCellEditor` and use it with both `setCellRenderer` and `setCellEditor`. That is what I meant with "add" and "adjusted" - I did not replace or remove anything. – vanOekel Aug 05 '14 at 23:11
  • @vanOekel thank you, it works! But is there a way to make it work like that and have cells be uneditable? – Vicki K Aug 05 '14 at 23:35

2 Answers2

0

Just rightclick the Jtable in the Design section and press surround with then javax.swing.JScrollPane

  • You dont understand, I want to add a scroll bar to each cell in a spacific column, try running the code I have above and you will understand what the problem is. if there is a way to add scrollPane or scrollBar to a cell (like it is added to the table) I can add it to each cell in that column. can you help me with that? – Vicki K Aug 05 '14 at 13:42
0

Sharing the knowledge... how to make a cell work as JTextPane, with individual scrollPane, and also how to make it uneditable if you want. It is runable for anyone who wants to copy and run the code. If you do not have Eclipse Kepler you might need to add a few 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;
                }
            }
        }
Vicki K
  • 23
  • 8