JTable cell Render Problem!

Hello,

I wrote a program which simply display News Item inside JTable. If I click once it pops up a window with the detail news. My Problem is If I click twice it goes to the edit mode. I don't want the cell to be editable. It should be always uneditable.

If anybody helps me where to change the code and what it should be I would very grateful.

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.table.*;

publicclass Newsextends JPanel

{

JTable table;

DefaultTableModel model;

JPanel buttonPanel;

JButton button;

Object[][] data ;

int SIZE = 0;

String[] columnNames ={"News"};

Chat chatClient;

JScrollPane jScrollPane1 =new JScrollPane();

JTable jTable1;

JScrollPane scrollPane =new JScrollPane();

Object[] newRow =new Object[1];

int noOfColumn = 1;

int sessionid = 0;

int totalrow;

String newsDetail =new String ();

String [] detailNews =new String [6000];

public News (){

try{

jbInit();

}

catch(Exception e){

e.printStackTrace();

}

}

public News(Chat chatclient)

{

this.chatClient = chatclient;

try{

jbInit();

}

catch(Exception e){

e.printStackTrace();

}

// Create table

data =new Object [SIZE][noOfColumn];

for (int i = 0; i < SIZE;i++){

for (int j = 0; j < noOfColumn; j++){

data[i][j] = Integer.toString(i);

}

}

model =new DefaultTableModel(data, columnNames);

table =new JTable(model);

for (int i = 0; i < noOfColumn;i++){

table.getColumnModel().getColumn(i).setPreferredWidth(250);

}

scrollPane =new JScrollPane( table );

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

scrollPane.setSize(30,30);

add(scrollPane,"LEFT");

}

publicvoid setRow(String news)

{

newRow[0] = news;

}

publicvoid setDetailNews (String detail){

this.newsDetail = detail;

}

public Object [] getRow(){

return newRow;

}

publicvoid addRow(){

model.insertRow(0, getRow());

totalrow = table.getRowCount();

int row_select = totalrow - 1;

detailNews[row_select] = newsDetail;

table.changeSelection(row_select, 0, false,false);

table.requestFocusInWindow();

}

publicstaticvoid main(String[] args)

{

News frame =new News();

}

privatevoid jbInit()throws Exception{

this.setBackground(new Color(233, 236, 216));

this.setAlignmentY((float) 0.5);

// Create table

data =new Object [SIZE][noOfColumn];

for (int i = 0; i < SIZE;i++){

for (int j = 0; j < noOfColumn; j++){

data[i][j] = Integer.toString(i);

}

}

model =new DefaultTableModel(data, columnNames);

table =new JTable(model);

for (int i = 0; i < noOfColumn;i++){

table.getColumnModel().getColumn(i).setPreferredWidth(250);

table.getColumnModel().getColumn(i).setCellRenderer(new MyCellRenderer());

}

table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

table.addMouseListener(new MouseAdapter(){

publicvoid mouseClicked(MouseEvent e){

if (e.getClickCount() == 1){

JTable target = (JTable) e.getSource();

int row = target.getSelectedRow();

int column = target.getSelectedColumn();

String message = detailNews[totalrow - row - 1];

if (!message.trim().equals("")){

DetailNewsWindow news =new DetailNewsWindow();

news.jTextArea1.setText(message);

}

}

}

});

scrollPane =new JScrollPane( table );

scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

scrollPane.setFont(new java.awt.Font("MS Sans Serif", 0, 11));

scrollPane.setSize(191,258);

table.setFont(new java.awt.Font("Dialog", 1, 10));

add(scrollPane,"LEFT");

}

}

class MyCellRendererextends JTextAreaimplements TableCellRenderer{

public MyCellRenderer(){

setLineWrap(true);

setWrapStyleWord(true);

//setEditable(false); doesn't work

setFont(new java.awt.Font("SansSerif", Font.PLAIN, 12));

}

public Component getTableCellRendererComponent(JTable table, Object

value,boolean isSelected,boolean hasFocus,int row,int column){

setText(value.toString());

if(row %2 == 0){

setBackground(new Color(255,235,221));

}

else{

setBackground(new Color(208,255,221));

}

setSize(table.getColumnModel().getColumn(column).getWidth(),

getPreferredSize().height);

if (table.getRowHeight(row) != getPreferredSize().height){

table.setRowHeight(row, getPreferredSize().height);

}

returnthis;

}

}

Thanks.

[10136 byte] By [ishraka] at [2007-11-26 21:52:09]
# 1

You are using DefaultTableModel which, by default, allows table cells to be edited. You can change this behavior by overriding isCellEditable() and returning false. You can do this in one of two ways:

1) Create a subclass of DefaultTableModel and override the method to return false. Use an instance of this subclass as your table's model.

2) Override the method inline.

model = new DefaultTableModel(data, columnNames) {

public boolean isCellEditable() {

return false;

}

};

If you do not need to override any other functionality of DefaultTableModel then using option 2 is the quickest and easiest.

smalinowa at 2007-7-10 3:45:54 > top of Java-index,Desktop,Core GUI APIs...
# 2
Although option 2 is just an example of option 1 :o)
itchyscratchya at 2007-7-10 3:45:54 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks a lot. It works. I have one more thing to know. Each cell in this table represents News headline and some Headline has background news, some don't have. I want those cell to be in bold format and different color which has the background news while which don't have background news will be in plain text headline.

With the same code is it possible to do that?

Thanks again.

ishraka at 2007-7-10 3:45:54 > top of Java-index,Desktop,Core GUI APIs...
# 4
This posting has two approaches. My posting may be the easiest: http://forum.java.sun.com/thread.jspa?forumID=57&threadID=610474
camickra at 2007-7-10 3:45:54 > top of Java-index,Desktop,Core GUI APIs...
# 5
Thanks a lot. It really helped me a lot.
ishraka at 2007-7-10 3:45:54 > top of Java-index,Desktop,Core GUI APIs...