Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 646
  • Last Modified:

JTable - message when clicking on non-editable JCheckBox

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
Scoteq
Asked:
Scoteq
  • 11
  • 5
1 Solution
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
>>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
 
zzynxSoftware engineerCommented:
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
ScoteqAuthor Commented:
>> 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
 
CyberdocCommented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
ScoteqAuthor Commented:
>> 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
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
In that case the MouseEvent contains the coordinates of the click point
0
 
zzynxSoftware engineerCommented:
>>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
 
ScoteqAuthor Commented:
>> 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
 
zzynxSoftware engineerCommented:
>> 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
 
ScoteqAuthor Commented:
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
 
ScoteqAuthor Commented:
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
 
zzynxSoftware engineerCommented:
>> 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
 
zzynxSoftware engineerCommented:
>> 1) Aks to delete this question
that should be ask to **reopen** this question of course ;°)
0
 
zzynxSoftware engineerCommented:
Thank you.
(Didn't know that's another option)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

  • 11
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now