Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8820141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:32:03+00:00 2026-06-14T05:32:03+00:00

I have a JTable that has two columns ( editable JTable ). When a

  • 0

I have a JTable that has two columns (editable JTable). When a user types something in the second column, my requirement is as follows:

  • user can only typewrite a number and comma

  • when user type wrong character, it will beep (Toolkit.getDefaultToolkit().beep();)

How do I go about achieving this? (if it jtextfield it need document filter or plain document etc, if it JTable, then how?)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:32:04+00:00Added an answer on June 14, 2026 at 5:32 am

    Implement a TableCellEditor that is capable of returning a text component (JTextField) that has a DocumentFilter attached to it capable of filtering the incoming text as you require.

    You might like to take a look at

    • Limit the Characters in the text field using document listner
    • MDP’s Weblog
    • accept only a single digit in java
    • Text Component Features (look for “Implementing a Document Filter”)

    UPDATED with example

    if it jtextfield it need document filter or plain document etc, if it
    JTable, then how?

    Read the tutorials, ask questions specific to the problem you’re having. It’s important that you understand why things are done in a certain way rather then simply copying and pasting some one else code, otherwise you won’t know when you’re copying bad ideas 😉

    public class TestFilteredCellEditor {
    
        public static void main(String[] args) {
            new TestFilteredCellEditor();
        }
    
        public TestFilteredCellEditor() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException ex) {
                    } catch (InstantiationException ex) {
                    } catch (IllegalAccessException ex) {
                    } catch (UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new FilteredEditorPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class FilteredEditorPane extends JPanel {
    
            public FilteredEditorPane() {
                setLayout(new BorderLayout());
                JTable table = new JTable(new MyTableModel());
                TableColumnModel columnModel = table.getColumnModel();
                columnModel.getColumn(1).setCellEditor(new FilteredTableEditor());
                add(new JScrollPane(table));
            }
        }
    
        public class FilteredTableEditor extends AbstractCellEditor implements TableCellEditor {
    
            private JTextField editor;
    
            public FilteredTableEditor() {
                editor = new JTextField();
                ((AbstractDocument) editor.getDocument()).setDocumentFilter(new NumericDocumentFilter());
            }
    
            @Override
            public boolean isCellEditable(EventObject e) {
                return true;
            }
    
            @Override
            public Object getCellEditorValue() {
                return editor.getText();
            }
    
            @Override
            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                if (value instanceof String) {
                    editor.setText((String) value);
                } else {
                    editor.setText(null);
                }
                return editor;
            }
        }
    
        public class NumericDocumentFilter extends DocumentFilter {
    
            public void insertString(DocumentFilter.FilterBypass fb, int offset,
                    String string, AttributeSet attr)
                    throws BadLocationException {
    
                StringBuilder buffer = new StringBuilder(string);
                boolean isBad = false;
                for (int i = buffer.length() - 1; i >= 0; i--) {
                    char ch = buffer.charAt(i);
                    if (!Character.isDigit(ch)) {
                        buffer.deleteCharAt(i);
                        isBad = true;
                    }
                }
                if (isBad) {
                    Toolkit.getDefaultToolkit().beep();
                }
                super.insertString(fb, offset, buffer.toString(), attr);
            }
    
            public void replace(DocumentFilter.FilterBypass fb,
                    int offset, int length, String string, AttributeSet attr) throws BadLocationException {
                if (length > 0) {
                    fb.remove(offset, length);
                }
                insertString(fb, offset, string, attr);
            }
        }
    
        public class MyTableModel extends AbstractTableModel {
    
            @Override
            public int getRowCount() {
                return 1;
            }
    
            @Override
            public int getColumnCount() {
                return 2;
            }
    
            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                return "";
            }
    
            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return true;
            }
    
            @Override
            public Class<?> getColumnClass(int columnIndex) {
                return String.class;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jTable with two columns.First column is set as Boolean(for checkbox) and
I have a JTable which has certain cells editable. If the user enters in
I have a JTable with few columns that are painted as checkboxes. What I
I have a JTable with two columns and both are JComboBox, for this purpose
I have a JTable in Java that has a custom dataMOdel and custom renderer.
I have a JTable that is using a TableColumnModelListener() to detect when the column
I am using an editable JTable that contains a column named Subject. When the
I have a JTable where one column occasionally has a fair amount of text
I am trying to make a JTable that has column spans available. Specifically, I
Have an application that has a Jtable with the data held in RAM, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.