Solved

JTable - message when clicking on non-editable JCheckBox

Posted on 2004-10-12
19
639 Views
Last Modified: 2008-01-09
Hi, I have a JTable with a column of JCheckBox. On some of the rows this column is editable while on others it is not. I would like some sort of pop-up message to let the user know why they can't edit if they try to click on a non-editable check box (may be a different reason for a different row). I can't see what sort of listener I need to use here, can anyone help? Or otherwise a way to change the look of the checkboxes that are not editable so the difference between editable and non-editable is clear.
Thanks
0
Comment
Question by:Scoteq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
19 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12284500
>> Or otherwise a way to change the look of the checkboxes that are not editable so the difference between editable and non-editable is clear.
You could write you own cell renderer for this boolean values.
In case of non-editable you could show an uneditable check box
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284508
>>I can't see what sort of listener I need to use here
When they (try to) change the value your table model's setValue(Object aValue, int row, int column)) is called.
In this function you could pop up a message box in case the column is uneditable
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284517
That are the two possibilities:
1) Write your own cell renderer (to represent the boolean as you want)
or
2) Use your own table model (extends DefaultTableModel)
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 1

Author Comment

by:Scoteq
ID: 12284543
>> 2) Use your own table model (extends DefaultTableModel)
Thanks for the suggestions. I already have a defined a table model (that implements TableModel rather than extending Default table model, not sure why as I copied this code form elsewhere). I will try putting the code in the setValue(Object aValue, int row, int column) method as you suggest.
0
 

Expert Comment

by:Cyberdoc
ID: 12284612
Hi,

1.) write an own TableModel based on a DefaultTableMolde like zzynx said, and overwrite the
public boolean isCellEditable(int row, int column) {
  if (<condition1>)
    return false;
  return true;
}

2.) The click you can get over a MouseListener added on the JTable. Then ask for the cell at mousepos, if it is editable. Yes, do nothing and no open a dialog with the message
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 12284622
>> I already have a defined a table model
Then you also have a function isCellEditable().
Well, this should now always return true (otherwise setValue() won't never be triggered)
But, the logic to determine if editable or not should now be moved into setValue()
If not editable:
1) show your pop up
2) don't really change the cell value
0
 
LVL 1

Author Comment

by:Scoteq
ID: 12284699
>> otherwise setValue() won't never be triggered
I just noticed that, and have managed to get the pop-up working from the isCellEditable (and so can see that I could move it to setValue) method... but the table model doesn't know where it has been called from does it? I have a class myPanel that extends JPanel, and this is where the JTable is, and then a class myTableModel. How can I make sure the pop-up is in an appropriate place in relation to the JPanel (would be a bit confusing/annoying if the user has moved the panel to one corner of the screen and a pop-up appears in the middle of the screen rather than in front of the panel)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284716
>> would be a bit confusing/annoying if the user has moved the panel to one corner of the screen and a pop-up appears in the middle of the screen
That's a matter of taste. I find the middle of the screen a good, common place to place warning messages.
If you want it to be placed relative to the place where one clicked, then I think you should follow the method of Cyberdoc (MouseListener on your table)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284721
In that case the MouseEvent contains the coordinates of the click point
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284728
>>How can I make sure the pop-up is in an appropriate place
Hey, maybe you can by using JTable's function

 Rectangle getCellRect(int row, int column, boolean includeSpacing)
          Returns a rectangle for the cell that lies at the intersection of row and column.
0
 
LVL 1

Author Comment

by:Scoteq
ID: 12284844
>> maybe you can by using JTable's function
Isn't there still the same problem, in that the table model doesn't know about the jtable instance that uses it?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284864
>> the table model doesn't know about the jtable instance that uses it?
You can simple pass it in the constructor so that it is available when needed:

class MyTableModel extends ... {
   
    private JTable theTable = null;

    public MyTableModel(JTable table) {        // <<<<<<
        super();
        theTable = table;       // <<<<<<<<<<<<
    }
}
0
 
LVL 1

Author Comment

by:Scoteq
ID: 12285017
It's working. I went for a variation on Zzynx's solutions - it seems simpler for me than a mouse listener (In fact I passed the JFrame that I am using to the table model, so I could create a message dialog from isCellEditable method using the JFrame      
JOptionPane.showMessageDialog(myJFrame,"Cell cannot be edited","",JOptionPane.INFORMATION_MESSAGE);.  
In this way the message appears in the centre of the frame (which is good enough) thanks for all your advice.
0
 
LVL 1

Author Comment

by:Scoteq
ID: 12285036
Sorry Zznx, I think I just rated your answer as average when I meant to rate it as excellent.. do you know if I can change that?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12285066
>> Sorry Zznx, I think I just rated your answer as average when I meant to rate it as excellent.. do you know if I can change that?
Sure.
1) Aks to delete this question by posting a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body: Please reopen this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21164655.html

2) Then you can reaccept with an A grade
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12285070
>> 1) Aks to delete this question
that should be ask to **reopen** this question of course ;°)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12285623
Thank you.
(Didn't know that's another option)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question