• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem in JTable  RSS feed

Anagha Medicharla
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
I am using JTable.I want rows that takes multiline text.
I have written the code as follows.what is happening is when I press enter on a cell of that row it's going to next row(means it's is not going to next line of same cell but it is crearing new row and it's gong to that row.)
Can anybody help me .
I am giving cod here:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import javax.swing.DefaultCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.JOptionPane;
import javax.swing.border.*;
public class ConersationLogInfo extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JScrollPane jScrollPane1 = new JScrollPane();
//Construct the frame
public ConersationLogInfo() {
try {
catch(Exception e) {
//Component initialization
private void jbInit() throws Exception{
contentPane = (JPanel) this.getContentPane();
this.setSize(new Dimension(576, 300));
this.setTitle("CONVERSATION LOG");
jPanel1.setMinimumSize(new Dimension(80, 80));
jPanel1.setPreferredSize(new Dimension(80, 80));
jScrollPane1.setBounds(new Rectangle(1, 10, 573, 218));
contentPane.add(jPanel1, BorderLayout.NORTH);
contentPane.add(jPanel2, BorderLayout.CENTER);
jPanel2.add(jScrollPane1, null);
MyTableModel myModel = new MyTableModel(){
public Class getColumnClass(int columnIndex) {
return String.class;
JTable table = new JTable(myModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
int lines = 2;
table.setRowHeight( table.getRowHeight() * lines);
table.setDefaultRenderer(String.class,new MultiLineCellRenderer());
jScrollPane1.getViewport().add(table, null);

//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
class MyTableModel extends AbstractTableModel {
final String[] colheads={"DATE","TIME","CALLER_ID","REASON","CONVERSATION INFO"};
final String[][] data={{"","","","",""}, {"","","","",""}};
public int getColumnCount() {
return colheads.length;
public int getRowCount() {
return data.length;
public String getColumnName(int col) {
return colheads[col];
public Object getValueAt(int row, int col) {
return data[row][col];
* JTable uses this method to determine the default renderer/
* editor for each cell. If we didn't implement this method,
* then the last column would contain text ("true"/"false"),
* rather than a check box.
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
* Don't need to implement this method unless your table's
* editable.
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.

return true;
class MultiLineCellRenderer extends JTextArea implements TableCellRenderer {
public MultiLineCellRenderer() {
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
} else {
if (hasFocus) {
setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
if (table.isCellEditable(row, column)) {
setForeground( UIManager.getColor("Table.focusCellForeground") );
setBackground( UIManager.getColor("Table.focusCellBackground") );
} else {
setBorder(new EmptyBorder(1, 2, 1, 2));
setText((value == null) ? "" : value.toString());
return this;
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to create a JTextArea-based editor for your JTable. The default editor (which is what you're using) uses a JTextField and stops editing when the Enter key is pressed.
Brett Spell
Author, Professional Java Programming
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when posting huge code samples like this, please,
use the opening tag [ code ] and closing tag [ /code ]
without the spaces, this will indent the code.
Addl. tags are defined at http://www.javaranch.com/ubb/ubbcode.html
Thanks for your cooperation.
Also, you could edit your posts by clicking on the
notepad/pencil icon.
- satya
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!