Needs double click to edit cell in JTable

I know this is bad design but there's no other way that I could possibly meet the requirement which is to have dynamic components. Anyhow, the requirement is to have any kind of components(e.g. TextFields, Combobox, Regular expression fields, a panel with a number of checkboxes) in a table cell in the same column. It would have been easier to do this by using the the DefaultCellEditor, with the 'panel containing a number of checkboxes' I cannot use it.
I already have an implementation for this requirement. There was no problem with it when we used Java 1.5. But when we shifted to Java 1.6, I noticed that I need to double-click on a cell so that I can edit it. I did not notice this behavior at all with 1.5. What could have changed in 1.6?
Below, are my (trimmed-down) codes:
//TestComponent.java
public class TestComponent extends JPanel{
private int componentHeight=16;
public TestComponent(int i){
this.setPreferredSize(new Dimension(90, this.componentHeight));
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
switch (i){
case 0: createTextField(); break;
case 1: createCheckBox(); break;
case 2: createCombo(); break;
private void createTextField(){
String text = null;
int textFieldMaxLength = 5;
JTextField oText = new JTextField(textFieldMaxLength);
// add it to this Panel:
this.add(oText);
// set the data:
text = "test";
oText.setText(text);
// set size for this TextField:
Dimension fieldSize = new Dimension(
textFieldMaxLength * 13, this.componentHeight);
          oText.setPreferredSize(fieldSize);
          oText.setMinimumSize(fieldSize);
          oText.setMaximumSize(fieldSize);     
private void createCheckBox(){
JCheckBox chkbox = new JCheckBox();
this.add(chkbox);
private void createCombo(){
JComboBox combo = new JComboBox(new String[]{"apple", "orange", "plum", "grapefruit"});
Dimension fieldSize = new Dimension(                    5 * 13, this.componentHeight);
          combo.setPreferredSize(fieldSize);
          combo.setMinimumSize(fieldSize);
          combo.setMaximumSize(fieldSize);
this.add(combo);
//ComponentCellEditor.java
public class ComponentCellEditor extends AbstractCellEditor
     implements TableCellEditor, Serializable{
protected JComponent editorComponent = null;     
public Component getComponent() {
return editorComponent;
public Object getCellEditorValue() {
return editorComponent;
public boolean isCellEditable(EventObject anEvent) {
return true;
public boolean shouldSelectCell(EventObject anEvent) {
return true;
public boolean stopCellEditing() {
fireEditingStopped();
return true;
// Implementing the TreeCellEditor Interface
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
this.editorComponent = (TestComponent)value;
return editorComponent;
//ComponentCellRenderer.java
public class ComponentCellRenderer implements TableCellRenderer
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
TestComponent oComp = (TestComponent) value;
if (isSelected) {
oComp.setForeground(table.getSelectionForeground());
oComp.setBackground(table.getSelectionBackground());
} else {
oComp.setForeground(table.getForeground());
oComp.setBackground(table.getBackground());     
return oComp;
//TestTable.java
public class TestTable
public static void main(String []args){
String columns[] = {"Text", "Value"};          
Class types[] = {String.class, TestComponent.class};
boolean editable[] = {false, true};
AsrTable table = new AsrTable(new Dimension(200, 150), columns, types, editable);
table.addRow(new Object[]{"Field1", new TestComponent(0)});
table.addRow(new Object[]{"Field2", new TestComponent(1)});
table.addRow(new Object[]{"Field3", new TestComponent(2)});
table.setDefaultEditor(TestComponent.class, new ComponentCellEditor());
table.setDefaultRenderer(TestComponent.class, new ComponentCellRenderer());
table.setShowGrid(false);
table.setRowHeight(20);
table.setRowSelectionAllowed(false);
JFrame frame = new JFrame();
frame.addWindowListener( new WindowAdapter() {
     public void windowClosing(WindowEvent e)
     Window win = e.getWindow();
     win.setVisible(false);
     win.dispose();
     System.exit(0);
JScrollPane pane = new JScrollPane(table);
frame.getContentPane().add(pane);
frame.pack();
frame.setVisible(true);
}

My last post doesn't have Code Formatting.
// TestComponent.java
public class TestComponent extends JPanel{
     private int componentHeight=16;
     public TestComponent(int i){
          this.setPreferredSize(new Dimension(90, this.componentHeight));
          setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
          switch (i){
               case 0: createTextField(); break;
               case 1: createCheckBox(); break;
          case 2: createCombo(); break;
     private void createTextField(){
          String text = null;
          int textFieldMaxLength = 5;
          JTextField oText = new JTextField(textFieldMaxLength);
          // add it to this Panel:
          this.add(oText);
          // set the data:
          text = "test";
          oText.setText(text);
          // set size for this TextField:
          Dimension fieldSize = new Dimension(
                    textFieldMaxLength * 13, this.componentHeight);
          oText.setPreferredSize(fieldSize);
          oText.setMinimumSize(fieldSize);
          oText.setMaximumSize(fieldSize);
     private void createCheckBox(){
          JCheckBox chkbox = new JCheckBox();
          this.add(chkbox);
     private void createCombo(){
          JComboBox combo = new JComboBox(new String[]{"apple", "orange", "plum", "grapefruit"});
          Dimension fieldSize = new Dimension( 5 * 13, this.componentHeight);
          combo.setPreferredSize(fieldSize);
          combo.setMinimumSize(fieldSize);
          combo.setMaximumSize(fieldSize);
          this.add(combo);
// ComponentCellEditor.java
public class ComponentCellEditor extends AbstractCellEditor 
     implements TableCellEditor, Serializable
     protected JComponent editorComponent = null;     
     public Component getComponent() {
          return editorComponent;
     public Object getCellEditorValue() {
          return editorComponent;     
     public boolean isCellEditable(EventObject anEvent) {
          return true;
     public boolean shouldSelectCell(EventObject anEvent) {
          return true;
     public boolean stopCellEditing() {
          fireEditingStopped();
          return true;
//  Implementing the TreeCellEditor Interface
    public Component getTableCellEditorComponent(JTable table, Object value,
          boolean isSelected, int row, int column) {
          this.editorComponent = (TestComponent)value;
          return editorComponent;
// ComponentCellRenderer.java
public class ComponentCellRenderer implements TableCellRenderer
     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row,
               int column) {
          TestComponent oComp = (TestComponent) value;
          if (isSelected) {
               oComp.setForeground(table.getSelectionForeground());
               oComp.setBackground(table.getSelectionBackground());
          } else {
               oComp.setForeground(table.getForeground());
               oComp.setBackground(table.getBackground());
          return oComp;
// TestTable.java
public class TestTable
     public static void main(String []args){
          String columns[] = {"Text", "Value"};          
          Class types[] = {String.class, TestComponent.class};
          boolean editable[] = {false, true};
          AsrTable table = new AsrTable(new Dimension(200, 150), columns, types, editable);
          table.addRow(new Object[]{"Field1", new TestComponent(0)});
          table.addRow(new Object[]{"Field2", new TestComponent(1)});
          table.addRow(new Object[]{"Field3", new TestComponent(2)});
          table.setDefaultEditor(TestComponent.class, new ComponentCellEditor());
          table.setDefaultRenderer(TestComponent.class, new ComponentCellRenderer());
          table.setShowGrid(false);
          table.setRowHeight(20);
          table.setRowSelectionAllowed(false);
          JFrame frame = new JFrame();
          frame.addWindowListener( new WindowAdapter() {
               public void windowClosing(WindowEvent e)
                    Window win = e.getWindow();
                    win.setVisible(false);
                    win.dispose();
                    System.exit(0);
          JScrollPane pane = new JScrollPane(table);
          frame.getContentPane().add(pane);
          frame.pack();
          frame.setVisible(true);
}

Similar Messages

  • Detecting mouse clicks in editable cell of JTable

    Hi everyone :)
    I thought that this question might have been asked before, but I have searched the forums extensively and have not been able to find the solution.
    What I want to achieve is to detect single and double mouse clicks on JTable cells (that are editable).
    For example, I have a JTable and there exists within it an editable cell. If the user clicks on it once then that cell goes into edit mode, and the user can type directly into the cell. I have already successfully implemented this.
    However, what I also want to do is detect a double-click so that I can pop up a dilaog that shows a list of default values that the user can select.
    So here is what I want;
    1. User clicks on the cell once.
    2. Cell moves into edit mode.
    3. If the user clicks again within a certain time interval then cancel edit mode and pop up a dialog containing values that the user can select from.
    I think that to do this I need to be able to detect mouse clicks on the cell that is currently being edited. So far I have been unable to discover how this is done. I have even tried extending JTextField to get what I want, but with no luck.
    Any help would be greatly appreciated.
    Kind regards,
    Ben Deany

    Thanks for the reply.
    Unfortunately, it is not possible to call 'AddMouseListener()' on a cell editor. You are only able to call 'addCellEditorListener()' and that only allows two events to the broadcast (edit cancel, and edit stop).
    Ben

  • Double clicking for editing JCheckBox in JTable under java 1.6?

    Hi all,
    I have a JTable with JCheckboxes in a column (and associated renderer and editor). All worked good with/until java 1.5.
    Now with java 1.6 I have to click two times on the checkbox in order to change the selection...
    Anyone has experimented a similar problem??

    I have spent some hours over this strange problem finding no way to solve it and no workaround.
    Could this be a bug introduced with java 1.6?
    Note that the behaviour is more strange as what I hade described in my last post:
    - mouse click on a cell with a checkbox: checkbox CHANGE state
    - mouse click on other celll with checkbox: NO EFFECT
    - click on other celll: checkbox CHANGE state
    - click on other celll: NO EFFECT
    - click on other celll: checkbox CHANGE state
    - click on other celll: NO EFFECT
    - click on other celll: checkbox CHANGE state
    - click on other celll: NO EFFECT
    - ... and so on
    Really strange, at least for me...

  • How? double click to edit a cell in a JTable (Custom Editor/TableModel)

    I have a JTable with a custom table model that when you click anything in the first column a custom editor appears. I would like to know how to make the custom editor appear after a double click on any cell in the first column. It can probably be done with a MouseListener but is there any easier way to do this?
    Thanks.

    this works for me.
    public class MyJcustomEditor extends DefaultCellEditor {
    public MyJcustomEditor(JTextField tField) {
    super(tField);
    setClickCountToStart(2);
    }

  • Unable to edit cells in JTable on single click of the cell.

    Hi,
    I am unable to edit a cell in JTable on single click of the cell. If I double click on the cell, I am able to edit it. Please help me.
    Thanks
    Subbu

    Thanks for all replies. Now, i am able to edit the cell on single click.

  • Can No Longer See Photos After Double Click to Edit

    Photos and videos only visible as thumbnails. When double click to edit, cannot see. However, can double click on books and see them in edit mode. All photos still located in iPhoto Library. Have run backup and gone through all four options for database repair. Have uninstalled and reinstalled version 9.5.1. Still cannot see photos. Running latest version of OS Mavericks and latest version of iPhoto.

    There are several possible causes for the Black Screen issue
    1. Permissions in the Library: Back Up and try rebuild the library: hold down the command and option (or alt) keys while launching iPhoto. Use the resulting dialogue to rebuild. Include the option to check and repair permissions.
    2. Minor Database corruption: Back Up and try rebuild the library: hold down the command and option (or alt) keys while launching iPhoto. Use the resulting dialogue to rebuild.
    3. A Damaged Photo: Select one of the affected photos in the iPhoto Window and right click on it. From the resulting menu select 'Show File (or 'Show Original File' if that's available). (On iPhoto 11 this option is under the File -> Reveal in Finder.) Will the file open in Preview? If not then the file is damaged. Time to restore from your back up.
    4. A corrupted iPhoto Cache: Trash the com.apple.iPhoto folder from HD/Users/Your Name/ Library/ Caches...
    5. A corrupted preference file: Trash the com.apple.iPhoto.plist file from the HD/Users/ Your Name / library / preferences folder. (Remember you'll need to reset your User options afterwards. These include minor settings like the window colour and so on. Note: If you've moved your library you'll need to point iPhoto at it again.)
    If none of these help: As a Test:
    Hold down the option (or alt) key key and launch iPhoto. From the resulting menu select 'Create Library'
    Import a few pics into this new, blank library. Is the Problem repeated there?

  • How to make Double Click To Edit work instead of ClickToEdit for Af:Table

    Hi All,
    We use AF:Table in clickToEdit mode and most of the time the users use this table for selecting a master record then work on the detail records. To avoid the cost of rendering the selected row as editable we would like to change the behavior to Double Click To edit. This way the user can intentionally put the selected row in edit mode when needed. What would be the best approach to achieve this ?
    We use 11.1.1.6 and ADF BC.
    Thanks

    Not sure if you can get it to work in 11.1.1.6.0 as you don't have the ActiveRowKey property which can be used to make a table row editable. The property comes with 11gr2.
    What you can try is to set the tabel to clicktoedit, add a clientListener which you use to listen to the click event and then cancel the event. Add another clientListener whihc handles the doubleClick event, make the clicked row the current row (see http://www.oracle.com/technetwork/developer-tools/adf/learnmore/56-handle-doubleclick-in-table-170924.pdf) and, well now comes the problem, make the current row editable. I don't know how to do this without the ActiveRowKey property.
    One other possible solution is a trick described here http://dstas.blogspot.com/2010/08/press-edit-button-to-make-table-row.html
    here a transient attribute is used to manipulate the isEditable() status of an attribute of the VO. This should do the trick, but is much more work.
    May be someone else knows a better solution.
    Timo

  • Can't edit in Design tab - "overflow: hidden (double-click to edit)" - cause, remedy?

    In the Design tab, the content of some web pages becomes uneditable (where it had previously been editable), even though it is in an editable region in the template. All of the uneditable text has a black background. When the cursor is floated over this text a popup gives the message "overflow: hidden (double-click to edit)". (Note: the text is still editable in the Code tab.) Double clicking in the Design tab often doesn't do anything but sometimes corrects the problem. The following also didn't help:
    1. Making revisions in the Code tab and then refreshing. (The revisions show up in the Design tab but the text there is still uneditable.)
    2. Closing and reloading the page.
    The content of other pages that use the same template remains editable.
    What causes this problem?
    What is the best method to prevent it and correct it?
    DW CS6
    Win7 x64
    Thanks,
    Don C.

    The problem is always a side-effect of the use of the overflow:hidden style. So why use it? It's tremendously valuable as a way of clearing floats inside a container, rather than adding a float clearing page element. And besides, if you can still edit the content in Code view, why worry? Anyhow, paste this into a new HTML page and try it.
    <div style="height:100px;overflow:hidden;">
    <p>This content will not be editable without double clicking on it. Furthermore, any floats contained within this div will be cleared before closing the div, allowing the div to completely wrap its contents.</p>
    <p>This content will not be editable without double clicking on it. Furthermore, any floats contained within this div will be cleared before closing the div, allowing the div to completely wrap its contents.</p>
    <p>This content will not be editable without double clicking on it. Furthermore, any floats contained within this div will be cleared before closing the div, allowing the div to completely wrap its contents.</p>
    <p>This content will not be editable without double clicking on it. Furthermore, any floats contained within this div will be cleared before closing the div, allowing the div to completely wrap its contents.</p>
    </div>
    In Design view, you will only see two paragraphs. When you click in either of those two paragraphs, you will not be able to edit anything. When you double click you will see the div expand to show all 4 paragraphs, and you will then be able to click and edit at will.

  • Keypressed event for a particular  edited cell of jtable

    hi friend,
    how to write the key pressed event for the edited cell of jtable which having focus on that particular cell.
    pls help me out.

    [http://catb.org/~esr/faqs/smart-questions.html]
    [http://mindprod.com/jgloss/sscce.html]
    db

  • How to get "Double-click to edit" behavior in fields added to Master slides

    Hi! I edited a master slide to add a box of Placeholder Text. Now the word "Text" appears whenever I make a new slide (child of the master), which is fine. But if I don't change the text on a particular slide, the word "Text" shows up there when I run the slideshow. This is different from the behavior of the Apple-defined placeholder text, where the phrase "Double-click to edit" does NOT appear when you run the slideshow.
    I've searched the help files and tried various Inspector settings, but cannot seem to get this wonderful timesaving feature working for my own Master Slide fields. Is it possible?

    I was just looking for a solution to this... how timely! The closest I found to being able to have an "apple-defined placeholder" is by following all your steps (editing master, making new text box), but then doing the following:
    1) select the text box
    2) go to the "Format">"Advanced" menu and select "Define as Text Placeholder."
    This isn't the same, since the text will always show up on the slides (as opposed to only showing up if YOU wrote something in the box... you can delete it if you want, but its not the same as apple's default), but it does have the functionality you want. I hope this helps you!
    EDITED TO ADD:
    ...aaaand the age-old trick of reading the manual works again. Go to page 188 of the linked document and you'll see that there's a different way to do it. In short:
    1) Make a text box in a master slide, keep it selected.
    2) In the Inspector, go to "slide inspector" (second from right), and select the "appearance" tab.
    3) Check the checkbox next to "Define as Text Placeholder." Assign it a tag.
    For me, I found that once I did this, I could delete the text inside the textbox and it wouldn't automatically be deleted. I assume that assigning it a tag has something to do with that. I still don't know how to get text to automatically show up in the textbox... that seems to be a special functionality reserved for the apple-defined boxes. Again, I hope this helps!
    Message was edited by: Eliezer Kanal

  • Mouse Double Click on an editable cell of JTable

    Hi Pros:
    Maybe this is an old question but no answser from Forum.
    I have a JTable with adding MouseListener. I tried to put double click behavior on nay row in the table. The problem was that this action can obly work on the uneditable cell and do not work on editable cell.
    Anyone have ideas and help me.
    Thank you!

    Hi Wang,
    I have a problem similar to the one you have some time back.
    I have a query for which I need to use PreparedStatement .The query runs likes this :-
    String str = " Select ? , ename from emp where deptno ? ";
    The values of ? need to be assigned dynamically.
    But I cannot create Prepared Statement from this query .
    If you have got answer to your questions can you inform me at
    [email protected]
    Thanks in advance

  • Double-click to edit?

    Anyone know of a hack er something to get a double-click in the card panel's background to flip between edit/view modes?
    --jason
    MacBookPro17,PowerMacG5   Mac OS X (10.4.10)   Avid Crack Smoker

    Ben:
    1 - No. We can't change those default features of iPhoto.
    2 - the highlight color is hard coded in the application and is not user changeable.
    It's just the nature of the beast.
    TIP: For insurance against the iPhoto database corruption that many users have experienced I recommend making a backup copy of the Library6.iPhoto database file and keep it current. If problems crop up where iPhoto suddenly can't see any photos or thinks there are no photos in the library, replacing the working Library6.iPhoto file with the backup will often get the library back. By keeping it current I mean backup after each import and/or any serious editing or work on books, slideshows, calendars, cards, etc. That insures that if a problem pops up and you do need to replace the database file, you'll retain all those efforts. It doesn't take long to make the backup and it's good insurance.
    I've created an Automator workflow application (requires Tiger), iPhoto dB File Backup, that will copy the selected Library6.iPhoto file from your iPhoto Library folder to the Pictures folder, replacing any previous version of it. It's compatible with iPhoto 08 libraries and Leopard. iPhoto does not have to be closed to run the application, just idle. You can download it at Toad's Cellar. Be sure to read the Read Me pdf file.

  • Double click on Datagrid cell

    Hi,
    I'm developing an application where I'm using a datagrid that shows some information from an xml. Every cell in the grid is of the form
    <itemRenderer>
    <compoment>
    <Vbox>
    <HBox>
    <Text/> //some text goes within this block
    <Text/> //some text goes within this block
    <Text/> //some text goes within this block
    </Hbox>
    </VBox>
    </component>
    </itemRenderer>
    I have set a function call for the datagrid on ItemDoubleClick event; and this even gets triggered only when i double click on any TEXT that is on the cell; and NOT on any part of the cell. However, I would like to have this triggered when the user clicks anywhere in the cell.
    Sombody please help,
    Thanks!!

    Hi,
    Thanks for the suggestion. Maybe I wasn't clear in explaining the issue here. Let me throw some light now, My code is somewhat like this
    <mx:VBox opaqueBackground="{myxml.@data>10 ?  '0xA2FEA2' : '0xFEA2A2' }" width="100%" height="100%" verticalScrollPolicy="off">
         <mx:Box width="100%" height="100%">
              <mx:Text id="txt1" text="Available" width="100%"/>
               <mx:Text id="txt2" text="{myxml.@data}"/>
         </mx:Box>
    </mx:VBox>
    The output is shown in the attachment. Now whenever I double click on the text in the datagrid(highlighted in blue in the attachment) my doubleclick even is fired; but if i click anywhere in the cell(green region in the attachment) my doubleclick event is not called.
    Hope this helps! Looking forward for your assistance,
    Thanks again!!
    Cheers

  • My IPad needs double click to access anything and does not scroll through anything

    IPad needs a double click to access anything and then does not scroll down.

    Do you need to tap (and the app/item then gets a box around it) and then double-tap ? If you do then you probably have VoiceOver (one of the accessibility features) 'on'. Try triple-clicking the home button and see if that turns it off, and if it does you can then change what a triple-click does via Settings > General > Accessibility > Triple-Click Home.
    If that doesn't turn it off then you can either turn it off directly on the iPad (you need to use a tap-to-select and then double-tap to activate/type process and 3 fingered scrolling) to go into Settings > General > Accessibility and turn VoiceOver 'off', or you can do it by connecting to your computer's iTunes : http://support.apple.com/kb/HT4064

  • Geturl needs double-click

    Trivial problem: a Flash 8 animation which has an embedded
    URL button with a geturl - whether 'press' or 'release' - attached
    needs a double click to function; single-click selects the
    animation itself (frame displays the 'selected' border). How pls
    can I get the button to work with a single click like a normal
    <a> URL ?

    I think the problem you facing is the active content warning.
    You can easily see that when you mouse over your flash movie
    a gray border appears around it and a bubble tells you that you
    need to click to activate.
    If this is the case, you need to download the flash 8 update
    that has a new set of templates to remove that system.
    The update can be found here:
    http://www.adobe.com/support/flash/downloads.html

Maybe you are looking for

  • How to set dynamic deadlines in ccBPM blocks?

    Is there any chance to set a dynamic deadline in a ccBPM block? My business case is that I receive messages which contain a "latest execution" date/time field. I need to take this value and set it as a deadline for the block in the business process.

  • What external HD for bootable drive

    Have tried to make a bootable hard drive using Disk Utility as advised by Apple Tech. Didn't work. The external HD is a WD 1T that is a few years old and wouldn't let me change the size of the 2 partitions on it so that I could install the OS 10.3 fr

  • HT204406 Don't understand how to buy new music?

      I had Itunes a long time and I would like to buy more music. I don't understand how to get into purchasing the music.

  • DVD menu button rollovers not working on PS3

    I am totally new to Adobe Encore, and I tried to make my first DVD project yesterday.  I used the default "Party" menu which I then edited in Photoshop to include my own graphic assets and text.  The project is just two videos of a play that I record

  • How do I enable Function Keys for Capt 5 simulations?

    I have a simualtion where the learner is instructed to press certain function keys (F11, F4) to "activate" a window within out system (this new window is part of the next slide). During preview, the function keys are enabled,but when launching the si