The JButton addItem is not responding to being clicked. I have tried addItem.setEnabled(true); which did not work.
Here is my class:
package com.duckblade.inventory.teller;
import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
public class DatabaseFrame extends JFrame {
DatabaseFrame f = this;
Object[][] data;
public DatabaseFrame(String ip, final String name){
super(name + "@" + ip);
try {
data = Main.dbc.getAll();
JTable table = new JTable(new AbstractTableModel(){
String[] names = {"Item ID", "Name", "Quantity", "Profit"};
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return names[col];
}
public int getColumnCount() {
return names.length;
}
public boolean isCellEditable(int row, int col) {
return col!=0 && col!=3;
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public void setValueAt(Object value, int row, int col){
try{
Main.dbc.updateItem(Integer.parseInt(getValueAt(row, 0).toString()), getValueAt(row, 1).toString(), Integer.parseInt(getValueAt(row, 2).toString()));
}catch (Exception e){
JOptionPane.showMessageDialog(f, "An error occurred: " + e.getMessage());
}
}
});
add(table.getTableHeader(), BorderLayout.PAGE_START);
JScrollPane pane = new JScrollPane(table);
table.setFillsViewportHeight(true);
add(pane, BorderLayout.CENTER);
JButton addItem = new JButton("Add Item");
addItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
int itemId = Integer.parseInt(JOptionPane.showInputDialog("Please enter an item id: "));
String itemName = JOptionPane.showInputDialog("Please enter the item name: ");
int itemPrice = Integer.parseInt(JOptionPane.showInputDialog("Please enter an item price: "));
int itemQuantity = Integer.parseInt(JOptionPane.showInputDialog("Please enter the quantity owned: "));
Main.dbc.insertItem(itemId, itemName, itemPrice, itemQuantity);
data = Main.dbc.getAll();
} catch (Exception ex) {
JOptionPane.showMessageDialog(f, "An error occurred: " + ex.getMessage());
}
}
});
add(addItem, BorderLayout.SOUTH);
} catch (SQLException e) {
JOptionPane.showMessageDialog(Main.f, "Could not retrieve data from database: " + e.getMessage());
return;
}
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(400, 400);
setVisible(true);
}
}
Aucun commentaire:
Enregistrer un commentaire