0

I've implemented a method so user inputs are set to decimal format. But it there's two forms of editing JTable cells (?).

In one of them (first pic) when the user simply selects a cell and start writing my Cell Editor doesn't work.

But in the other, when it double clicks the cell the editor works.

There's a way to allow editing only when the user double clicks?

PS.: isCellEditable and setClickCountToStart didn't work.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class example extends JFrame {

    private JPanel contentPane;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    example frame = new example();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public example() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);

        JTable table = new JTable(new DefaultTableModel(new Object[]{"RB"}, 10));

        table.setPreferredScrollableViewportSize(new Dimension(700, 350));
        TableColumn RBColumn = table.getColumnModel().getColumn(0);
        RBColumn.setCellEditor(new DefaultCellEditor(tfMon())); 

        JScrollPane scrollPane = new JScrollPane(table);

        contentPane.add(scrollPane, BorderLayout.CENTER);
    }


    private JTextField tfMon(){
        JTextField tf = new JTextField();
        tf.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                char c = e.getKeyChar();
                if (!((c >= '0') && (c <= '9') || (c ==         KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))){
                    e.consume();
                }else{
                    String txt = tf.getText();
                    txt = txt.replace(".","").replace("0","");
                    if(!((c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))){
                        e.consume();
                        txt=txt+c;
                    }
                    while(txt.length()<3){
                        txt="0"+txt;
                    }
                    txt = txt.substring(0,txt.length()-2)+"."+txt.substring(txt.length()-2,txt.length());
                    tf.setText(txt);
                }
            }
        });
           return tf;
    }
}

Case 1 happens when user types in this condition enter image description here

2 Answers2

1

There's a way to allow editing only when the user double clicks?

The problem is with your editor. The solution should be to fix the editor, not prevent the user from typing directly into the text field.

I'm not sure what your editor is doing but it looks like you are trying to enter only numbers in the editor.

If this is the case then you should:

  1. Override the getColumnClass(...) method of the TableModel to return Integer or Double. Then the table will use an appropriate editor that will only allow numeric data to be entered. So you won't need to write a custom editor.

  2. If you want custom editing, then instead of using a JTextField you can use a JFormattedTextField in which you specify a mask to only allow numeric values

  3. Finally the most complicated would be to use a DocumentListener on the Document of the text field (instead of a KeyListener). The DocumentEvent will be generated whenever the Document is changed.

Edit:

You might be able to get your code to work by using:

table.setSurrendersFocusOnKeystroke( true );

I believe this should pass the KeyEvents to the editor even when the cell is not in editing mode.

Actually, I just remembered I may have a better solution. Use a custom JTextField to implement the logic, instead of a KeyListener. Check out the code in: Make a JFormattedTextField behave like ATM input

I haven't tried using this text field as an editor, but I see no reason why it won't work as the logic is not based on KeyEvents.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288
  • My editor changes the text of the cell as the user types anything. cells should start at 0.00, and when user type 1 it goes to 0.01, and so on, so if he types 1 and 2 it becomes 0.12, for 1,2 and 3 it becomes 1.23. It works fine when the user double click the cell. – Felipe Hogrefe Bento Apr 06 '17 at 15:51
  • @FelipeHogrefeBento, And I gave you 3 possible solutions. – camickr Apr 06 '17 at 16:12
  • @FelipeHogrefeBento, It is irrelevant "how" the cell is editing. What is important is the text being added to the cell. That is why you don't want to use a KeyListener. If the cell is not in editing mode, the "character" typed while the cell has focus is passed to the editor, not the KeyEvent. – camickr Apr 06 '17 at 16:28
  • Got it! Its not like the key are being pressed, the character is passed by other ways, right? – Felipe Hogrefe Bento Apr 06 '17 at 16:36
0

I've found a way to force the edit mode, which will do the trick:

https://stackoverflow.com/a/3015438/6047129

Community
  • 1
  • 1