How to do a check before allowing editing of a given row in a JTable

The problem is quite basic. I have a JTable showing cached data from a database. If a user clicks in a cell for editing, I want to attempt a lock on that row in the database. If the lock does not succeed, I want to prevent editing.

But I can't seem to find any clean way to accomplish this. Am I missing something?

Asked by: Anna833 | Posted: 23-01-2022

Answer 1

Before editing/setting value the table model is asked via TableModel.isCellEditable(row,col) whether this cell is editable. Here you can implement your lock. and after TableModel.setValue(row,col,val) you should unlock this. BUT. The lock operation should take a lot of time and makes your UI un-responsible. And it si BAD. Try different approach. What about lazy fail? You lock the row, check for validity of data and fail if data are newer. If data are OK, you put them down. UNLOCK.

Answered by: Ryan561 | Posted: 24-02-2022

Answer 2

Oracle has a nice way of handling this but I don't know how universally applicable it is.

In Oracle, you can use FOR UPDATE on a SELECT statement to lock a record as you read it.

For example, if you are fetching a row for display:

select * into v_row from my_table where my_table_id = 1
for update;

This will allow reads but prevent updates. If another transaction has a lock your transaction will wait until it becomes available (or timeout, eventually). If you want the statement to throw an exception if you try to lock you add NOWAIT.

select * into v_row from my_table where my_table_id = 1
for update nowait;

If the row is already locked you will get:

ORA-00054: resource busy and acquire with NOWAIT specified.

Hope that helps.

Answered by: Steven529 | Posted: 24-02-2022

Answer 3

Because you have to test on the click you can't use the model's way of doing this so you should try overriding the JTable's public void changeSelection(int rowIndex, int columnIndex, boolean toggle, oolean extend) method. If the row is locked then don't call super.changeSelection and it should leave the row unselected.

Answered by: Oliver437 | Posted: 24-02-2022

Answer 4

Instead, you could wait until the user actually changes something, then override JTable.editingStopped to do the work there (you could even check to see if the value changed)

That way no locking is done until the user actually changes something.

Answered by: Alford278 | Posted: 24-02-2022

Similar questions

c# - Allowing a method to lock its parent Object in Java

Is there a way in Java to get a method to lock (mutex) the object which it is in? I know this sounds confusing but basically I wan't an equivelent to this snippet of C# but in Java. lock(this) { // Some code here... } I've been tasked with reimplementing an API written in .Net into Java, and I've been asked to keep the Java version as similar to the .Net version as humanly poss...

java - TreeMap only allowing one item to be put into it?

I'm developing a Java application and am new to using TreeMap. The program needs to keep track of the number of occurrences of each word in a text file. However, I'm having trouble putting my data into the TreeMap. It works fine when I use the same exact code to put the data into a HashMap, but I need the data to be sorted by the value. I've been working on this for two days and I'm completely stumped! Any ...

java - How to display a stitch of TIFF images, allowing zooming and moving

I want to write a simple Java application to display a portion of large stitched tiles of TIFF images. I want to be able to zoom and move around. Is using JAI the best current way to go? The documentation seems awfully out-of-date. I found this

java - Allowing only two thread to operate on a function

I have an unusual problem. I have a function, operation in this function can be done by two threads at a time. static int iCount = 1; public synchronized void myFunct(){ while(iCount >= 3) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } iCount++; //Do Stuffs //After operation decrement count iCount --; notifyAll(); } What i am try...

java - Allowing parent to process mouse event if event was not consumed by child

I have this frame: Here is what happens: When I'm over the Pinkish panel, the scroll pane works just fine. When I put the mouse over the Darker gray JTextArea the scroll pane does not get the event. In general my question is how can I make sure the parent of a compo...

java - Whats the benefit of allowing multiple null keys in hashmap?

Just for experimenting, I added multiple null keys in a Hashmap instance. And it didn't complain. What's the benefit of doing that? The code is, Map hmap = new HashMap(); hmap.put("sushil","sushil11" ); hmap.put(null,null); hmap.put(null,"king"); hmap.put(null,"nagasaki"); hmap.put(null,null); How many keys are there in the map?

java - Allowing audio files in Spring MVC 3.0?

I'm using Spring MVC 3.0 where I specify the following mvc:resources tag to allow the static resources through:- <mvc:resources location="/resources/" mapping="/resources/**" /> Somehow, I'm having problem getting my audio files to work. I decided to place an image file in the same location just to test the path and that works fine. http://server/context/...

java - Format a Date, allowing null

I'm trying to print a Date, just the way DateFormat.getDateTimeInstance() does it. format throws a NullPointerException when passing null, so I've been wondering if there is a different approach that would return null

java - Allowing the user to upload a file

I'm working on a university assignment where we're building a website that allows students to see available courses, enlist in them, show them their timetable, etc. When the user sees his timetable, it is produced in XML and we're using XSLT to view it (as in a previous question I asked). There are currently two different XSLT files ...

java - Another method not allowing me to access HashMap values

I have implemented a hashmap in a method (call it method a) and in that method a I have called another method (call it method b) from which I transfer the hashmap built in method a to method b. Problem is that when I try to get the values of hmap in method b, it didn't allow me to write the statement for that. In class analyzer I have 2 methods method a and method b. I have called a method b from the statement belo...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)