0

我的 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;
}

}

图形用户界面截图:

http://img90.imageshack.us/img90/1676/schoolopdrachtgui.jpg

4

2 回答 2

1

Take a look at your actionPerformed method

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);
        }
    });
}

First, you remove all the active component from jPanel1, then you create a new Barsdherm window and make it visible.

What should be happening is you should be loading all the values from the database that you need and refreshing the list model.

There's not enough code in your example to be 100% sure of an exact solution, but one method would be to load all the values from the database you need into a new DefaultListModel and apply this model to your JList

Take a look at How to use lists for examples

于 2013-05-04T09:44:35.433 回答
1

使用JList.setMode l 设置您自己的DefaultListModel实例。它具有添加、更改和删除内容的方法,并且 JList 应该监视它并观察您的更改。

于 2013-05-04T09:19:41.530 回答