Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: DefaultCellEditor does not support empty cells

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default DefaultCellEditor does not support empty cells

    I have a table (MyTable) with a tablemodel and a DefaultCellEditor (see below code at the bottom). I do not want any cells in my table to be negative but empty cells are acceptable. When a user deletes the content of a cell, an error is triggered here:

    if(txtfld.getText().isEmpty()==true)
    return super.stopCellEditing(); //this triggers an error

    What do I need to change in the code to be able to have nulls in the table?

    Thanks in advance

    --------------------------------------------------------

    import java.awt.Color;
    import java.awt.Component;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.table.*;



    public class MyFrame extends javax.swing.JFrame {


    public MyFrame() {
    initComponents();

    }

    private void initComponents() {

    jScrollPane1 = new javax.swing.JScrollPane();
    MyTable = new javax.swing.JTable();

    setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
    getContentPane().setLayout(new java.awt.FlowLayout());

    MyTable.setModel(new MyTableModel());
    MyTable.setDefaultEditor(Double.class,new TableCellNumericEditor(new javax.swing.JTextField()));
    jScrollPane1.setViewportView(MyTable);

    getContentPane().add(jScrollPane1);

    pack();
    }


    public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new MyFrame().setVisible(true);

    }
    });
    }

    private javax.swing.JTable MyTable;
    private javax.swing.JScrollPane jScrollPane1;

    }







    class MyTableModel extends AbstractTableModel{
    private String[] columnNames = {"A","B","C","D","E"};

    private Object[][] data = {
    {10,25.8,29.6,33.0,36.1},
    {17.5,20.9,24.0,26.7,45},
    };



    public boolean isCellEditable(int rowIndex,
    int columnIndex){
    return true;
    }




    public Class<?> getColumnClass(int columnIndex) {

    //return data[0][columnIndex].getClass();
    return Double.class;
    }



    public int getRowCount(){
    return data.length;
    }


    public int getColumnCount(){

    return data[0].length;
    }


    public Object getValueAt(int row,int column){

    return data[row][column];
    }


    public void setValueAt(Object aValue, int row, int column) {

    data[row][column]=aValue;
    }



    public String getColumnName(int c){

    return columnNames[c];

    }

    }


    class TableCellNumericEditor extends DefaultCellEditor implements TableCellEditor {
    private static final Border redcol = new LineBorder(Color.red);
    private static final Border blackcol = new LineBorder(Color.black);
    private JTextField txtfld;


    public TableCellNumericEditor(JTextField txtfld) {
    super(txtfld);
    this.txtfld = txtfld;
    this.txtfld.setHorizontalAlignment(JTextField.CENT ER);

    }




    public boolean stopCellEditing() {


    if(txtfld.getText().isEmpty()==true)
    return super.stopCellEditing(); //this triggers an error


    try {
    double vald = Double.valueOf(txtfld.getText());
    if (vald < 0) {
    throw new NumberFormatException();
    }
    } catch (NumberFormatException e) {
    txtfld.setBorder(redcol);
    return false;
    }


    return super.stopCellEditing();
    }


    public Component getTableCellEditorComponent(JTable table,
    Object value, boolean isSelected, int row, int column) {

    txtfld.setBorder(blackcol);
    return super.getTableCellEditorComponent(
    table, value, isSelected, row, column);
    }
    }


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: DefaultCellEditor does not support empty cells

    Please use code tags when posting code. Help can be found on the Announcements page.

    Quote Originally Posted by ALCOLEA1972 View Post
    What do I need to change in the code to be able to have nulls in the table?
    That is for you to decide.
    Figure out when the null value will be introduced, how to handle it, what to display in it's place on the table as a marker. (empty space is something to show)

    Quote Originally Posted by ALCOLEA1972 View Post
    I do not want any cells in my table to be negative but empty cells are acceptable.
    Sometimes in systems with all positive numbers, negative values are used to represent "null" or "invalid" type values. Not that this fits into all designs, but something to consider.

Similar Threads

  1. Merge Cells in JTable
    By Stx in forum AWT / Java Swing
    Replies: 0
    Last Post: July 16th, 2012, 04:21 AM
  2. Randomizing cells in a grid
    By Flowbs in forum AWT / Java Swing
    Replies: 3
    Last Post: December 18th, 2010, 09:53 PM
  3. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 05:04 PM
  4. Replies: 1
    Last Post: September 30th, 2010, 02:36 PM