Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JTable Listeners

Status
Not open for further replies.

brinker

Programmer
May 31, 2001
48
CA
Hi All,

I have a pretty simple question regarding JTables. Basically, I am trying to update one column dynamically based upon the user typing in a value into another column.

My Key pressed action listener appears to be functioning properly as it processes the event. However, when I try to get the string from the column the user is typing in, I get a null pointer exception (ie no string).

Has anybody had any experiece similiar to mine? If so, could you please tell me how to resolve the problem.

thanks
 
I don't know what you really want so I post a little example :
-------------------------------------------
The table and main class :
Code:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

public class TableExample {

  public static void main(String[] args) {
    TableExample example = new TableExample();
    example.doIt();
  }

  public void doIt() {
    JFrame f = new JFrame("Table Example");
    JTable tbl = new JTable(new TableModelExample());
    JScrollPane sp = new JScrollPane(tbl);
    f.getContentPane().add(sp, "Center");
    f.pack();
    f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                    System.exit(0);
            }
    });
    f.setVisible(true);
  }

}
-------------------------------------------------
The TableModel class :
Code:
import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class TableModelExample extends AbstractTableModel {

  protected Object[][] data = new Object[][] {
      { "One", "AAA"},
      { "Two", "BBB"},
      { "Three", "CCC"},
      { "Four", "DDD"}};

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

  public int getColumnCount() {
    return data[0].length;
  }

  public boolean isCellEditable(int row, int col) {
    if (col == 0)
      return true;
    else
      return false;
  }

  public void setValueAt(Object value, int row, int col) {
    data[row][col] = (String)value;
    //fireTableRowsUpdated(row, row);
  }

  public Object getValueAt(int row, int col) {
    switch(col) {
      case 1:
        return "UpperCase : " + ((String)data[row][0]).toUpperCase();
      default:
        return data[row][col];
    }
  }

}
 
Thanks for the reply,

Actually the problem that I am having is that when the user types into a Table cell, the table cell is not updated until "Enter" is hit or the user clicks on another cell. I want to update another cell dynamically while the user is still typing information into the first cell. Is this possible?

thanks
 
The TableModel is only updated after the user presses "Enter" or clicks in another cell, but if the user presses "Escape" the changes must be ignored. It is possible to capture KeyEvents, but it is a bad idea to put this in the model for the other column. But I'll give you some code anyway. Backspace, escape,... etc are not handled... I do NOT think this is the correct approach. I think a better approach is to make a CellEditor and let him handle the two cells.

The table and main
Code:
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;

public class TableExample {

  JTable tbl;

  public static void main(String[] args) {
    TableExample example = new TableExample();
    example.doIt();
  }

  public void doIt() {
    JFrame f = new JFrame("Table Example");
    tbl = new JTable(new TableModelExample());
    JTextField textField = new JTextField();
    //TableCellEditor editor = new MyCellEditor(textField);
    tbl.setDefaultEditor(JTextField.class, new DefaultCellEditor(textField));
    // Sometimes the table gets the KeyEvents
    tbl.addKeyListener(new KeyListener() {
      public void keyPressed(KeyEvent evt){
        //System.out.println("keyPressed : " + evt.getKeyChar());
      }
      public void keyTyped(KeyEvent evt){
        System.out.println("keyTyped : " + evt.getKeyChar());
        tbl.setValueAt( (String)tbl.getValueAt(tbl.getSelectedRow(),1) + evt.getKeyChar(),tbl.getSelectedRow(),1);
      }
      public void keyReleased(KeyEvent evt){
        //System.out.println("keyReleased = " + evt.getKeyChar());
      }
    });

    // Sometimes the editor gets the KeyEvent
    textField.addKeyListener(new KeyListener() {
      public void keyPressed(KeyEvent evt){
        //System.out.println("TkeyPressed : " + evt.getKeyChar());
      }
      public void keyTyped(KeyEvent evt){
        System.out.println("TkeyTyped : " + evt.getKeyChar());
        tbl.setValueAt( (String)tbl.getValueAt(tbl.getSelectedRow(),1) + evt.getKeyChar(),tbl.getSelectedRow(),1);
      }
      public void keyReleased(KeyEvent evt){
        //System.out.println("TkeyReleased = " + evt.getKeyChar());
      }
    });

    JScrollPane sp = new JScrollPane(tbl);
    f.getContentPane().add(sp, "Center");
    f.pack();
    f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent evt) {
                    System.exit(0);
            }
    });
    f.setVisible(true);
  }

}
===================================
The tableModel
Code:
import javax.swing.*;
import javax.swing.table.AbstractTableModel;

public class TableModelExample extends AbstractTableModel {

  protected Object[][] data = new Object[][] {
      { "One", "AAA"},
      { "Two", "BBB"},
      { "Three", "CCC"},
      { "Four", "DDD"}};

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

  public int getColumnCount() {
    return data[0].length;
  }

  public Class getColumnClass(int com) {
    return JTextField.class;
  }

  public boolean isCellEditable(int row, int col) {
    if (col == 0)
      return true;
    else
      return false;
  }

  public void setValueAt(Object value, int row, int col) {
    data[row][col] = (String)value;
    if (col == 1)
      this.fireTableCellUpdated(row, col);
  }

  public Object getValueAt(int row, int col) {
    switch(col) {
     /* case 1:
        System.out.print("get1");
        return "UpperCase : " + ((String)data[row][0]).toUpperCase();*/
      default:
        return data[row][col];
    }
  }

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top