මම recently implement කරපු code එකක්
--------------------------------------------------------------------------------
To add a progress bar to a Java table cell, you can create a custom cell renderer that displays a JProgressBar component instead of a plain text or other data. Here's an example code snippet that demonstrates how to do this:
```java
Java:
public class ProgressBarCellRenderer extends JProgressBar implements TableCellRenderer {
public ProgressBarCellRenderer() {
setOpaque(true);
setMinimum(0);
setMaximum(100);
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Integer) {
int progress = (Integer) value;
setValue(progress);
setStringPainted(true);
setString(progress + "%");
}
if (isSelected) {
setBackground(table.getSelectionBackground());
} else {
setBackground(table.getBackground());
}
return this;
}
}
```
This class extends the JProgressBar class and implements the TableCellRenderer interface, which is used to customize the appearance of the cells in a JTable.
In the constructor, the progress bar is initialized with a minimum value of 0 and a maximum value of 100.
The `getTableCellRendererComponent()` method is called by the JTable to render each cell in the table. In this method, we check if the value of the cell is an integer (which is the expected value for a progress bar). If it is, we set the value of the progress bar to the integer value, and set the progress bar to display the value as a percentage with the `setStringPainted()` and `setString()` methods. If the cell is selected, we set the background color to the selection background color, otherwise, we set it to the default background color.
Once you have created this custom cell renderer, you can use it to display progress bars in specific cells of your JTable by setting the cell renderer for that column using the `setCellRenderer()` method. For example, to set the progress bar cell renderer for the 5th column of your JTable, you can use this code:
```java
Java:
JTable table = new JTable();
table.getColumnModel().getColumn(4).setCellRenderer(new ProgressBarCellRenderer());
```
This code sets the cell renderer for the 5th column of the table to be an instance of the `ProgressBarCellRenderer` class, which will display progress bars in the cells of that column.