By Emil

2010-08-28 13:28:09 8 Comments

I have a JTable which is loaded from a data-structure using table model.The data-structure is of the format NavigableMap<Float,NavigableMap<Float,Boolean>>.An example data is:

Table Format:
 Range     f1,v1   f2,v2    f3,v3   f4,v4
12.1-30.2 30,true 32,false 45,true 50,false
30.2-45.6 30,true 32.4,true 45,true 50.1,true

The above data format is represented in the DS as

DS Format:
Key  Value
12.1 <<30,true>,<32,false>,<45,true>,<50,false>>
30.2 <<30,true>,<32.4,true>,<45,true>,<50.1,true>>
45.6 null

I have managed to represent the above given data in Jtable using table-model.Once the data is loaded from the DS to the table I have to allow user edit.Now this is where I have problem.My doubt is whether is should keep the data structure synchronized with the changes in the table or should i recreate the DS from the table once the user finish editing and then replace it with the old one.

More over I need to validate the data(for example from above - Suppose the user want's to edit the value 30.1.He should only be allowed to enter values between 12.1 and 45.6.Since data the tables are string's (once loaded) I'm planning to use regex and key-listener and consume all user key presses which doesn't match the regex and values which doesn't come within the range.I'm not sure is this is a good idea or what are implications.I would like to get some suggestions on this.


@camickr 2010-08-28 15:07:05

I would recreate your DS once the user is finised editing the table.

You can always create a custom editor to display a popup dialog where you have two separate text fields for each value of the range. Then you can edit each field as a double value within your specified range and recreate the formatted string before saving it to the model. Here's an old example I have lying around to get you started:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;

 * The editor button that brings up the dialog.
//public class TablePopupEditor extends AbstractCellEditor
public class TablePopupEditor extends DefaultCellEditor
    implements TableCellEditor
    private PopupDialog popup;
    private String currentText = "";
    private JButton editorComponent;

    public TablePopupEditor()
        super(new JTextField());


        //  Use a JButton as the editor component

        editorComponent = new JButton();
        editorComponent.setContentAreaFilled( false );

        //  Set up the dialog where we do the actual editing

        popup = new PopupDialog();

    public Object getCellEditorValue()
        return currentText;

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

        SwingUtilities.invokeLater(new Runnable()
            public void run()
                popup.setText( currentText );
//              popup.setLocationRelativeTo( editorComponent );
                Point p = editorComponent.getLocationOnScreen();
                popup.setLocation(p.x, p.y + editorComponent.getSize().height);

        currentText = value.toString();
        editorComponent.setText( currentText );
        return editorComponent;

    *   Simple dialog containing the actual editing component
    class PopupDialog extends JDialog implements ActionListener
        private JTextArea textArea;

        public PopupDialog()
            super((Frame)null, "Change Description", true);

            textArea = new JTextArea(5, 20);
            textArea.setLineWrap( true );
            textArea.setWrapStyleWord( true );
            KeyStroke keyStroke = KeyStroke.getKeyStroke("ENTER");
            textArea.getInputMap().put(keyStroke, "none");
            JScrollPane scrollPane = new JScrollPane( textArea );
            getContentPane().add( scrollPane );

            JButton cancel = new JButton("Cancel");
            cancel.addActionListener( this );
            JButton ok = new JButton("Ok");
            ok.setPreferredSize( cancel.getPreferredSize() );
            ok.addActionListener( this );

            JPanel buttons = new JPanel();
            buttons.add( ok );
            buttons.add( cancel );
            getContentPane().add(buttons, BorderLayout.SOUTH);

            getRootPane().setDefaultButton( ok );

        public void setText(String text)
            textArea.setText( text );

        *   Save the changed text before hiding the popup
        public void actionPerformed(ActionEvent e)
            if ("Ok".equals( e.getActionCommand() ) )
                currentText = textArea.getText();

            setVisible( false );

    public static void main(String[] args)
        String[] columnNames = {"Item", "Description"};
        Object[][] data =
            {"Item 1", "Description of Item 1"},
            {"Item 2", "Description of Item 2"},
            {"Item 3", "Description of Item 3"}

        JTable table = new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);

        // Use the popup editor on the second column

        TablePopupEditor popupEditor = new TablePopupEditor();
        table.getColumnModel().getColumn(1).setCellEditor( popupEditor );

        JFrame frame = new JFrame("Popup Editor Test");
        frame.getContentPane().add( scrollPane );
        frame.setLocationRelativeTo( null );

@R71 2014-08-23 10:22:40

NOTE: the above code shows multi-line, but does not allow a CR to be entered in the editor, because the ENTER is trapped as an exit signal. So if your data also has to be multi-line, just comment the 2 lines using "keyStroke" in PopupDialog constructor. In that case, the opoup will exit only if you click on OK button.

@bobndrew 2018-05-09 15:19:03

This is a great example! But we have problems with losing the first keystroke most of the time and posted a new question here on stackoverflow:…

Related Questions

Sponsored Content

19 Answered Questions

[SOLVED] Tree data structure in C#

  • 2008-09-15 20:58:20
  • stimms
  • 380370 View
  • 249 Score
  • 19 Answer
  • Tags:   c# data-structures

6 Answered Questions

[SOLVED] Data structure for maintaining tabular data in memory?

3 Answered Questions

[SOLVED] What are the underlying data structures used for Redis?

5 Answered Questions

[SOLVED] C++ : suitable Data Structure for this given scenario

1 Answered Questions

83 Answered Questions

1 Answered Questions

[SOLVED] Data structure to contain data indexed by column and row string indexes

  • 2011-06-03 13:56:21
  • Johann Blais
  • 1611 View
  • 6 Score
  • 1 Answer
  • Tags:   c# data-structures

1 Answered Questions

Sponsored Content