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