Solved

JTable - message when clicking on non-editable JCheckBox

Posted on 2004-10-12
19
637 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example for saving data 19 57
ejb example issues 3 28
CSV file parsing thru Java 13 34
Eclipse for Java EE development 2 28
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

821 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