Rendering cell of JTable

Hi,
I want to know if the cells of JTable can be rendered so that it allows wordwrapping.
Please help.
Cheers,
NNR

public class VariableCellSize extends JTextArea implements TableCellRenderer {
  private static Border focusBorder = BorderFactory.createLineBorder(Color.red);
  private static Border noBorder = BorderFactory.createEmptyBorder();
  public VariableCellSize() {
    setEditable(false);
    setLineWrap(true);
    setWrapStyleWord(true);
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if (isSelected) {
      setBackground(table.getSelectionBackground());
      setForeground(table.getSelectionForeground());
    } else {
      setBackground(table.getBackground());
      setForeground(table.getForeground());
    if (hasFocus) {
      setBorder(focusBorder);
    } else {
      setBorder(noBorder);
    setText(value.toString());
    if (getPreferredSize().height > table.getRowHeight(row)) {
      table.setRowHeight(row, getPreferredSize().height);
    return this;
  /* THE FOLLOWING COPIED FROM javax.swing.table.DefaultTableCellRenderer.....
   * The following methods are overridden as a performance measure to
   * to prune code-paths are often called in the case of renders
   * but which we know are unnecessary. Great care should be taken
   * when writing your own renderer to weigh the benefits and
   * drawbacks of overriding methods like these.
  public void validate() {}
  public void revalidate() {}
  public void repaint(long tm, int x, int y, int width, int height) {}
  public void repaint(Rectangle r) {}
}thomas

Similar Messages

  • Dynamically Rendering and editing JTable cell

    Hi all,
    I wonder if some body can help me.
    As in my application each cell in Jtable returns java Object. and what i am trying to do is based on each object returned in cell my renderer is going to change dynamically. for example if the object returned is primitive data types the renderer is defualt, if the returned object is Map, Set or List, then the Cell Renderer should show clickable button with icon on right hand side and TextField on left hand side and so on for other objects. i am able get renderer for Map, set or list, which is button on right hand side of cell and text field on left hand side displaying toString of Object. then what i want is when i click button it should show those values in popup window and if i double click it should edit the cell which i am able to do it. so how can i handle events or what i have to do to make button clickable. if any body come accross these situations or got idea. please let me know how to move further.
    Thanks,
    Kotesh.

    Hi,
    Thanks for your advise and as i said i am able to get render for Map as (TextField in left hand side+Button with icon in right hand side side) of Cell. but what i need is when i click button which is on right hand, then i should be able to show popup wvindow with map values and if i double click on TextField which is on right hand side, then i should be able to edit Cell which is happening. but i am not able to get popup window when i click button on right hand side of cell. basically i am not sure how to handle events on cell and the button on right hand side?
    if some one can guide me that will be great as i am in big trouble.
    Thanks
    Kotesh.

  • Change the background color of a cell in JTable

    Hi all,
    How can I change the background color of individual cell in JTable. I need to construct my own TableCellRenderer or not? I'm now using the DefaultTableCellRenderer now.
    Thx

    You could create your own renderer or you could try something like:
    table = new JTable(model)
         public TableCellRenderer getCellRenderer(int row, int column)
              DefaultTableCellRenderer tcr =
               (DefaultTableCellRenderer)super.getCellRenderer(row, column);
              if (row == 1 && column == 1)
                   tcr.setBackground(Color.green);
              else
                   tcr.setBackground(Color.red);
              return tcr;
    };

  • Aligning the Text in Centre for MultiLine Cells in JTable

    Hi;
    Does anyone know how I could make a java table that has multiline text cells AND this multiline text is aligned to center? I tried to extend JTextArea and to implement TableRenderer - multiline was OK but it did not do anything on setHorizontalAlign. If I extend DefaultTableRender than I can align the text to center but I got no multiline...
    Any ideas?
    Kindly to provide me a solution for this its urgent.

    Hi;
    Thanks for the reply,on using Text Area as renderer i.e using Multi-Line cells in JTable, i achieved the objective of displaying the text on multiple lines in the cell."In my Table there are 5 columns and i'm setting renderer on 3rd and 5th column,If the text is too large in column 5 it automatically gets wrapped off for all the cells in column 5.For column 3 my text is not so large as compared to column 5 and due to which the Text in the column 3 starts from the top for all the cells i.e (the first row in column 3 is parallel to first row in column 5 ),Now the problem is i can't display the text in the center of all cells for Column 3 .
    Thanks in advance

  • How do i do that? MultiLine in a cell of JTable !!!

    Dear friends,
    I am trying to add multiline in a cell of JTable. For that bellow code i have written, but my statement in cell displays in a line only. Code is given bellow (copy/paste/run), could anyone help me how to do that?
    Regards..
    Jaya.
    import javax.swing.JTable;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    import javax.swing.JOptionPane;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import javax.swing.table.*;
    import java.sql.*;
    public class OtL extends JFrame {
        public OtL()
          initComponents();
        private void initComponents() {
            model1 = new DefaultTableModel();
            btnClose  = new javax.swing.JButton();
            tblOt       = new javax.swing.JTable(model1);
            scrOt   = new javax.swing.JScrollPane();
            TableCellRenderer renderer = new TableRenderer();
            tblOt.setDefaultRenderer(Object.class,renderer);
            tblOt.setRowHeight(tblOt.getRowHeight()*3);
            tblOt.setBackground(new java.awt.Color(250,225,162));
            getContentPane().setLayout(null);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
            btnClose.setText("CLOSE");
            btnClose.setToolTipText("Close This Screen");
            btnClose.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
            btnClose.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                  System.exit(0);
            getContentPane().add(btnClose);
            btnClose.setBounds(380, 470, 110, 25);
            tblOt.setToolTipText("Operation Theatre Booking List");
            scrOt.setViewportView(tblOt);
            tableSize();
            scrOt.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
            scrOt.setToolTipText("Operation Theatre Booking List");
            getContentPane().add(scrOt);
            scrOt.setBounds(15, 150, 770, 300);
            displayOtList();
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
        public static void main(String args[]) {
            JFrame j  = new OtL();
            j.setSize(800,560);
            j.show();
    /**********Code for displaying Operation Theater Booking List****Start Here*************/
        public void displayOtList(){
    Here i fire a sql query and i populate data in table.
    Sometime i get more than 1 value for some column e.g Surgery Name and Surgeon Name, so
    i want to display more than 1 value in the same cell. like bellow i have mentiond for
    5th and 6th column. But its displayin in one line only, how can i display that in multiple line!!
    I know that can be taken care with  Renderer, i tried but couldn't go through. I have written TableRenderer
    class for that, please help me what should i do in Renderer class.
          int row1=0;
          tableSize();
          DefaultTableModel model1 = (DefaultTableModel)tblOt.getModel();
          Object[] newRow = new Object[6];
            for(int i=0;i<1;i++){
              row1 = tblOt.getRowCount() + 1;
              newRow[0] = "1";
              newRow[1] = "10:10";
              newRow[2] = "11:30";
              newRow[3] = "Name Here";
              newRow[4] = "Surgery Name1 \n Surgery Name2";
              newRow[5] = "Dr. Amit \n Dr. Johnson";
              model1.addRow(newRow);
    /**********Code for displaying Operation Theater Booking List****End Here*************/
       class TableRenderer extends DefaultTableCellRenderer{
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean
    hasFocus, int row, int column)
           super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
          setOpaque(true);
           return this;
    /******tableSize()*****code for sizing table******start here()********/
      public void tableSize(){
        tblOt.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {"Ot No.", "Start Time","End Time", "Patient Name", "Surgery Name","Surgeon Name"}
        TableColumn column = null;
        for (int i = 0; i < 6; i++) {
          column = tblOt.getColumnModel().getColumn(i);
          if (i == 0) {
            column.setPreferredWidth(5);
          if (i == 1) {
            column.setPreferredWidth(15);
          if (i == 2) {
          column.setPreferredWidth(15);
          if (i == 3) {
            column.setPreferredWidth(130);
          if (i == 4) {
            column.setPreferredWidth(130);
          if (i == 5) {
            column.setPreferredWidth(130);
    /******tableSize()*****code for sizing table******end here()********/
        private javax.swing.JButton btnClose;
        private javax.swing.JScrollPane scrOt;
        private javax.swing.JTable tblOt;
        private DefaultTableModel model1;
    [\code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

    OK Filne, Thanx .. its displaying data in multiple line now.
    But still there is a problem, whenever i move mouse over the table row it returns error like bellow. I am not able to findout the reason of error. I tried to put code in try{} catch{} block to avoid this error, but still its coming. Help me.....!!!!!!!!
    java.lang.NullPointerException
            at javax.swing.text.View.getViewIndex(View.java:1060)
            at javax.swing.text.View.getToolTipText(View.java:1031)
            at javax.swing.plaf.basic.BasicTextUI.getToolTipText(BasicTextUI.java:1057)
            at javax.swing.text.JTextComponent.getToolTipText(JTextComponent.java:1533)
            at javax.swing.JTable.getToolTipText(JTable.java:2380)
            at javax.swing.ToolTipManager$insideTimerAction.actionPerformed(ToolTipManager.java:669)
            at javax.swing.Timer.fireActionPerformed(Timer.java:256)
            at javax.swing.Timer$DoPostEvent.run(Timer.java:190)
            at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:167)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:437)
            at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:150)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:131)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)my latest programe code is here :
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.border.*;
    import javax.swing.table.*;
    public class OtL extends JFrame {
        public OtL()
          initComponents();
        private void initComponents() {
            model1 = new DefaultTableModel();
            btnClose  = new javax.swing.JButton();
            tblOt       = new javax.swing.JTable(model1);
            scrOt   = new javax.swing.JScrollPane();
            TableCellRenderer renderer = new TableRenderer();
            tblOt.setDefaultRenderer(Object.class,renderer);
            tblOt.setRowHeight(tblOt.getRowHeight()*3);
            tblOt.setBackground(new java.awt.Color(250,225,162));
            getContentPane().setLayout(null);
            addWindowListener(new java.awt.event.WindowAdapter() {
                public void windowClosing(java.awt.event.WindowEvent evt) {
                    exitForm(evt);
            btnClose.setText("CLOSE");
            btnClose.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.RAISED));
            btnClose.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                  System.exit(0);
            getContentPane().add(btnClose);
            btnClose.setBounds(380, 470, 110, 25);
            scrOt.setViewportView(tblOt);
            tableSize();
            scrOt.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
            getContentPane().add(scrOt);
            scrOt.setBounds(15, 150, 770, 300);
            displayOtList();
        private void exitForm(java.awt.event.WindowEvent evt) {
            System.exit(0);
        public static void main(String args[]) {
            JFrame j  = new OtL();
            j.setSize(800,560);
            j.show();
        public void displayOtList(){
          int row1=0;
          tableSize();
          DefaultTableModel model1 = (DefaultTableModel)tblOt.getModel();
          Object[] newRow = new Object[6];
            for(int i=0;i<1;i++){
              row1 = tblOt.getRowCount() + 1;
              newRow[0] = "1";
              newRow[1] = "10:10";
              newRow[2] = "11:30";
              newRow[3] = "Name Here";
              newRow[4] = "Surgery Name1 \n Surgery Name2";
              newRow[5] = "Dr. Amit \n Dr. Johnson";
              model1.addRow(newRow);
       class TableRenderer extends JTextArea implements TableCellRenderer{
          public TableRenderer(){
            setLineWrap(true);
            setWrapStyleWord(true);
            setOpaque(true);
         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean
    hasFocus, int row, int column)
          if (isSelected) {
            setForeground(tblOt.getSelectionForeground());
            setBackground(tblOt.getSelectionBackground());
          } else {
            setForeground(tblOt.getForeground());
          setFont(tblOt.getFont());
          if (hasFocus) {
            setBorder( UIManager.getBorder("Table.focusCellHighlightBorder") );
              if (tblOt.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;
    /******tableSize()*****code for sizing table******start here()********/
      public void tableSize(){
        tblOt.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {},
            new String [] {"Ot No.", "Start Time","End Time", "Patient Name", "Surgery Name","Surgeon Name"}
        TableColumn column = null;
        for (int i = 0; i < 6; i++) {
          column = tblOt.getColumnModel().getColumn(i);
          if (i == 0) {
            column.setPreferredWidth(5);
          if (i == 1) {
            column.setPreferredWidth(15);
          if (i == 2) {
          column.setPreferredWidth(15);
          if (i == 3) {
            column.setPreferredWidth(130);
          if (i == 4) {
            column.setPreferredWidth(130);
          if (i == 5) {
            column.setPreferredWidth(130);
    /******tableSize()*****code for sizing table******end here()********/
        private javax.swing.JButton btnClose;
        private javax.swing.JScrollPane scrOt;
        private javax.swing.JTable tblOt;
        private DefaultTableModel model1;
    }

  • How to change the Background color of a cell in JTable

    hi!
    Actually i want to change the background color of few cells in JTable
    is there any method to make this change in JTable ?
    and also is it possible to have 5 rows with single column and 5 rows with 3 columns in a single JTable

    i want to change the background color of few cells in JTableDepending on your requirements for the coloring of cells it may be easier to override the prepareRenderer() method of JTable:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=610474

  • How can I get right data in a cell of JTable when table  enter editing

    how can I get right data in a cell of JTable when table enter editing

    how can I get right data in a cell of JTable when table enter editing

  • Setting Focus to a particular cell in JTable

    Hi, can i know how to set the focus to a particular cell in JTable.
    Say I have a table with 2 rows and 10 columns. The focus now is at position (1, 9) which is the last cell in the table. But I want to set the focus to (1, 3). How can i achieve this ? Pls help. Thanks

    OK. It's partially working. The right methods to use are setRowSelectionInterval and setColumnSelectionInterval. Jeanette was right. Mine didn't work because of a thread issue. I put the those two methods in a block such as:
    SwingUtilities.invokeLater(new Runnable(){
    public void run()
    table.setRowSelectionInterval(tblLineItem.getRowCount()-1,
    table.getRowCount()-1);
    table.setColumnSelectionInterval(0,0);
    Then it worked.
    But after I finished editing the first cell of the newly created row and press ENTER, the selection went back to the cell that's next to the originally editing cell on the first(old) row, instead of staying at the current row and going to the second cell.
    Can anybody shed a light on what I'm missing?

  • How to disable a particular cell in JTable ?

    I having a problem on how to set disable a particular cell in JTable. At the first place to load the JTable, I want to set some of the cell to disabled, it only will run at the first time, is there anyway to do it ?

    This is the function that disable/enable cells in JTables.
    Are you looking for this?
    public boolean isCellEditable(int row, int col)
      if(((new Integer((String)(getValueAt(row,0)).toString()).intValue()>9))&&(col==1))
        return true;
      if(row!=4&&col!=5)
        return true;
      return false;
         this metod belong to the TableModel function
    and what I gave you is only an example of what you can do with it.
    if it return false it disable and if true it enable the cell...but I think this you allready know!
    Regards Amnon

  • Unable to edit cells in JTable on single click of the cell.

    Hi,
    I am unable to edit a cell in JTable on single click of the cell. If I double click on the cell, I am able to edit it. Please help me.
    Thanks
    Subbu

    Thanks for all replies. Now, i am able to edit the cell on single click.

  • What component is placed default in each cell of JTable?

    Hi,
    What component is placed default in each cell of JTable?

    It depends on the content, see How to Use Tables - Concepts: Editors and Renderers for details.

  • Problem with select all cells in JTable

    Hi guys! I get some problem about selecting all cells in JTable. I tried to used two methods:
    1> table.selectAll()2> changeSelection(firstcell, lastcell,false,true)
    firstcell:[0,0], lastcell[rowcount-1,colcount-1]
    Result: only the first row selected when i use both methods.
    Note: i set up the selection model as following:
    this.dataSheet.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                    this.dataSheet.setCellSelectionEnabled(true);
                    this.dataSheet.setRowSelectionAllowed(true);
                    this.dataSheet.setColumnSelectionAllowed(true);Thanks !

    What selection properity should be changed in order to enable selectAll() method work properly? Is there Any constraints? Here is the TableModel I am using. And i set up selection mode use the following code:
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setCellSelectionEnabled(true);
    table.setRowSelectionAllowed(true);
    table.setColumnSelectionAllowed(true);
    import java.util.Vector;
    import javax.swing.table.*;
    import javax.swing.JTable;
    public class DataSheetModel extends AbstractTableModel{
              private Vector data = new Vector();//Store data
              private Vector columnNames = new Vector();//Store head
              public DataSheetModel(){}
              public DataSheetModel(Vector headVector, Vector dataVector){
                   if(headVector != null) this.columnNames = headVector;
                   if(dataVector != null) this.data = dataVector;
              public int getColumnCount(){
                   return columnNames.size()+1;
              public int getRowCount(){
                   return data.size()+1;
              public String getColumnName(int col){
                   if(col==0) return "";
                   else return (String)columnNames.get(col-1);
              public Object getValueAt(int row, int col){
                   if(col==0) {
                        if(row != data.size()) return String.valueOf(row);
                        else return "*";
                   else{
                        if(row != data.size()){
                             Vector rowVector = (Vector)data.elementAt(row);
                             return rowVector.elementAt(col-1);
                        }else return null;
              public void setValueAt(Object value, int row, int col){
                   if(row != this.data.size()){
                        Vector rowVector = (Vector)data.elementAt(row);
                        rowVector.set(col-1,value);
                        this.data.set(row,rowVector);
                        this.fireTableDataChanged();
                   }else{
                        Vector rowVector = new Vector();
                        for(int i=0; i<this.getColumnCount()-1; i++) rowVector.add(null);
                        rowVector.set(col-1,value);
                        this.data.add(rowVector);
                        this.fireTableDataChanged();
              public Class getColumnClass(int c){
                   return getValueAt(0,c).getClass();
              public boolean isCellEditable(int row, int col){
                   if(col == 0) return false;
                   else return true;
              public void setDataVector(Vector head, Vector data){
                   if(head != null) this.columnNames = head;
                   if(data != null) this.data = data;
    }

  • Merge cells in JTable.......

    Plz some help me out ......i want to merge two consecutive cells of a row in one cell in JTable......... how to do that.....give some code if possible....

    Well, as a new member you should learn to search the forum first before posting questions. Using keywords like "jtable merge cell" will find other postings on this topic. Some with solutions some without, so you will need to read a few and decide on the approach you want to take.

  • Text position in a cell in JTable?

    hello experts!!
    how can i center(horizontal) position/left/right justified a text in a cell in JTable?
    plz help.. tnx in advance!

    kagaw3000 wrote:
    ok i got it!
    But i am really confuse in JTable tutorial. it uses JLable as a CellRenderer and JButton as a CellEditor
    then it returns a Color.
    for clarification, when i am editing a cell, CellEditor will takes place, right? and when customizing the display of a cell, CellRendere will takes place, right?
    There is a very good explanation at the beginning of the Concepts: Editors and Renderers section in the tutorial.
    can you show me code that get and return a String not a Color.class?The default editor GenericEditor of JTable uses a JTextField which returns a String:
    static class GenericEditor extends DefaultCellEditor {
        Class[] argTypes = new Class[]{String.class};
        java.lang.reflect.Constructor constructor;
        Object value;
        public GenericEditor() {
            super(new JTextField());
            getComponent().setName("Table.editor");
        public boolean stopCellEditing() {
            String s = (String)super.getCellEditorValue();
            // Here we are dealing with the case where a user
            // has deleted the string value in a cell, possibly
            // after a failed validation. Return null, so that
            // they have the option to replace the value with
            // null or use escape to restore the original.
            // For Strings, return "" for backward compatibility.
            if ("".equals(s)) {
                if (constructor.getDeclaringClass() == String.class) {
                    value = s;
                super.stopCellEditing();
            try {
                value = constructor.newInstance(new Object[]{s});
            catch (Exception e) {
                ((JComponent)getComponent()).setBorder(new LineBorder(Color.red));
                return false;
            return super.stopCellEditing();
        public Component getTableCellEditorComponent(JTable table, Object value,
                boolean isSelected,
                int row, int column) {
            this.value = null;
            ((JComponent)getComponent()).setBorder(new LineBorder(Color.black));
            try {
                Class type = table.getColumnClass(column);
                // Since our obligation is to produce a value which is
                // assignable for the required type it is OK to use the
                // String constructor for columns which are declared
                // to contain Objects. A String is an Object.
                if (type == Object.class) {
                    type = String.class;
                constructor = type.getConstructor(argTypes);
            catch (Exception e) {
                return null;
            return super.getTableCellEditorComponent(table, value, isSelected, row, column);
        public Object getCellEditorValue() {
            return value;
    }

  • Merge table cells of JTable

    Can we merge two cells of JTable, the way we do it in html using rowspan and colspan?

    no, that's unsupported. There are some (partly oldish, google should help) experiments out there - but beware: they all require tweaks on the level of the ui-delegates which isn't a real option most of the time.
    Cheers
    Jeanette

Maybe you are looking for