SpecialKeyEvent im JTable

THEJS

Mitglied
Hallo Leute!

Folgendes:
In meinem JTable würd ich gerne abfangen, dass wenn der Benutzer - ohne zuerst in den CellEditor zu wechseln - etwas eingibt, das eingegebene den aktuellen Text überschreibt.
Standardfall ist, dass das eingegebene einfach hinten drangehängt wird.

Hab schon ein bisschen mit dem CellEditor herumgespielt und habe bemerkt, dass der KeyListener und auch der FocusListener in diesem Fall garnicht angestoßen werden. :confused:

Hat jemand eine Lösung oder zumindest einen Denkanstoß für mich?

Danke
THEJS
 
Hallo!
Wenn du dich nur daran störst, dass standardmässig der Text hinten angehängt wird, wie wärs wenn du stattdessen den CellEditor einfach mit einem leeren TextFeld initialisierst?
//Editor
Code:
public class MyTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    // This is the component that will handle the editing of the cell value
    JTextField component = new JTextField();
    // This method is called when a cell value is edited by the user.
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int rowIndex, int vColIndex) {
        
        component.setText("");
        return component;
    } 
    public Object getCellEditorValue() {
        return component.getText(); 
    }
}
//Hauptanwendung
Code:
public class TestFrame extends javax.swing.JFrame {

    /** Creates new form TestFrame */
    public TestFrame() {
        initComponents();
        jTable1.setDefaultEditor(Object.class, new MyTableCellEditor());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

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

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "2", "3", "4"},
                {"1", "2", "3", "4"},
                {"1", "2", "3", "4"},
                {null, "2", "3", "4"}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        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()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration

}


*Viele grüße*
 
Hey Artorius!

Das ist zwar nicht das was ich wollte, aber es hat als Denkanstoß gereicht ;)
Danke dafür!!

Für alle die, dies interessiert:

--> Mein Editor: StandardEditor.java
Code:
JTextField txtField = new JTextField();
String lastValue = "";

public StandardEditor() {
	
	txtField.addFocusListener(new FocusListener()
	{
		public void focusLost(FocusEvent e) {}
		public void focusGained(FocusEvent e)
		{
			txtField.setText(lastValue);
		}
	});
	
}

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

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


Und in meiner Komponente, wo der Tabel drin ist setz ich dann als Default-Editor diesen StandardEditor
Code:
table.setDefaultEditor(String.class, new StandardEditor());
table.setDefaultEditor(Double.class, new StandardEditor());
table.setDefaultEditor(Integer.class, new StandardEditor());

Danke nochmal...warst mir eine wirklich große Hilfe ;)

greez
THEJS
 
Zurück