Reputation: 27
I have a little problem about filtering a JTable.
Updating, Adding and Filtering stocks are working fine. When I add stocks, of course it will display on the table. Same with filtering, when I search any letter or word it will display or not depending if the searched product is available.
The problem is like this, when I tried to delete a specific product, it won't delete and an error will be shown as stated below. The error is about the changing of cell colors.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
at java.util.Vector.elementAt(Vector.java:474)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
at javax.swing.JTable.getValueAt(JTable.java:2719)
at javax.swing.JTable.prepareRenderer(JTable.java:5720)
at VapeShop.Inventory$1.prepareRenderer(Inventory.java:55)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2114)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2016)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1812)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161)
at javax.swing.JComponent.paintComponent(JComponent.java:777)
at javax.swing.JComponent.paint(JComponent.java:1053)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1532)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1455)
at javax.swing.RepaintManager.paint(RepaintManager.java:1252)
at javax.swing.JComponent._paintImmediately(JComponent.java:5165)
at javax.swing.JComponent.paintImmediately(JComponent.java:4976)
at javax.swing.RepaintManager$3.run(RepaintManager.java:811)
at javax.swing.RepaintManager$3.run(RepaintManager.java:794)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:794)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1680)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:744)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:714)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
I tried removing the code about changing the cell colors but there still an error without stating what line it was. So I don't know what to do about it.
The error code is shown below:
Inventory.java:55 is the "Component c = super.prepareRenderer(r, data, columns);"
itemTable = new JTable(model){
public boolean isCellEditable(int data, int columns){
return false; //Cell Uneditable
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns){ //Changing of Cell Colors
Component c = super.prepareRenderer(r, data, columns);
if(data % 2 == 0)
c.setBackground(Color.WHITE);
else
c.setBackground(Color.LIGHT_GRAY);
if(isCellSelected(data, columns))
c.setBackground(Color.GREEN);
return c;
}
};
This is the code of filtering my table:
else if(btnSource==btnSearch){
String getSearch = txtSearch.getText();
if(rowCount==0){
JOptionPane.showMessageDialog(null,"Inventory is Empty.","Note!",JOptionPane.ERROR_MESSAGE);
}
else{
sorter.setRowFilter (RowFilter.regexFilter (getSearch));
sorter.setSortKeys (null);
}
}
That's just my problem. I'm not sure if the problem is about filtering or deleting my table because if I remove the code about filtering, the program works fine but when I put it back again, error will be prompted.
Here is the full code:
package VapeShop;
import static VapeShop.Style.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
public class Inventory extends JFrame{
static JPanel inventoryview;
JLabel bg, lblInventory;
JTextField txtInventory, txtSearch;
static JTable itemTable;
JButton btnSearch, btnAdd, btnBuy, btnDelete,
btnUpdate, btnLogout;
JScrollPane scrollPane;
final TableRowSorter sorter;
static Object[][] data = new Object[0][6];
static String[] columns = new String[6];
public Inventory(){
bg = new JLabel(new ImageIcon("C:\\Users\\DPdieciocho\\Documents\\NetBeansProjects\\Database 2\\src\\Images\\bg.png"));
add(bg);
inventoryview = new JPanel();
inventoryview.setLayout(null);
inventoryview.setOpaque(true);
inventoryview.setBorder(border);
inventoryview.setBackground(Color.DARK_GRAY);
inventoryview.setBounds(128, 85, 750, 400);
bg.add(inventoryview);
lblInventory = new JLabel("Inventory");
lblInventory.setFont(font5);
lblInventory.setForeground(Color.white);
lblInventory.setBounds(270, -2, 250, 45);
inventoryview.add(lblInventory);
txtInventory = new JTextField();
txtInventory.setBounds(-1, -1, 750, 50);
txtInventory.setEditable(false);
txtInventory.setOpaque(false);
txtInventory.setBorder(border);
inventoryview.add(txtInventory);
columns = new String[] {"No. of Stock", "Product Code", "Item", "Category", "Type", "Class", "Price (PHP)"};
DefaultTableModel model = new DefaultTableModel (data, columns);
itemTable = new JTable(model){
public boolean isCellEditable(int data, int columns){
return false; //Cell Uneditable
}
public Component prepareRenderer(TableCellRenderer r, int data, int columns){ //Changing of Cell Colors
Component c = super.prepareRenderer(r, data, columns);
if(data % 2 == 0)
c.setBackground(Color.WHITE);
else
c.setBackground(Color.LIGHT_GRAY);
if(isCellSelected(data, columns))
c.setBackground(Color.GREEN);
return c;
}
};
sorter = new TableRowSorter (model);
itemTable.setRowSorter (sorter);
itemTable.getTableHeader().setReorderingAllowed(false); //Undraggable Columns
itemTable.setSelectionMode (ListSelectionModel.SINGLE_SELECTION); //Selecting a Single Row Only
scrollPane = new JScrollPane(itemTable);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setBounds(2, 80, 747, 320);
inventoryview.add(scrollPane);
buttons();
String e_smoke = "Images/smoke.png";
java.net.URL imgURL = getClass().getClassLoader().getResource(e_smoke);
ImageIcon smoke = new ImageIcon(imgURL);
JLabel lblImage = new JLabel(smoke);
lblImage.setBounds(0, 0, 1280, 500);
bg.add(lblImage);
ActionListener listener = new ButtonListener();
btnBuy.addActionListener(listener);
btnUpdate.addActionListener(listener);
btnSearch.addActionListener(listener);
btnAdd.addActionListener(listener);
btnDelete.addActionListener(listener);
btnLogout.addActionListener(listener);
}
public void buttons(){
String search = "Images/search.png";
java.net.URL imgURL = getClass().getClassLoader().getResource(search);
ImageIcon searchthis = new ImageIcon(imgURL);
txtSearch = new JTextField();
txtSearch.setFont(font6);
txtSearch.setBorder(border1);
txtSearch.setHorizontalAlignment(JTextField.CENTER);
txtSearch.setForeground(Color.BLACK);
txtSearch.setBounds(485, 55, 175, 20);
inventoryview.add(txtSearch);
btnSearch = new JButton(searchthis){
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setForeground(Color.BLACK);
tip.setBackground(Color.WHITE);
tip.setFont(font4);
return tip;
}
};
btnSearch.setBounds(665, 53, 22, 22);
btnSearch.setFocusable(false);
btnSearch.setBackground(Color.gray);
btnSearch.setBorderPainted(false);
btnSearch.setToolTipText("Search Item");
btnSearch.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnSearch.setOpaque(false);
inventoryview.add(btnSearch);
String add = "Images/add.png";
java.net.URL imgURL1 = getClass().getClassLoader().getResource(add);
ImageIcon addthis = new ImageIcon(imgURL1);
btnAdd = new JButton(addthis){
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setForeground(Color.BLACK);
tip.setBackground(Color.WHITE);
tip.setFont(font4);
return tip;
}
};
btnAdd.setBounds(693, 53, 22, 22);
btnAdd.setFocusable(false);
btnAdd.setBackground(Color.gray);
btnAdd.setBorderPainted(false);
btnAdd.setToolTipText("Add new Item");
btnAdd.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnAdd.setOpaque(false);
inventoryview.add(btnAdd);
String delete = "Images/delete.png";
java.net.URL imgURL3 = getClass().getClassLoader().getResource(delete);
ImageIcon deletethis = new ImageIcon(imgURL3);
btnDelete = new JButton(deletethis){
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setForeground(Color.BLACK);
tip.setBackground(Color.WHITE);
tip.setFont(font4);
return tip;
}
};
btnDelete.setBounds(720, 53, 22, 22);
btnDelete.setFocusable(false);
btnDelete.setBackground(Color.gray);
btnDelete.setBorderPainted(false);
btnDelete.setToolTipText("Delete Item");
btnDelete.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnDelete.setOpaque(false);
inventoryview.add(btnDelete);
String buy = "Images/buy.png";
java.net.URL imgURL2 = getClass().getClassLoader().getResource(buy);
ImageIcon buythis = new ImageIcon(imgURL2);
btnBuy = new JButton(buythis){
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setForeground(Color.BLACK);
tip.setBackground(Color.WHITE);
tip.setFont(font4);
return tip;
}
};
btnBuy.setBounds(10, 53, 22, 22);
btnBuy.setFocusable(false);
btnBuy.setBackground(Color.gray);
btnBuy.setBorderPainted(false);
btnBuy.setToolTipText("Buy new Item");
btnBuy.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnBuy.setOpaque(false);
inventoryview.add(btnBuy);
String update = "Images/update.png";
java.net.URL imgURL4 = getClass().getClassLoader().getResource(update);
ImageIcon updatethis = new ImageIcon(imgURL4);
btnUpdate = new JButton(updatethis){
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setForeground(Color.BLACK);
tip.setBackground(Color.WHITE);
tip.setFont(font4);
return tip;
}
};
btnUpdate.setBounds(38, 53, 22, 22);
btnUpdate.setFocusable(false);
btnUpdate.setBackground(Color.gray);
btnUpdate.setBorderPainted(false);
btnUpdate.setToolTipText("Update Quantity");
btnUpdate.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnUpdate.setOpaque(false);
inventoryview.add(btnUpdate);
String out = "Images/logout.png";
java.net.URL imgURL5 = getClass().getClassLoader().getResource(out);
ImageIcon logout = new ImageIcon(imgURL5);
btnLogout = new JButton(logout){
public JToolTip createToolTip() {
JToolTip tip = super.createToolTip();
tip.setForeground(Color.BLACK);
tip.setBackground(Color.WHITE);
tip.setFont(font4);
return tip;
}
};
btnLogout.setBounds(965, 500, 18, 22);
btnLogout.setBackground(Color.gray);
btnLogout.setBorderPainted(false);
btnLogout.setToolTipText("Log out");
btnLogout.setCursor(new Cursor(Cursor.HAND_CURSOR));
btnLogout.setOpaque(false);
bg.add(btnLogout);
}
private class ButtonListener implements ActionListener{
public void actionPerformed(ActionEvent e){
Object btnSource = e.getSource();
int rowCount = itemTable.getRowCount();
int rowCheck = itemTable.getSelectedRow();
if(btnSource==btnBuy){
dispose();
new BuyItem().run();
}
else if(btnSource==btnUpdate){
if(rowCount==0){
JOptionPane.showMessageDialog(null,"Inventory is Empty.","Note!",JOptionPane.ERROR_MESSAGE);
}
else if(rowCheck<0){
JOptionPane.showMessageDialog(null,"Select the item to be updated.","Note!",JOptionPane.ERROR_MESSAGE);
}
else{
new UpdateItem().run();
}
}
else if(btnSource==btnSearch){
String getSearch = txtSearch.getText();
if(rowCount==0){
JOptionPane.showMessageDialog(null,"Inventory is Empty.","Note!",JOptionPane.ERROR_MESSAGE);
}
else{
sorter.setRowFilter (RowFilter.regexFilter (getSearch));
sorter.setSortKeys (null);
}
}
else if(btnSource==btnAdd){
dispose();
new AddItem().run();
}
else if(btnSource==btnDelete){
if(rowCount==0){
JOptionPane.showMessageDialog(null,"Inventory is Empty.","Note!",JOptionPane.ERROR_MESSAGE);
}
else if(rowCheck<0){
JOptionPane.showMessageDialog(null,"Select the item to be deleted.","Note!",JOptionPane.ERROR_MESSAGE);
}
else{
Object[][] temp = new Object[data.length-1][7];
for(int i=0; i<rowCheck; i++){
temp[i][0] = data[i][0];
temp[i][1] = data[i][1];
temp[i][2] = data[i][2];
temp[i][3] = data[i][3];
temp[i][4] = data[i][4];
temp[i][5] = data[i][5];
temp[i][6] = data[i][6];
}
for(int i=rowCheck+1; i<data.length; i++){
temp[i-1][0] = data[i][0];
temp[i-1][1] = data[i][1];
temp[i-1][2] = data[i][2];
temp[i-1][3] = data[i][3];
temp[i-1][4] = data[i][4];
temp[i-1][5] = data[i][5];
temp[i-1][6] = data[i][6];
}
data=temp;
itemTable.setModel(new DefaultTableModel(data, columns));
}
}
else if(btnSource==btnLogout){
int selected = JOptionPane.showConfirmDialog(null,"Are you sure you want to Sign out?","Confirm Sign Out",JOptionPane.YES_NO_OPTION);
if(selected == JOptionPane.YES_OPTION){
dispose();
new Admin().run();
}
else if(selected ==JOptionPane.NO_OPTION){
}
}
}
}
public void run(){
setSize(1000, 562);
setResizable(false);
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation (EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Inventory().run();
}
}
I don't know how to fix this because this is my first time in using JTable.
Upvotes: 1
Views: 258
Reputation: 324207
Your delete method seems way too complicated. All you need to do use is use the removeRow(..)
method of the DefaultTableModel
.
The code would be something like:
//int index = table.getSelectedIndex();
int index = table.getSelectedRow();
DefaultTableModel model = (DefaultTableModel)table.getModel();
model.removeRow( table.convertRowIndexToModel( index );
If this doesn't help than post a proper SSCCE based on the tutorial example, not your current code.
Upvotes: 1