Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2007

Tip: Looking for answers? Try searching our database.

Problem in Coloring JTable cell to indicate that is editable

Thread view: 
Chanchal - 12 Dec 2007 13:05 GMT
Hi All,

I have a JTable in which one cell has a JComboBox as cell editor. If a
particular value is chosen in the JComboBox, some other cells in the
JTable should become editable.And i need to show the editable cells in
a different color. My problem is that, though cells are becoming
editable according to the condition, color of those cells are changing
only if click them. I want color to change when selection is made in
the JComboBox. Please find sample program, and give some suggentions.

Thanks in advance

Chanchal

<code>
import java.awt.*;
import java.util.List;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.*;

public class JTableTest extends JFrame {

   public JTableTest() {
       jScrollPane1 = new JScrollPane();
       jTable1 = new JTable();
       jTable1.setRowSelectionAllowed(false);

       TestModel testModel = new TestModel();
       testModel.setDataVector(new Object [][] {{"1", "2"},{"3",
"4"},},
               new String [] {"Title 1", "Title 2"});
       jTable1.setModel(testModel);
       JComboBox jcb = new JComboBox(new DefaultComboBoxModel(new
Object[]{"1","2"}));
       jTable1.getColumn(jTable1.getColumnName(0)).setCellEditor(new
DefaultCellEditor(jcb));

jTable1.getColumn(jTable1.getColumnName(0)).setCellRenderer(new
TestRenderer());

jTable1.getColumn(jTable1.getColumnName(1)).setCellRenderer(new
TestRenderer());

       jScrollPane1.setViewportView(jTable1);

       getContentPane().add(jScrollPane1, BorderLayout.CENTER);

       pack();

   }

   public static void main(String args[]) {
       java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
               new JTableTest().setVisible(true);
           }
       });
   }

   private JScrollPane jScrollPane1;
   private JTable jTable1;

   //inner class for TableModel
   class TestModel extends DefaultTableModel{

       List<Integer> nonEditableColumns;

       public TestModel(){
           nonEditableColumns = new ArrayList<Integer>();
           nonEditableColumns.add(1);
       }

       public void setDataVector(Object[][] data, Object[] headers) {
           super.setDataVector(data, headers);
       }

       public boolean isCellEditable(int row, int column) {
           if(nonEditableColumns.contains(column)){
               return false;
           }else {
               return true;
           }
       }

       public void setValueAt(Object value, int row, int column) {
           if(column == 0){
               int val = Integer.parseInt(String.valueOf(value));
               if(val == 2){
                   nonEditableColumns.remove(new Integer(1));
               }else{
                   nonEditableColumns.add(new Integer(1));
               }
           }
           fireTableCellUpdated(row, column);
       }
   }

   //inner class for renderer
   class TestRenderer extends DefaultTableCellRenderer{
       public Component getTableCellRendererComponent(JTable jTable,
Object value, boolean selected, boolean hasFocus, int row, int column)
{
           TableModel tModel = jTable.getModel();
           setOpaque(true);
           if(tModel.isCellEditable(row, column)){
               setBackground(Color.MAGENTA);
           }else{
               setBackground(Color.CYAN);
           }
           setForeground( Color.BLACK );

setText(String.valueOf(jTable.getModel().getValueAt(row,column)));
           return this;
       }
   }
}
</code>
Tom N - 14 Dec 2007 04:28 GMT
> I have a JTable in which one cell has a JComboBox as cell editor. If a
> particular value is chosen in the JComboBox, some other cells in the
[quoted text clipped - 3 lines]
> only if click them. I want color to change when selection is made in
> the JComboBox. Please find sample program, and give some suggentions.

You need to fireTableCellUpdated on the cells whose colour has changed
(i.e. when they stop being editable or become editable).  Otherwise they
might not be rerendered.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.