aspose file tools
The moose likes Swing / AWT / SWT and the fly likes problem in JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem in JTable" Watch "problem in JTable" New topic

problem in JTable

Anagha Medicharla

Joined: Feb 06, 2001
Posts: 11
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

Joined: Feb 24, 2001
Posts: 97
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

Joined: Jun 03, 2000
Posts: 5040
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
Thanks for your cooperation.
Also, you could edit your posts by clicking on the
notepad/pencil icon.
- satya

Take a Minute, Donate an Hour, Change a Life
Have you tried LearnNowOnline?
subject: problem in JTable