我正在研究我的项目,但我有一点问题。关于过滤jtable。
更新,添加和过滤股票工作正常。当我添加股票时,它当然会显示在桌面上。与过滤相同,当我搜索任何字母或单词时,它将显示与否,具体取决于搜索到的产品是否可用。
问题是这样的,当我尝试删除特定产品时,它不会删除,并且将显示如下所述的错误。错误是关于细胞颜色的变化。
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)
我尝试删除有关更改单元格颜色的代码,但仍然存在错误,但未说明它是什么行。所以我不知道该怎么做。
错误代码如下所示:
Inventory.java:55是“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;
}
};
这是过滤我的表格的代码:
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);
}
}
这只是我的问题。我不确定问题是关于过滤还是删除我的表,因为如果我删除了关于过滤的代码,程序运行正常,但是当我再次放回时,会提示错误。
以下是完整代码:
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();
}
}
我不知道如何解决这个问题,因为这是我第一次使用jtable。所以对于那些知道如何解决这个问题的人,请帮助我。
谢谢你和Godbless。
答案 0 :(得分:1)
你的删除方法似乎太复杂了。您需要使用的只是使用removeRow(..)
的{{1}}方法。
代码如下:
DefaultTableModel
如果这比根据教程示例而不是您当前的代码发布正确的SSCCE更有帮助。