Solved

JTable - message when clicking on non-editable JCheckBox

Posted on 2004-10-12
19
634 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse argument 14 60
Spring Framework HTTPSession management 1 33
hibernate jars 4 26
Unhandled exception type Exception 18 28
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now