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 / October 2004

Tip: Looking for answers? Try searching our database.

How to "turn of insert key" in a JTable ?

Thread view: 
Tux - 30 Sep 2004 22:17 GMT
When I editing cells in a JTable I press F2, delete the content of the cell,
then I update the cell.
What is the simpliest way to owrite the content of the cell when I type a
new content?
Dag Sunde - 01 Oct 2004 01:55 GMT
> When I editing cells in a JTable I press F2, delete the content of the cell,
> then I update the cell.
> What is the simpliest way to owrite the content of the cell when I type a
> new content?

One way is to override the table's editCellAt(...) method,
and use the overridden table class instead of JTable:

import javax.swing.JTable;
import javax.swing.table.TableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.ListSelectionModel;
import java.util.Vector;
import java.util.EventObject;
import java.awt.Component;
import javax.swing.*;
import javax.swing.text.JTextComponent;

public class OverWriteTable extends JTable {

 public OverWriteTable () {
 }

 public OverWriteTable (TableModel dm) {
   super(dm);

 }

 public OverWriteTable (TableModel dm, TableColumnModel cm) {
   super(dm, cm);
 }

 public OverWriteTable (TableModel dm, TableColumnModel cm,
ListSelectionModel sm) {
   super(dm, cm, sm);
 }

 public OverWriteTable (int numRows, int numColumns) {
   super(numRows, numColumns);
 }

 public OverWriteTable (Vector rowData, Vector columnNames) {
   super(rowData, columnNames);
 }

 public OverWriteTable (Object[][] rowData, Object[] columnNames) {
   super(rowData, columnNames);
 }

 public boolean editCellAt(int row, int column, EventObject e) {
   boolean res = super.editCellAt(row, column, e);
   Component c = this.getEditorComponent();
   if (c instanceof JTextComponent) {
     ( (JTextField) c).selectAll();
   }
   return res;
 }
}

Another way is to use a custom CellEditor where you need it:

import java.awt.Component;
import javax.swing.*;
import javax.swing.JTextField;
import java.awt.event.*;
import java.util.*;

public class TextCellEditor extends DefaultCellEditor implements
FocusListener {
 JTextField tf;

 public TextCellEditor(JTextField tf){
   super(tf);
   this.tf = tf;
   tf.addFocusListener(this);
   super.setClickCountToStart(1);
 }

 public boolean stopCellEditing() {
   String s = (String)getCellEditorValue();

   if( !isValid(s) ) {
     // Display errormessage here...
     return false;
   }
   return super.stopCellEditing();
 }

 public Component getTableCellEditorComponent(JTable table,
           Object value,
           boolean isSelected,
           int row, int column) {
   tf.setText(value.toString());
   return tf;
 }

 public void focusGained(FocusEvent e) {
   tf.selectAll();
 }

 public void focusLost(FocusEvent e) {
 }

 private boolean isValid(String s) {
   //Do validation here
   return true;
 }

}

Signature

Dag
58?26'15.9" N 008?46'45.5" E



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.