🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #10071

JTable cellEditor caret pbs

#10071
From: denisb75
Date: Mon, 15 Nov 1999 00:00
230 lines
6375 bytes
I've got one table with 1 cell editor for the 2 lasts col of the jtable.
When I go on one of
the 2 last col with "tab" key, the editor do not behave as expected.

Here is the behaviour of MyJTextFieldEditor :
	. allow all the chars
	. when you click once on it, all the chars are selected and the
caret blinks, and
you can use the arrow keys to move into the text.
	. when you click twice on it, the caret blink at the position
you clicked
	. after having clicked once, if you press any key, the text is
erased and the new
chars are put on the editor

Now, if you put this component into a JTable with DefaultCellEditor, the
caret disappear,
this component react as a pure JTextField without the caret ...


Here is a snippet of code for this problem :

// ----------------------- APPLET1
------------------------------------------
public class Applet1 extends JApplet {
  MyTextFieldEditor toto = new MyTextFieldEditor();
  AbstractTableModel tm = new tm();
  JTable myJTable1 = new MyJTable();
  JButton jButton1 = new JButton();

...

  private void jbInit() throws Exception {
    this.setSize(new Dimension(639, 509));
    this.getContentPane().setLayout(null);
    myJTable1.setBounds(new Rectangle(7, 83, 620, 87));

    jButton1.setText("jButton1");
    jButton1.setBounds(new Rectangle(262, 191, 81, 27));
    jButton1.addActionListener(new java.awt.event.ActionListener() {

      public void actionPerformed(ActionEvent e) {
        jButton1_actionPerformed(e);
      }
    });

    this.getContentPane().add(myJTable1, null);
  }

...

  void jButton1_actionPerformed(ActionEvent e) {
    myJTable1.setDefaultEditor(String.class, MyJTextFieldEditor);
  }
}
//
------------------------------------------------------------------------
---

// ----------------------- MyTextField
---------------------------------------
...
public class MyJTextField extends JTextField implements FocusListener {

public myJTextField() {
	super();
	initialize();
}

private void connEtoM1(FocusEvent arg1) {
	try {
		this.selectAll();
	} catch (java.lang.Throwable ivjExc) {
		handleException(ivjExc);
	}
}

private void connEtoM2(FocusEvent arg1) {
	try {
	    this.select(0,0);
	} catch (java.lang.Throwable ivjExc) {
		handleException(ivjExc);
	}
}

private void connEtoM3() {
	try {
		this.setMargin(new java.awt.Insets(0, 0, 0, 0));
	} catch (java.lang.Throwable ivjExc) {
		handleException(ivjExc);
	}
}

public void focusGained(FocusEvent e) {
	if ((e.getSource() == this) ) {
		connEtoM1(e);
	}
}

public void focusLost(FocusEvent e) {
	if ((e.getSource() == this) ) {
		connEtoM2(e);
	}
}


private void handleException(Throwable exception) {
...
}

public static void main(java.lang.String[] args) {
	try {
		...
		MyJTextField aMyJTextField;
		aMyJTextField = new MyJTextField();
		frame.add("Center", aMyJTextField);
		frame.setSize(aMyJTextField.getSize());
		frame.setVisible(true);
	} catch (Throwable exception) {
		...
	}
}

public String validation() {
	return null;
}
}
// --------------------------------------------------------------

//-------------------------- MyJTextFieldEditor -----------------
public class MyJTextFieldEditor extends MyJTextField implements
TableCellEditor {

  protected transient Vector listeners;
  protected transient Object originalValue;
  public Object valeurComposant;

  public MyJTextFieldEditor() {
    super();
    listeners = new Vector();
  }


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

    final MyJTextField editor = this;

    super.setText(value.toString());

    valeurComposant = value;
    originalValue = value;

    table.setRowSelectionInterval(row, row);
    table.setColumnSelectionInterval(column, column);

    return this;
  }

  public void cancelCellEditing() {fireEditingCanceled();}

  public Object getCellEditorValue() {
    return getText();
  }

  public boolean isCellEditable(EventObject eo) {
    return true;
  }

  public boolean shouldSelectCell(EventObject eo) { return true ;}

  public boolean stopCellEditing() {
    fireEditingStopped();
    return true;
  }

  public void addCellEditorListener(CellEditorListener cel) {
    final MyJTextField editor = this;

    // Trick found into the java bugs repository, but not really
powerfull for this program
    try {

      SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            requestFocus();
          }
      });
    } catch (Exception e) {};

    this.setText(super.getText());
    listeners.addElement(cel);
  }

  public void removeCellEditorListener(CellEditorListener cel) {
    listeners.removeElement(cel);
  }


  protected void fireEditingCanceled() {
    super.setText((String)originalValue);
    ChangeEvent ce = new ChangeEvent(this);
	for (int i = listeners.size(); i >= 0; i--)
{((CellEditorListener)listeners.elementAt(i)).editingCanceled(ce);
    }
  }

  protected void fireEditingStopped() {
    if (super.getText().equals(""))
      return ;
    ChangeEvent ce = new ChangeEvent(this);
	    for (int i = listeners.size() - 1; i >= 0; i--)     {

((CellEditorListener)listeners.elementAt(i)).editingStopped(ce);
    }
  }
}
// -------------------------------------------------------------------






Sent via Deja.com http://www.deja.com/
Before you buy.
Technical Details
Message-ID: <80p23v$vpm$1@nnrp1.deja.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,8247dcbb63fd9190,start
X-Google-Attributes: gid111d45,public
From: denisb75@hotmail.com
Subject: JTable cellEditor caret pbs
Date: 1999/11/15
Message-ID: <80p23v$vpm$1@nnrp1.deja.com>
X-Deja-AN: 548805334
X-Http-Proxy: 1.0 janus:80 (Squid/2.1.PATCH2), 1.0 x43.deja.com:80 (Squid/1.1.22) for client 193.190.23.216, 193.190.182.11
Organization: Deja.com - Before you buy.
X-Article-Creation-Date: Mon Nov 15 13:33:51 1999 GMT
X-MyDeja-Info: XMYDJUIDdenisb7575
Newsgroups: borland.public.jbuilder.java.swing
X-Http-User-Agent: Mozilla/4.7 [en] (WinNT; I)