• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to add a red X button/icon that appears in JTable, when an end date column needs to be changed.  RSS feed

 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any one please guide me with task

I want a red X button/ Icon which will appear in the last column of the JTable only when that row has a value in the END Date column greater than system Date.
The Button should appear only in that row and when clicked on the button, the value in the END Date column for that row changes to system Date.


The JTable gets loaded from the DB, except the last column gets its button when that row needs a change to the End Date column value.

Do I have to create the columns which do not need an edit as JLabels and populate them
and the END Date column should be a JTextField, has it might or may not need an edit to its value.

And the last column should be a JButton Panel.

Can some one explain the steps I should be taking to do this task. Thank You.

 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will a good head start for a swing beginner.
As we code it we get confident.

 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JTable gets loaded from the DB, except the last column gets its button when that row needs a change to the End Date column value.


So you need a separate column for the Icon.

So, as you add each row to the TableModel you will need to add an Icon when the above condition is true otherwise leave the value as null.

Then you need to create a custom renderer and editor that looks like a button for those cells that contain the Icon

For this you can use the Table Button Column. It shows how you can use a button as a renderer/editor for a column. You will need to write your custom Action that set the data when the button is clicked.

Finally, you need to override the isCellEditable(...) method of the TableModel to return true is the column contains a Icon or false otherwise.

 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sir for your kind reply.

I need some more understanding.

This is what I understood from what you said.

I should place an Red X icon in the cell of the record which needs a date change.
And that Icon should act as a button.

Am I right here.
 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you need a separate column for the Icon.
So, as you add each row to the TableModel you will need to add an Icon when the above condition is true otherwise leave the value as null.
Then you need to create a custom renderer and editor that looks like a button for those cells that contain the Icon


Sir, based on the above lines, I wrote the code that puts a button with Red X icon in the last column only if that row contains an endDate value greater than system Date.
I am using MVC framework,
I created a Custom Table Model, which gets the header and data from another Class.


This below class gets the values into the Table



Now when I click the button witht he red X icon on it,
it is calling the addActionListener method and printing the Sysout message System.out.println("Withing FlagRestrictEditor-- editing the DB table");
Below this Sysout line, I have called a method to update the database table, and it is crashing there.
model.updateToMessageTable();

I have tried calling the method which updates the database table , in the getCellEditorValue method. But it fails. How do I update the DB table when the button gets clicked.Please guide me.
//This method is the one that does the actual triggering.
public Object getCellEditorValue() { ..}


 
rekha sen
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
The JTable gets loaded from the DB, except the last column gets its button when that row needs a change to the End Date column value.


So you need a separate column for the Icon.

So, as you add each row to the TableModel you will need to add an Icon when the above condition is true otherwise leave the value as null.

Then you need to create a custom renderer and editor that looks like a button for those cells that contain the Icon

For this you can use the Table Button Column. It shows how you can use a button as a renderer/editor for a column. You will need to write your custom Action that set the data when the button is clicked.

Finally, you need to override the isCellEditable(...) method of the TableModel to return true is the column contains a Icon or false otherwise.



Sir, Please see my post and help me with this.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!