我的Java应用程序有点问题。我想显示左边Jlist中餐馆的订单,以及右边Jlist中所选订单的详细信息。订单通过MySQL查询读入,然后打印到Jlist中。但是,我们想要创建一个刷新按钮,查询必须再次执行,然后打印到Jlist。但是,我无法使刷新按钮工作。它会刷新内容,但会打开一个新的Jfram窗口......
来自GUI类的:
public class Barscherm extends javax.swing.JFrame {
private BedieningsManager manager;
/**
* Creates new form Barscherm
*/
public Barscherm() {
manager = new BedieningsManager();
initComponents();
}
private void opvraagKnopActionPerformed(java.awt.event.ActionEvent evt) { // This is the refresh button
jPanel1.removeAll();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Barscherm().setVisible(true);
}
});
}
来自经理班级:
public class BedieningsManager {
private ArrayList<Barbestelling> bestellingen;
private int tafelNummer,bestelNummer, aantal;
Database db1 = new Database();
public BedieningsManager(){
this.bestellingen = new ArrayList<Barbestelling>();
maakBestellingen();
public void maakBestellingen(){
aantal = db1.getAantalRijenBarBestelling();
Barbestelling[] bss = new Barbestelling[aantal];
for (int i = 0; i < aantal; i++){
tafelNummer = db1.getTafelnummer(i);
bestelNummer = db1.getBestelNummer(i);
bss[i] = new Barbestelling(bestelNummer,tafelNummer,"Geplaatst");
bestellingen.add(bss[i]);
}
}
public ArrayList<String> toonBarbestellingen()
{
ArrayList<String> tafelNummers = new ArrayList<String>();
for(Barbestelling i : bestellingen)
{
tafelNummers.add(i.getTafelNummer());
}
return tafelNummers;
}
public ArrayList<Barbestelling> getBestellingen()
{
return bestellingen;
}
public String getDetails(String barbestelling){
String details = "";
boolean found = false;
int count = 0;
while(!found && count < bestellingen.size())
{
if(bestellingen.get(count).getTafelNummer().equals(barbestelling))
{
found = true;
details = bestellingen.get(count).getDetails();
}
else
{
count ++;
}
}
if(!found)
{
System.out.println("Bestelling met Tafelnummer: " + barbestelling + " bestaat niet!");
}
return details;
}
}
数据库查询:
public class Database {
private Connection con;
private int bestelNummer;
public Database() {
String connectiestring;
connectiestring = "jdbc:mysql://localhost:3306/hartigehap";
try {
con = DriverManager.getConnection(connectiestring,"root", "");
}
catch(Exception e) {
System.err.println("Melding: " + e.getMessage());
}
}
public int getAantalRijenBestelRegel(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
int i = 0;
try {
stmt = con.createStatement();
query = "SELECT COUNT(ItemItemID) FROM bestelregel WHERE barbestellingbestellingnummer = '"+bestelNummer+"'";
rs = stmt.executeQuery(query);
while (rs.next()){
i = rs.getInt("COUNT(ItemItemID)");
}
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return i ;
}
public int getAantalRijenBarBestelling(){
Statement stmt;
ResultSet rs;
String query;
int i = 0;
try {
stmt = con.createStatement();
query = "SELECT BestellingNummer FROM barbestelling WHERE Status = 'Geplaatst'";
rs = stmt.executeQuery(query);
while ( rs.next() ) {
i++;
}
stmt.close();
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return i ;
}
public String getProduct(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
String productNaam = "";
try {
stmt = con.createStatement();
query = "SELECT Naam FROM item, bestelregel WHERE bestelregel.barbestellingbestellingnummer = "+bestelNummer+" AND item.ItemID = bestelregel.ItemItemID";
rs = stmt.executeQuery(query);
rs.absolute(bestelNummer);
productNaam = rs.getString("Naam");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return productNaam;
}
public int getTafelnummer(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
int tafelNummer = 0;
bestelNummer++;
try {
stmt = con.createStatement();
query = "SELECT barbestelling.TafelNummer FROM barbestelling WHERE barbestelling.BestellingNummer = "+bestelNummer+"";
rs = stmt.executeQuery(query);
rs.absolute(1);
tafelNummer = rs.getInt("Tafelnummer");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return tafelNummer;
}
public int getAantal(int bestelNummer){
Statement stmt;
ResultSet rs;
String query;
int aantal = 0;
try {
stmt = con.createStatement();
query = "SELECT Aantal FROM bestelregel WHERE barbestellingbestellingnummer = '"+bestelNummer+"'";
rs = stmt.executeQuery(query);
rs.absolute(bestelNummer);
aantal = rs.getInt("Aantal");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return aantal;
}
public String getBestelRegel(int bestelNummer) {
Statement stmt;
ResultSet rs;
String query, querytekst1;
String details = "";
try {
stmt = con.createStatement();
query = "SELECT bestelregel.Aantal, item.Naam FROM bestelregel, item WHERE bestelregel.barbestellingbestellingnummer = " + bestelNummer + " AND item.ItemID = bestelregel.ItemItemID";
rs = stmt.executeQuery(query);
while(rs.next()){
String aantal = rs.getString("Aantal");
String naam = rs.getString("Naam");
details += aantal + " " + naam + "\n";
}
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return details;
}
public int getBestelNummer(int bestelNummer) {
Statement stmt;
ResultSet rs;
String query;
bestelNummer++;
try {
stmt = con.createStatement();
query = "SELECT BestellingNummer FROM barbestelling WHERE BestellingNummer = '"+bestelNummer+"' AND Status = 'Geplaatst'";
rs = stmt.executeQuery(query);
rs.absolute(1);
bestelNummer = rs.getInt("BestellingNummer");
}
catch(Exception e) {
System.err.println("Melding1: " + e.getMessage());
}
return bestelNummer;
}
private boolean sluitConnectie() {
boolean x = false;
try {
if(con != null) {
con.close();
x = true;
}
} catch(SQLException e) {
x = false;
}
return x;
}
}
GUI的屏幕截图:
答案 0 :(得分:1)
使用JList.setMode l设置您自己的DefaultListModel实例。它具有添加,更改和删除内容的方法,JList也应监视它以观察您的更改。
答案 1 :(得分:1)
看看你的actionPerformed方法
private void opvraagKnopActionPerformed(java.awt.event.ActionEvent evt) { // This is the refresh button
jPanel1.removeAll();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Barscherm().setVisible(true);
}
});
}
首先,从jPanel1
中删除所有活动组件,然后创建一个新的Barsdherm
窗口并使其可见。
应该发生的是您应该从数据库加载所需的所有值并刷新列表模型。
您的示例中没有足够的代码可以100%确定一个确切的解决方案,但是一种方法是将所需的数据库中的所有值加载到新的DefaultListModel
中并将此模型应用于您的{ {1}}
查看How to use lists示例