Solved

JTable - message when clicking on non-editable JCheckBox

Posted on 2004-10-12
19
629 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

706 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

16 Experts available now in Live!

Get 1:1 Help Now