Disable particular row in JTable using data from database - java

I was wondering if it's possible to grey out or render some rows in my JTable to be invisible using the data from my data base?
These are the columns in my JTable:
public DutyModel(ArrayList<Duty> listOfObjects) {
rowCount = listOfObjects.size();
colCount = columnNames.length;
data = new Object[rowCount][colCount];
for (int i = 0; i < rowCount; i++) {
/* Copy an ArrayList element to an instance of MyObject */
Duty d1 = (Duty) (listOfObjects.get(i));
data[i][0] = d1.getDutyId();
data[i][1] = d1.getDutyName();
data[i][2] = d1.getDutyDesc();
data[i][3] = d1.getDutySectorName();
data[i][4] = d1.getDutyStatus();
}
}
When my table is displayed, I want the rows with d1.getDutyStatus() where dutyStatus = "Inactive" to be greyed out. My table can currently be updated to either "Active" (By pressing an update button), or "Inactive" (By pressing a remove button). I don't want the user to be able to edit any of the duties that have the dutyStatus of "Inactive". Any assistance or help is greatly appreciated. Thanks!
The below code is what happens when the user selects a row in the JTable and presses the "Update" and "Delete" button. Also, I'm trying to use a 3 tier architecture to run my codes, my controller class is RetrieveDutyControl and my entity is Duty.
public void actionPerformed(ActionEvent e) {
int rowSelected = tblDutyList.getSelectedRow();
if (rowSelected >= 0) {
String idDuty = tblDutyList.getValueAt(rowSelected, 0)
.toString();
String name = tblDutyList.getValueAt(rowSelected, 1)
.toString();
String desc = tblDutyList.getValueAt(rowSelected, 2)
.toString();
String sector = tblDutyList.getValueAt(rowSelected, 3)
.toString();
String status = tblDutyList.getValueAt(rowSelected, 4)
.toString();
Duty duty = new Duty(Integer.parseInt(idDuty), name, desc,
sector, status);
RetrieveDutyControl rdc = new RetrieveDutyControl();
ArrayList<Duty> dutyList = rdc.processRetrieveDuties(duty);
JPanel contentPane = new RemoveDutyForm(myFrame, duty);
myFrame.getContentPane().removeAll();
myFrame.setContentPane(contentPane);
myFrame.setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "No Record Selected",
"Alert", JOptionPane.ERROR_MESSAGE);
}

Try with overridden prepareRenderer() method to gray out the row based on data
Sample code: (use first column's value to validate)
Object[] columnNames = { "A", "B", "C", "D" };
Object[][] data = {
{ "abc", new Double(850.503), 53, true },
{ "lmn", new Double(36.23254), 6, false },
{ "pqr", new Double(8.3), 7, false },
{ "xyz", new Double(246.0943), 23, true } };
JTable table = new JTable(data, columnNames) {
#Override
public java.awt.Component prepareRenderer(TableCellRenderer renderer, int row, int col) {
java.awt.Component comp = super.prepareRenderer(renderer, row, col);
Object value = getModel().getValueAt(row, 0);
if (value.equals("lmn")) {
comp.setBackground(Color.lightGray);
} else {
comp.setBackground(Color.white);
}
return comp;
}
};
For buttons check for value and do nothing.
How to get the value of the selected row?
if(table.getSelectedRow()!=-1){
int rowIndex=table.getSelectedRow();
Object value=table.getModel().getValueAt(rowIndex, 0);
if(value.equals("lmn")){
//do nothing
}else{
// perform desired operation
}
}

Related

Getting old values from Jtable

I am facing a strange problem with the Jtable. I have a small standalone java application in which I have used Jtable that shows data that is retrieved from database. User can view and edit the data. Once he edit the cell and presses the update button then the edited values will be updated in the database. Now what happening is for the first time when user edit the data and click on update button then the values are properly persisted in the database and user gets out of the module. But again if he/she goes to the same module and edits some more cells and click on update button then in back end i get old value from those cells although the Jtable shows the latest updated values in the cells.
Below is the code.
private JTable jt;
private TableRowSorter<TableModel> tableSorter;
private Dao dao=new Dao();
updateDetails.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
boolean isSuccessful = true;
int rowCount = jt.getRowCount();
List<EmployeeMaster> empMasterList = new ArrayList<>();
int j = 0;
for (int i = 0; i < rowCount; i++) {
EmployeeMaster master = new EmployeeMaster();
j = 0;
try {
master.setEmpId(Long.parseLong((String) jt.getValueAt(i, j)));
j++;
compId.setName(Long.parseLong((String) jt.getValueAt(i, j)));
j++;
compId.setNumber((String) jt.getValueAt(i, j));
j++;
empMasterList.add(master);
}
dao.updateAllEmpDetails(empMasterList);
homePanel.setVisible(true);
UpdateEmp.setVisible(false);
}
}
});
getDataFromDBtoJtable.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
List<EmployeeMaster> empMasterList = dao.getAllEmpDetails();
String data[][] = new String[empMasterList.size()][3];
int i = 0;
int j = 0;
for (EmployeeMaster employeeMaster : empMasterList) {
j = 0;
data[i][j] = String.valueOf(employeeMaster.getEmpId());
j++;
data[i][j] = String.valueOf(employeeMaster.getName());
j++;
data[i][j] = String.valueOf(employeeMaster.getNumber());
i++;
}
String column[] = { "ID", "Name", "Number"};
jt = new JTable(data, column) {
public boolean isCellEditable(int row, int column) {
if (column == 0 || column == 1 || column == 2)
return false;
else
return true;
};
};
tableSorter = new TableRowSorter<TableModel>(jt.getModel());
jt.setBounds(12, 12, 1200, 400);
jt.setRowSorter(tableSorter);
homePanel.setVisible(false);
UpdateEmp.setVisible(true);
JScrollPane pane = new JScrollPane(jt);
pane.setBounds(1, 40, 1150, 300);
setBounds(0, 0, 2000, 800);
UpdateEmp.add(pane);
}
});
updateDetails -> button to get the data from jtable and update it in database
getDataFromDBtoJtable -> button to get data from Database and show it in Jtable
Whenever user hits the updateDetails i am updating the database and forcing user to get out of the module. So everytime whenever user get into jtable module he/she gets new Jtable instance. But still unable to identify what's going wrong.

Change background color of JTable row based on column value issue

Based on this question I did this:
public class ShowTicketPlanning extends javax.swing.JFrame {
private static String prevTicket="";
private static int color=1;
public ShowTicketPlanning() {
initComponents();
for (int i = 0; i < C.GameList.size(); i++) {
gameSelectGameCB.addItem(C.GameList.get(i).printForList());
}
}
private void gameSelectGameCBActionPerformed(java.awt.event.ActionEvent evt) {
int ind = gameSelectGameCB.getSelectedIndex();
if (C.GameList.size() > 0) {
DefaultTableModel tmodel = (DefaultTableModel) dbgPlanTBL.getModel();
while (tmodel.getRowCount() > 0) {
tmodel.removeRow(0);
}
String columnNames[] = new String[C.DBGList.get(ind).getDrawDayList().size()];
int gameDrawSize = C.DBGList.get(ind).getDrawDayList().size();
for (int z = 0; z < gameDrawSize; z++) {
columnNames[z] = C.DBGList.get(ind).getDrawDayList().get(z).getDrawNumber() + ":" + C.DBGList.get(ind).getDrawDayList().get(z).getCdc() + ":" + ES.dayToShortDay(C.DBGList.get(ind).getDrawDayList().get(z).getDay());
}
String dataValues[][] = {};
DefaultTableModel model = new DefaultTableModel(dataValues, columnNames);
dbgPlanTBL.setModel(model);
for (int t = 0; t < C.TicketList.size(); t++) {//for all tickets
for (int b = 0; b < C.TicketList.get(t).getBoardList().size(); b++) {
Object[] ob = new Object[gameDrawSize];
for (int brpd = 0; brpd < C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().size(); brpd++) {
int d = C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getDrawIndex();
if (C.DBGList.get(ind).isHasRaffle()) {
ob[d] = C.TicketList.get(t).getTicketRefName() + ":" + b
+ ":" + C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getNumberResult()
+ ":" + C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getRaffleResult();
} else {
ob[d] = C.TicketList.get(t).getTicketRefName() + ":" + b
+ ":" + C.TicketList.get(t).getBoardList().get(b).getBoardResultPerDrawList().get(brpd).getNumberResult();
}
}
model.addRow(ob);
}
}
DefaultTableModel amodel = (DefaultTableModel) dbgPlanTBL.getModel();
int tableSize = dbgPlanTBL.getRowCount();
//System.out.println("rows:"+tableSize);
int tableCol = dbgPlanTBL.getColumnCount();
//System.out.println("cols:"+tableCol);
for (int r = 0; r < tableSize; r++) {
for (int col = 0; col < tableCol; col++) {
if (amodel.getValueAt(r, col) == null) {
} else {
dbgPlanTBL.setDefaultRenderer(Object.class, new ShowTicketPlanning.CustomRenderer());
dbgPlanTBL.getCellEditor(r, col).getTableCellEditorComponent(dbgPlanTBL, dbgPlanTBL.getValueAt(r, col), true, r, col).setForeground(Color.red);
}
}
}
//define column width..............
TableColumnModel tcm = dbgPlanTBL.getColumnModel();
for (int i = 0; i < (tcm.getColumnCount()); i++) {
tcm.getColumn(i).setPreferredWidth(120);
}
dbgPlanTBL.setDefaultRenderer(Object.class, new ShowTicketPlanning.CustomRenderer());
}
}
class CustomRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object value,boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (value == null) {
setBackground(new java.awt.Color(255, 255, 255));
} else {
StringTokenizer st2 = new StringTokenizer(value.toString(), ":");
String ref = st2.nextElement().toString();
if (prevTicket.equals(ref)) {
if (color == 1) {
setBackground(new java.awt.Color(204, 204, 204));
} else {
setBackground(new java.awt.Color(255, 255, 255));
}
} else {
if (color == 1) {
color = 2;
} else {
color = 1;
}
if (color == 1) {
setBackground(new java.awt.Color(204, 204, 204));
} else {
setBackground(new java.awt.Color(255, 255, 255));
}
}
prevTicket=ref;
}
return c;
}
}
So I have tickets that each ticket has more than one board. And I want to change color per ticket.
Result:
The colors are correct, however after scrolling the colors become irregular as the following image.
Note: Each "ticket0, ticket1, ticket2" etc should have alternating colors.
Feel free to check out how this snippet solves this problem and apply it in your own program.
I'll have to disclaim this by saying that I'm not sure that this is the best way to do this, but it works in the sense that it doesn't have the odd behavior that you are describing (row coloring getting messed up when scrolling). I also added some code to account for sorting events and inserting/deleting/updating rows.
My snippet keeps track of colors in an array, so once a color is established for a row it doesn't establish it again. The renderer just gets the color from the color store.
For explanation of what the snippet does, see the comments inside.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableWithAlternatingColorsExample {
#SuppressWarnings("serial")
private static class AlternatingColorOnKeyRenderer extends DefaultTableCellRenderer {
/* The alternating colors */
public static final Color color1 = new Color(204,204,204);
public static final Color color2 = new Color(255,255,255);
/** The model index of the key column, ie the model column from which the key is derived. */
public static final int modelKeyColId = 0;
/** The color store that stores the colors for each row in the view.
* Indexed with view index to retrieve color for a row in the view. */
private ArrayList<Color> colorStore = new ArrayList<>( );
/** Reset all colors in the color store. */
public void resetAllColors() {
colorStore = new ArrayList<>(Math.max(10,colorStore.size()));
}
/** Reset all colors in the color store from a certain view index on. */
public void resetColorsFrom(int row) {
if(row==0)
resetAllColors();
else {
ArrayList<Color> retainedColors = new ArrayList<>(Math.max(10,colorStore.size()));
retainedColors.addAll(colorStore.subList(0,Math.min(row-1,colorStore.size()-1))); // copy retained colors
colorStore = retainedColors;
}
}
/** Determines the key value from a cell value. */
private Object getCellKeyVal(Object cellValue) {
return new StringTokenizer(cellValue.toString(),":").nextElement();
}
/** Retrieves the key value for a certain row. */
private Object getRowKeyVal(JTable t,int viewRowId) {
int modelRowId = t.convertRowIndexToModel(viewRowId); // convert row view index to model index to index the model
Object cellValue = t.getModel().getValueAt(modelRowId,modelKeyColId); // get cell value from model, because the column might not be present in the view
return getCellKeyVal(cellValue);
}
/** Prepares the color store for a certain row in the view.
* After calling this method colors for the range [0,row] will be present in the color store.
* Call this method to ensure that <i>colorStore.get(row)</i> will return a color. */
private void prepareColorStore(JTable t,int row) {
if(colorStore.size()>row) return; // color already present for row
if(colorStore.isEmpty()) colorStore.add(color1); // if color store empty, start with the first color
if(row==0) return;
Object curKeyVal = getRowKeyVal(t,colorStore.size()-1);
while(colorStore.size()<=row) { // add colors until color present for row
Object prevKeyVal = curKeyVal;
curKeyVal = getRowKeyVal(t,colorStore.size());
if(prevKeyVal.equals(curKeyVal)) // compare key values between current and previous row
colorStore.add(colorStore.get(colorStore.size()-1)); // add previous color in store when key values are equal
else if(colorStore.get(colorStore.size()-1)==color1) // reference compare because color instances are fixed
colorStore.add(color2); // add alternating color when key values differ
else
colorStore.add(color1);
}
}
#Override
public Component getTableCellRendererComponent(JTable table,Object value,boolean isSelected,boolean hasFocus,int row,int column) {
Component c = super.getTableCellRendererComponent(table,value,isSelected,hasFocus,row,column);
if(!table.isRowSelected(row)) {
prepareColorStore(table,row); // ensure color is present for row
c.setBackground(colorStore.get(row)); // set color from color store
}
return c;
}
};
private static JTable createTable() {
Vector<Vector<Object>> rowData = new Vector<>( Arrays.asList(
new Vector<>(Arrays.asList(new Object[]{"ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::","ticket0:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::","ticket25:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::","ticket0:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::","ticket1:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::","ticket1:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::","ticket10:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::","ticket10:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::","ticket11:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::","ticket11:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::","ticket12:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::","ticket12:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::","ticket13:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::","ticket13:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::","ticket14:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::","ticket17:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::","ticket14:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::","ticket15:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::","ticket15:1::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::","ticket16:0::"})),
new Vector<>(Arrays.asList(new Object[]{"ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::","ticket16:1::"}))
));
Vector<Object> colData = new Vector<>(Arrays.asList(new Object[]{"1:103:Wed","2:106:Sat","3:110:Wed","4:110:Sat","5:117:Wed","6:120:Sat","7:124:Wed","8:127:Sat"}));
final JTable t = new JTable(rowData,colData);
DefaultTableCellRenderer tcr = new AlternatingColorOnKeyRenderer();
Enumeration<TableColumn> e = t.getColumnModel().getColumns();
while(e.hasMoreElements())
e.nextElement().setCellRenderer(tcr); // all table columns share the same AlternatingColorOnKeyRenderer instance
t.setAutoCreateRowSorter(true);
/* When the row sorter changes, reset all colors in the color store.
* When row sorting changes, the row view indices will point to different rows in the model, so the color store needs to be reset. */
t.getRowSorter().addRowSorterListener(new RowSorterListener() {
#Override
public void sorterChanged(RowSorterEvent e) {
((AlternatingColorOnKeyRenderer) t.getColumnModel().getColumn(0).getCellRenderer()).resetAllColors(); // all rows share the same AlternatingColorOnKeyRenderer instance, so pick the renderer from the first column and reset color store
}
});
/* Add a model listener to listen for inserted/deleted/updated rows. When rows are inserted/deleted/updated, the row colors could change for subsequent rows.
* Determines the minimum view index for the inserted/deleted/updated rows, then resets the color store from that view index on. */
t.getModel().addTableModelListener(new TableModelListener() {
#Override
public void tableChanged(TableModelEvent e) {
int minViewId;
switch(e.getType()) {
case TableModelEvent.INSERT:
case TableModelEvent.DELETE:
minViewId = Integer.MAX_VALUE;
for(int r=e.getFirstRow();r<=e.getLastRow();++r) { // determine minimum view index for inserted/deleted rows
int viewId=t.convertRowIndexToView(r); // convert model index to view index
if(viewId>=0 && viewId<minViewId) // due to filtering, it is possible that inserted/deleted rows are not visible in the view (convert will return -1 in that case)
minViewId=viewId;
}
if(minViewId!=Integer.MAX_VALUE) // if any of the inserted/deleted rows were visible, reset the color store from the minimum view index on
((AlternatingColorOnKeyRenderer) t.getColumnModel().getColumn(0).getCellRenderer()).resetColorsFrom(minViewId);
break;
case TableModelEvent.UPDATE:
int r = e.getFirstRow();
if(r==TableModelEvent.HEADER_ROW)
return; // outside scope for this example
if(e.getColumn()!=TableModelEvent.ALL_COLUMNS && e.getColumn()!=AlternatingColorOnKeyRenderer.modelKeyColId)
return; // only listen for updates in the key column
minViewId = Integer.MAX_VALUE;
for( ;r<=e.getLastRow();++r) { // determine minimum view index for updated rows
int viewId=t.convertRowIndexToView(r); // convert model index to view index
if(viewId>=0 && viewId<minViewId) // due to filtering, it is possible that updated rows are not visible in the view (convert will return -1 in that case)
minViewId=viewId;
}
if(minViewId!=Integer.MAX_VALUE) { // if any of the updated rows were visible, reset the color store from the minimum view index on & repaint
((AlternatingColorOnKeyRenderer) t.getColumnModel().getColumn(0).getCellRenderer()).resetColorsFrom(minViewId);
t.repaint(); // repaint necessary for UPDATE events to update colors in the view
}
break;
}
}
});
return t;
}
protected static JButton createAddButton(final JTable t) {
JButton button = new JButton("Add row randomly");
button.addActionListener(new ActionListener() {
#SuppressWarnings("unchecked")
#Override
public void actionPerformed(ActionEvent e) {
DefaultTableModel dtm = (DefaultTableModel) t.getModel();
Vector<Object> newRow = new Vector<>(Arrays.asList(new Object[]{"ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::","ticket77:0::"}));
int index = new Random().nextInt(t.getRowCount()+1); // random index to insert
if(index<t.getRowCount())
dtm.getDataVector().add(index,newRow); // use the dataVector from DefaultTableModel to insert
else
dtm.getDataVector().add(newRow);
dtm.fireTableRowsInserted(index,index); // notify the view that a row was inserted
}
});
return button;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JTable t = createTable();
f.add(new JScrollPane(t),BorderLayout.CENTER);
f.add(createAddButton(t),BorderLayout.SOUTH);
f.setSize(500, 500);
f.setVisible(true);
}
});
}
}

JTable select and deselect not working

I have a problem with my JTable. Firstly I selected some entries in my table. However when I deselect some of them, the table could not understand it.
Example scenario: I select job1 and 2 for the testing after that I change my mind and de-select job2. But in the result I saw job1 job1 and job2 ( job 1 seen 2 times and even though I dis-select job 2 I saw them.) Or after selected all the jobs ( choose all button) I want to deselect all of them (Clear button) when I click clear all the table seems empty. It is good but somehow the background of the program still protect the all old selection. How can I solve this?
Try:
I created the row of my table by read csv file.
public class JobSelectionListPanel extends JPanel {
private static final long serialVersionUID = 5198916547962359735L;
private static JobSelectionListPanel INSTANCE = new JobSelectionListPanel();
public static JobSelectionListPanel getInstance() {
return INSTANCE;
}
private JTable table;
private JButton next, back, btnClear, btnNewButton, btnChooseAll;
private JobList fnnJobList = new JobList();
private JobSelectionListPanel() {
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
table.setBorder(new CompoundBorder());
// Read all FNN jobs from file
try {
fnnJobList.readFromFile("rules.csv");
} catch (IOException e1) {
System.out.println("You are not able to read the rules.csv file");
}
// Create ArrayList of JobNames
Object[][] initialData = new Object[fnnJobList.size()][1];
int i = 0;
for (Job jobDes : fnnJobList) {
initialData[i][0] = (Object) jobDes.getJobname();
i++;
}
String[] columnNames = new String[] { "", "Your preferences" };
table.setModel(new DefaultTableModel(initialData, columnNames) {
private static final long serialVersionUID = 1L;
#SuppressWarnings("rawtypes")
Class[] columnTypes = new Class[] { Object.class, Boolean.class };
#SuppressWarnings({ "unchecked", "rawtypes" })
public Class getColumnClass(int columnIndex) {
return columnTypes[columnIndex];
}
});
table.getColumnModel().getColumn(1).setPreferredWidth(80);
table.getColumnModel().getColumn(1).setMinWidth(40);
table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
table.setCellSelectionEnabled(true);
I user want to choose all rows then I implemented this.
btnChooseAll = new JButton("Choose all");
btnChooseAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel chooseAllData = (DefaultTableModel) table.getModel();
if (DeviceGroups.DeviceAList.size() == 0 || DeviceGroups.DeviceBList.size() == 0
|| DeviceGroups.DeviceCList.size() == 0 || DeviceGroups.DeviceDList.size() == 0)
JOptionPane.showMessageDialog(null,
"You should choose at least 1 device for each test device to apply this test case", "Invalid OPTION",
JOptionPane.ERROR_MESSAGE);
else
for (int i = 0; i < chooseAllData.getRowCount(); i++) {
for (int j = 1; j < chooseAllData.getColumnCount(); j++) {
chooseAllData.setValueAt(true, i, j);
}
}
}
});
For clear all preferences :
btnClear = new JButton("Clear all");
// Clear button create a model of JTable and delete all the rows of table!!
btnClear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
DefaultTableModel clearTableData = (DefaultTableModel) table.getModel();
for (int i = 0; i < clearTableData.getRowCount(); i++) {
for (int j = 1; j < clearTableData.getColumnCount(); j++) {
clearTableData.setValueAt(null, i, j);
}
}
}
});
I see the following problem in your code: mixing up view indexes and model indexes. This is the offending snippet:
for (int i = 0; i < table.getRowCount(); i++) {
if (table.getValueAt(i, 1) != null) {
if (((Boolean) table.getValueAt(i, 1)).booleanValue()) {
String jobName = (((DefaultTableModel) table.getModel()).getValueAt(i, 0).toString());
You are using the i variable to denote view row indices, since you are checking values in this statement: table.getValueAt(i, 1) != null.
But then a bit further you are using i to index the model:
String jobName = ((DefaultTableModel) table.getModel()).getValueAt(i, 0).toString();
If i is to be a view index, you need to convert it to a model index before indexing the model:
String jobName = ((DefaultTableModel) table.getModel()).getValueAt(table.convertRowIndexToModel(i), 0).toString();
Also, when columns would be switched around in the view (ie on screen in your GUI), the following will probably not work as intended:
table.getValueAt(i, 1) != null
You most likely mean to say, get the second column value in the model, not the view. Best rewrite then as
table.getValueAt(i, table.convertColumnIndexToView(1)) != null

Best way to have a JTable populate with objects?

Let's say I have an array of People. These people have a bunch of fields like name, position, title, salary.
I've seen most questions be about filling a JTable with 2D arrays which, unless I'm wrong, isn't exactly what I'm trying to do.
I would like to be able to click a button and have the JTable look at the array of People and display that table.
Thanks!
Edit: I'm hoping to be able to just change the setModel arguments here to update using certain values.
JButton btnRefresh = new JButton("Refresh");
btnRefresh.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
table_1.setModel(new DefaultTableModel(new Object[][] {}, new String[] {
"#", "Song", "Artist", "Time" }));
table_1.getColumnModel().getColumn(0).setPreferredWidth(22);
table_1.getColumnModel().getColumn(1).setPreferredWidth(191);
table_1.getColumnModel().getColumn(2).setPreferredWidth(179);
panel_3.revalidate();
}
});
So I'm able to change the column headers by changing these string values but what can I change the new Object[][]{} to?
This code fragment does exactly what I needed it to do.
JButton btnRefresh = new JButton("Refresh");
btnRefresh.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
table.setModel(new DefaultTableModel(dataArray(),
new String[] { "first", "second", "third", "fourth" }));//Changes the column headers
panel.revalidate();
}
private Object[][] dataArray() {
Object[][] table = new Object[5][4];//5 = rows 4 = columns
for (int i = 0; i < 5; i++) {
for(int j = 0; j< 4; j++){
if(j == 0){
table[i][j] = i+1;
}else if(j== 1){
table[i][j] = "Second Row";//Change these strings to whatever variable you want to fill the second row with, same with the other 3.
}else if(j== 2){
table[i][j] = "Third Row";
}else{
table[i][j] = "Fourth Row";
}
}
}
return table;
}
});

Implementing a search method in JTable

I'm trying to implement a search method in JTable, the search code is:
private void searchTable()
{
String id = inputIdField.getText();
for(int row = 0; row <= listAllTable.getRowCount() - 1; row++)
{
System.out.println(row);
if(id.equals(listAllTable.getValueAt(row, 0)))
{
listAllTable.scrollRectToVisible(listAllTable.getCellRect(row, 0, true));
listAllTable.setRowSelectionInterval(row, row);
for (int i = 0; i <= listAllTable.getColumnCount() - 1; i++)
{
listAllTable.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer());
}
}
else
{
}
}
}
The method works only when I add String and Object:
final String[] columnNames = {"Name", "Surname", "Age"};
final Object[][] data = {{"Jhon", "Java", "23"}, {"Stupid", "Stupido", "500"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"},
{"Michael", "Winnie", "20"}, {"Winnie", "Thepoor", "23"}};
JTable listAllTable = new JTable(data, columnNames);
But when I fetch from DB, put it in the array and display it in the JTable the search method does not work, it get the rows perfectly, but it does not pass the if(id.equals(listAllTable.getValueAt(row, 0))) when the value exists, can somebody check the code and explain me why? Here is the code for building my JTable:
private void buildJTable(ArrayList<SalesOrder> list)
{
DefaultTableModel model = new DefaultTableModel()
{
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column)
{
return false;
}
};
model.setColumnIdentifiers(new String[] {"ID", "Customer ID", "Employee ID", "Voucher ID", "Status", "Date", "Price"});
model.setRowCount(list.size());
int row = 0;
for(SalesOrder so : list)
{
model.setValueAt(so.getId(), row, 0);
model.setValueAt(so.getCustomerId().getId(), row, 1);
model.setValueAt(so.getEmployeeId().getId(), row, 2);
model.setValueAt(so.getVoucherId().getId(), row, 3);
model.setValueAt(so.getStatus(), row, 4);
model.setValueAt(so.getDate(), row, 5);
model.setValueAt(so.getPrice(), row, 6);
row++;
}
listAllTable.setRowSelectionAllowed(true);
listAllTable.setModel(model);
}
Can somebody look at the code and explain me why it does not search my JTable when build from a method private void buildJTable(ArrayList<SalesOrder> list) ? It builds everything correclty but it does not search.
Thank you.
You are simply compare incompatible values. Try to change your search method as following
private void searchTable()
{
String id = inputIdField.getText();
for(int row = 0; row <= listAllTable.getRowCount() - 1; row++)
{
System.out.println(row);
String strVal = null == listAllTable.getValueAt(row, 0)? null : listAllTable.getValueAt(row, 0).toString();
if(id.equals(strVal))
{
listAllTable.scrollRectToVisible(listAllTable.getCellRect(row, 0, true));
listAllTable.setRowSelectionInterval(row, row);
for (int i = 0; i <= listAllTable.getColumnCount() - 1; i++)
{
listAllTable.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer());
}
}
else
{
}
}
}

Categories

Resources