Results 1 to 7 of 7
  1. #1
    ld_pvl is offline Member
    Join Date
    Jun 2009
    Posts
    14
    Rep Power
    0

    Default Non-Editable Table Column

    Hello,

    I'm writing a small test application that allows the user to add different columns which cells are then populated automatically.

    The thing that I've been struggling to do is to make the added columns non-editable. Ideally, the user is only allowed to change the values in the first column as it is the variable column.

    I know that TableModel.isCellEditable() method can be used to make different columns non-editable when you define the table variable in the first place but it doesn't really solve my case because in my case the table is already there, I just want to make the newly added column non-editable.

    Here's the test code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TableColumnTest extends javax.swing.JFrame
    {
    
        public TableColumnTest()
        {
            initComponents();
        }
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            ComboBox = new javax.swing.JComboBox();
            AddButton = new javax.swing.JButton();
            ScrollPane = new javax.swing.JScrollPane();
            Table = new javax.swing.JTable();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Frame");
    
            ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Plus 10", "Minus 10", "Multiply by 2", "Divide by 2" }));
    
            AddButton.setText("Add");
            AddButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    AddButtonActionPerformed(evt);
                }
            });
    
            Table.setModel(new javax.swing.table.DefaultTableModel(
                new Object [][] {
                    {""},
                    {"50"},
                    {"20"},
                    {null}
                },
                new String [] {
                    "Variables"
                }
            ));
            Table.getTableHeader().setReorderingAllowed(false);
            Table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    TablePropertyChange(evt);
                }
            });
            ScrollPane.setViewportView(Table);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(AddButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        private void AddButtonActionPerformed(java.awt.event.ActionEvent evt)                                          
        {                                              
            DefaultTableModel model = (DefaultTableModel) Table.getModel();
    
            TableColumn column = new TableColumn();
            column.setHeaderValue(ComboBox.getSelectedItem().toString());
            column.setCellRenderer(new CustomCellRenderer(ComboBox.getSelectedItem().toString()));
            Table.addColumn(column);
        }                                         
    
        private void TablePropertyChange(java.beans.PropertyChangeEvent evt)                                     
        {                                         
            Table.repaint();
        }                                    
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TableColumnTest().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton AddButton;
        private javax.swing.JComboBox ComboBox;
        private javax.swing.JScrollPane ScrollPane;
        private javax.swing.JTable Table;
        // End of variables declaration
    
    }
    
    class CustomCellRenderer extends DefaultTableCellRenderer
    {
        String formula;
    
        public CustomCellRenderer(String Formula)
        {
            formula = Formula;
        }   
    
        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
        {
            Component cell = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    
            try {
                if (formula.equals("Plus 10")) super.setValue(Double.parseDouble(table.getValueAt(row, 0).toString()) + 10);
                if (formula.equals("Minus 10")) super.setValue(Double.parseDouble(table.getValueAt(row, 0).toString()) - 10);
                if (formula.equals("Multiply by 2")) super.setValue(Double.parseDouble(table.getValueAt(row, 0).toString()) * 2);
                if (formula.equals("Divide by 2")) super.setValue(Double.parseDouble(table.getValueAt(row, 0).toString()) / 2);
            } catch (Exception ex) {}
    
            return cell;
        }
    }
    Thanks,
    LD

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Trying to do this inside a renderer is fighting java. Better to update the model with your new data. Then java can take care of details like cell editing.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TCT extends javax.swing.JFrame {
        public TCT() {
            initComponents();
        }
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            ComboBox = new javax.swing.JComboBox();
            AddButton = new javax.swing.JButton();
            ScrollPane = new javax.swing.JScrollPane();
            Table = new javax.swing.JTable();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Frame");
    
            ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { 
    
    "Plus 10", "Minus 10", "Multiply by 2", "Divide by 2" }));
    
            AddButton.setText("Add");
            AddButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    AddButtonActionPerformed(evt);
                }
            });
    
            Table.setModel(new DefaultTableModel(
                new Object [][] {
                    {""},
                    {"50"},
                    {"20"},
                    {null}
                },
                new String [] {
                    "Variables"
                }) {
                public boolean isCellEditable(int row, int column) {
                    //System.out.printf("row: %d  col: %d%n", row, column);
                    return column == 0;
                }
            });
            Table.getTableHeader().setReorderingAllowed(false);
            Table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    TablePropertyChange(evt);
                }
            });
            ScrollPane.setViewportView(Table);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout
    
    (getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(ScrollPane, 
    
    javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(ComboBox, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(AddButton, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE, 90, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ComboBox, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(AddButton))
                    .addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    275, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
            TableColumn column = new TableColumn();
            String key = ComboBox.getSelectedItem().toString();
            column.setHeaderValue(key);
            // Trying to do this with a renderer is fighting java.
    //        column.setCellRenderer(new CustomCellRenderer(
    //            ComboBox.getSelectedItem().toString()));
            Table.addColumn(column);
            // The table listens to the model.
            // update model
            DefaultTableModel model = (DefaultTableModel) Table.getModel();
            int cols = model.getColumnCount();
            model.setColumnCount(cols+1);
            for(int i = 0; i < model.getRowCount(); i++) {
                model.setValueAt(getValue(key, i), i, cols);
            }
        }
    
        private String getValue(String formula, int row) {
            Object o = Table.getValueAt(row, 0);
            // (0,0) is an empty string and (3,0) is null, so:
            if(o == null || o.toString().equals("")) return "";
            // now it's safe to parse the stringValue of o
            double value = Double.parseDouble(o.toString());
            if (formula.equals("Plus 10"))
                value += 10;
            if (formula.equals("Minus 10"))
                value -= 10;
            if (formula.equals("Multiply by 2"))
                value *= 2;
            if (formula.equals("Divide by 2"))
                value /= 2;
            return String.valueOf(value);
        }
    
        private void TablePropertyChange(java.beans.PropertyChangeEvent evt) {
            Table.repaint();
        }                                    
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TCT().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton AddButton;
        private javax.swing.JComboBox ComboBox;
        private javax.swing.JScrollPane ScrollPane;
        private javax.swing.JTable Table;
        // End of variables declaration
    
    }

  3. #3
    ld_pvl is offline Member
    Join Date
    Jun 2009
    Posts
    14
    Rep Power
    0

    Default

    Thank you, that all makes sense. I'll change my ways according to this.

    But still, I'm still wondering about the issue on making a column non-editable. The code below, I believe, is what makes the first column editable only and the rest not. However, say, now I not only want the first column to be editable, but also some added columns as well.

    Let's take the above application for example, say now I would like to allow the user to change values in the "Plus 10" columns, and not allow the user to change vaues in the "Minus 10", "Divide by 2" and "Multiply by 2" columns.

    Then how can we do that? That is my genuine query from the beginning.

    I originally thought that there should be a method like setEditable(Boolean boo) for TableColumns then it would have been much more simple. But apparently it is not.

    Quote Originally Posted by hardwired View Post
    Java Code:
    ...
                public boolean isCellEditable(int row, int column) {
                    //System.out.printf("row: %d  col: %d%n", row, column);
                    return column == 0;
                }
    ...
    }
    Thanks,
    LD

    BTW: the code in the previous post, the newly added columns don't have correct headers.

  4. #4
    ld_pvl is offline Member
    Join Date
    Jun 2009
    Posts
    14
    Rep Power
    0

    Default

    To fix the header problem, I changed the code a bit in the AddButtonActionPerformed() part.

    I also added an updateTable() method to provide the automatically-update-values functionality.

    Here's the code.

    Please have a thought about my query in the previous post, because that's really a big question mark for me at the moment.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TCT extends javax.swing.JFrame {
        public TCT() {
            initComponents();
        }
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            ComboBox = new javax.swing.JComboBox();
            AddButton = new javax.swing.JButton();
            ScrollPane = new javax.swing.JScrollPane();
            Table = new javax.swing.JTable();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Frame");
    
            ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
    
    "Plus 10", "Minus 10", "Multiply by 2", "Divide by 2" }));
    
            AddButton.setText("Add");
            AddButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    AddButtonActionPerformed(evt);
                }
            });
    
            Table.setModel(new DefaultTableModel(
                new Object [][] {
                    {""},
                    {"50"},
                    {"20"},
                    {null}
                },
                new String [] {
                    "Variables"
                }) {
                public boolean isCellEditable(int row, int column) {
                    //System.out.printf("row: %d  col: %d%n", row, column);
                    return column == 0;
                }
            });
            Table.getTableHeader().setReorderingAllowed(false);
            Table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    TablePropertyChange(evt);
                }
            });
            ScrollPane.setViewportView(Table);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout
    
    (getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(ScrollPane,
    
    javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(ComboBox,
    
    javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
    
    javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(AddButton,
    
    javax.swing.GroupLayout.PREFERRED_SIZE, 90,
    
    javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ComboBox,
    
    javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
    
    javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(AddButton))
                    .addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE,
    
    275, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
            String key = ComboBox.getSelectedItem().toString();
            DefaultTableModel model = (DefaultTableModel) Table.getModel();
            int cols = model.getColumnCount();
            model.addColumn(key);
            for(int i = 0; i < model.getRowCount(); i++) {
                model.setValueAt(getValue(key, i), i, cols);
            }
        }
    
        private void updateTable()
        {
            DefaultTableModel model = (DefaultTableModel) Table.getModel();
    
            for (int i = 1; i < Table.getColumnCount(); i++) {
                for (int k = 0; k < Table.getRowCount(); k++) {
                    model.setValueAt(getValue(Table.getColumnName(i), k), k, i);
                }
            }
        }
    
        private String getValue(String formula, int row) {
            Object o = Table.getValueAt(row, 0);
            // (0,0) is an empty string and (3,0) is null, so:
            if(o == null || o.toString().equals("")) return "";
            // now it's safe to parse the stringValue of o
            double value = Double.parseDouble(o.toString());
            if (formula.equals("Plus 10"))
                value += 10;
            if (formula.equals("Minus 10"))
                value -= 10;
            if (formula.equals("Multiply by 2"))
                value *= 2;
            if (formula.equals("Divide by 2"))
                value /= 2;
            return String.valueOf(value);
        }
    
        private void TablePropertyChange(java.beans.PropertyChangeEvent evt) {
            updateTable();
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TCT().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton AddButton;
        private javax.swing.JComboBox ComboBox;
        private javax.swing.JScrollPane ScrollPane;
        private javax.swing.JTable Table;
        // End of variables declaration
    
    }
    Thanks,
    LD
    Last edited by ld_pvl; 08-02-2009 at 06:10 AM.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    the code in the previous post, the newly added columns don't have correct headers
    Updating the column identifiers in the model is a little more work than adding the new row data.
    You can collect, modify and reset them when you add a new column.
    I added this in to the TCT example.

    For allowing edits in other columns you still want to report this in/from the model isCellEditable method which JTable uses for this. Learning how to work with the given design is one part of this. Coming up with clever ways to work within this structure to get what you want is the other part. Here's a suggestion about allowing editing for any "Plus 10" column.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TCT extends javax.swing.JFrame {
        public TCT() {
            initComponents();
        }
    
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            ComboBox = new javax.swing.JComboBox();
            AddButton = new javax.swing.JButton();
            ScrollPane = new javax.swing.JScrollPane();
            Table = new javax.swing.JTable();
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Frame");
    
            ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { 
    
    "Plus 10", "Minus 10", "Multiply by 2", "Divide by 2" }));
    
            AddButton.setText("Add");
            AddButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    AddButtonActionPerformed(evt);
                }
            });
    
            Table.setModel(new DefaultTableModel(
                new Object [][] {
                    {""},
                    {"50"},
                    {"20"},
                    {null}
                },
                new String [] {
                    "Variables"
                }) {
    
                // allow the user to change values in the "Plus 10" columns
                public boolean isCellEditable(int row, int column) {
                    // This println statement always reported column = 0
                    // when using your renderer instead of the model.
                    //System.out.printf("row: %d  col: %d%n", row, column);
                    return (column == 0) ||
                        (getColumnName(column).equals("Plus 10"));
                    // You may also want/need to restrict editing
                    // to the rows at index 1 and 2.
                }
            });
            Table.getTableHeader().setReorderingAllowed(false);
            Table.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
                public void propertyChange(java.beans.PropertyChangeEvent evt) {
                    TablePropertyChange(evt);
                }
            });
            ScrollPane.setViewportView(Table);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout
    
    (getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(ScrollPane, 
    
    javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(ComboBox, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(AddButton, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE, 90, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup
    
    (javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(ComboBox, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(AddButton))
                    .addPreferredGap
    
    (javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(ScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 
    
    275, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            pack();
        }// </editor-fold>
    
        private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {
            TableColumn column = new TableColumn();
            String key = ComboBox.getSelectedItem().toString();
            column.setHeaderValue(key);
            Table.addColumn(column);
            // The table listens to the model.
            // update model
            DefaultTableModel model = (DefaultTableModel) Table.getModel();
            int cols = model.getColumnCount();
            // collect column identifiers
            String[] colIds = new String[cols+1];
            for(int i = 0; i < cols; i++) {
                colIds[i] = model.getColumnName(i);
            }
            colIds[cols] = key;
            // add new column capacity in model
            model.setColumnCount(cols+1);
            // reset column identifiers
            model.setColumnIdentifiers(colIds);
            // set row data for new data column
            for(int i = 0; i < model.getRowCount(); i++) {
                model.setValueAt(getValue(key, i), i, cols);
            }
        }
    
        private String getValue(String formula, int row) {
            Object o = Table.getValueAt(row, 0);
            // (0,0) is an empty string and (3,0) is null, so:
            if(o == null || o.toString().equals("")) return "";
            // now it's safe to parse the stringValue of o
            double value = Double.parseDouble(o.toString());
            if (formula.equals("Plus 10"))
                value += 10;
            if (formula.equals("Minus 10"))
                value -= 10;
            if (formula.equals("Multiply by 2"))
                value *= 2;
            if (formula.equals("Divide by 2"))
                value /= 2;
            return String.valueOf(value);
        }
    
        private void TablePropertyChange(java.beans.PropertyChangeEvent evt) {
            Table.repaint();
        }                                    
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TCT().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JButton AddButton;
        private javax.swing.JComboBox ComboBox;
        private javax.swing.JScrollPane ScrollPane;
        private javax.swing.JTable Table;
        // End of variables declaration
    
    }

  6. #6
    ld_pvl is offline Member
    Join Date
    Jun 2009
    Posts
    14
    Rep Power
    0

    Default

    Thanks,

    I just would like to cite that there is a similar, but not the same, topic in the Sun's forum that has a few things related to this topic. This is the link: http*remove this*://forums.sun.com/*remove this*thread.jspa?messageID=10781761

    [I still can't post any links yet =P)

    What I'm really trying to figure out is how to make the solution for this problem really general - adding a new column and making it non-editable or editable, without having to know its header or anything.

    Please visit the link above for some more detail on the topic if you'd like.
    Last edited by ld_pvl; 08-03-2009 at 12:24 AM.

  7. #7
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    The contibutors to your Sun thread are also suggesting that you work within the JTable design, ie, use the isCellEditable method within the TableModel. If you stay with this I'm sure you'll find a way to meet your requirements.

Similar Threads

  1. Replies: 4
    Last Post: 02-05-2011, 06:47 PM
  2. Auto updation of Editable Column Indexes
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 0
    Last Post: 10-23-2008, 10:23 AM
  3. Replies: 0
    Last Post: 07-11-2008, 04:30 PM
  4. Sorting a SWT table by column
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-11-2008, 03:07 PM
  5. Replies: 4
    Last Post: 05-19-2007, 11:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •