2017-07-06 128 views
0

有這段視頻在YouTube上https://www.youtube.com/watch?v=Idtm2Y6I23w 由此人已動態填充從項目創建按鈕,存儲在database.For比如在這張照片 enter image description hereJavaFX如何添加元素例如。從存儲在數據庫中的項目動態創建按鈕Mysql?

它有一些類別包括TablesStockReportsUsers 當用戶點擊Table類別時,它會顯示Buttons動態地根據數據庫mysql中存儲的項目創建。例如,這是結果圖片:enter image description here

如何才能像那樣發展? 是否有for循環語句將被使用?

編輯

這就是我是如何做到的。這是源代碼。

public class OrderController implements Initializable { 

    @FXML 
    private ListView<Button> listvieww; 

    @FXML 
    private HBox hboxx; 

    List<String> listOfSomething = null; 
//@FXML 
// private Pane panedynamic; 
    private DBConnection database = new DBConnection(); 
    private Connection connection; 
    private Statement statement; 
    private ResultSet resultSet; 

    private ObservableList<Button> buttons = FXCollections.observableArrayList(); 

    /** 
    * Initializes the controller class. 
    */ 
    @Override 
    public void initialize(URL url, ResourceBundle rb) { 
    hboxx.getChildren().add(buttons) 
    } 

    @FXML 
    void acttable(ActionEvent event) { 

     // TODO 
     String sqlQuery = "SELECT * FROM restauranttables;"; 
     try { 
      connection = database.getConnection(); 
      statement = connection.createStatement(); 
      resultSet = statement.executeQuery(sqlQuery); 
      while (resultSet.next()) { 
       String current = resultSet.getString("tablename"); 
       ObservableList<String> list = FXCollections.observableArrayList(current); 
       Button b = new Button(list.toString()); 
       buttons.add(b); 

      } 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } 

    } 

    @FXML 
    void actlogout(ActionEvent event) { 

    } 

    @FXML 
    void actnew(ActionEvent event) { 

    } 

    @FXML 
    void actorder(ActionEvent event) { 

    } 

    @FXML 
    void actreports(ActionEvent event) { 

    } 

    @FXML 
    void actstock(ActionEvent event) { 

    } 



    @FXML 
    void actusers(ActionEvent event) { 

    } 

} 

這是我的場景生成器看起來像

<?xml version="1.0" encoding="UTF-8"?> 

<?import com.jfoenix.controls.JFXButton?> 
<?import com.jfoenix.controls.JFXTextField?> 
<?import javafx.scene.control.Menu?> 
<?import javafx.scene.control.MenuBar?> 
<?import javafx.scene.control.MenuItem?> 
<?import javafx.scene.control.TableColumn?> 
<?import javafx.scene.control.TableView?> 
<?import javafx.scene.layout.AnchorPane?> 
<?import javafx.scene.layout.ColumnConstraints?> 
<?import javafx.scene.layout.GridPane?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.layout.RowConstraints?> 
<?import javafx.scene.layout.StackPane?> 

<AnchorPane id="AnchorPane" prefHeight="600.0" prefWidth="1080.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.OrderController"> 
    <children> 
     <AnchorPane layoutX="55.0" layoutY="29.0" prefHeight="600.0" prefWidth="1080.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> 
      <children> 
       <TableView layoutX="21.0" layoutY="49.0" prefHeight="386.0" prefWidth="413.0"> 
        <columns> 
         <TableColumn prefWidth="122.0" text="Description" /> 
         <TableColumn prefWidth="57.0" text="Price" /> 
         <TableColumn prefWidth="81.0" text="Sub Total" /> 
         <TableColumn prefWidth="96.0" text="Table Id" /> 
         <TableColumn prefWidth="57.0" text="Price" /> 
        </columns> 
       </TableView> 
       <JFXTextField layoutX="53.0" layoutY="439.0" prefHeight="37.0" prefWidth="155.0" /> 
       <JFXTextField layoutX="253.0" layoutY="439.0" prefHeight="37.0" prefWidth="155.0" /> 
       <GridPane layoutX="63.0" layoutY="503.0" prefHeight="75.0" prefWidth="345.0"> 
        <columnConstraints> 
         <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
         <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
        </columnConstraints> 
        <rowConstraints> 
         <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
        </rowConstraints> 
        <children> 
         <AnchorPane prefHeight="200.0" prefWidth="200.0"> 
          <children> 
           <JFXButton layoutX="25.0" layoutY="25.0" onAction="#actorder" prefHeight="75.0" prefWidth="173.0" text="Order" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
          </children> 
         </AnchorPane> 
         <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1"> 
          <children> 
           <JFXButton layoutX="39.0" layoutY="14.0" onAction="#actnew" prefHeight="75.0" prefWidth="172.0" text="New" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
          </children> 
         </AnchorPane> 
        </children> 
       </GridPane> 
       <MenuBar layoutX="21.0" layoutY="14.0" prefHeight="29.0" prefWidth="413.0"> 
        <menus> 
         <Menu mnemonicParsing="false" text="File"> 
          <items> 
           <MenuItem mnemonicParsing="false" text="Close" /> 
          </items> 
         </Menu> 
         <Menu mnemonicParsing="false" text="Edit"> 
          <items> 
           <MenuItem mnemonicParsing="false" text="Delete" /> 
          </items> 
         </Menu> 
         <Menu mnemonicParsing="false" text="Help"> 
          <items> 
           <MenuItem mnemonicParsing="false" text="About" /> 
          </items> 
         </Menu> 
        </menus> 
       </MenuBar> 
       <StackPane fx:id="acContent" layoutX="454.0" layoutY="49.0" prefHeight="525.0" prefWidth="605.0"> 
        <children> 
         <AnchorPane fx:id="anchora" prefHeight="200.0" prefWidth="200.0"> 
          <children> 

           <GridPane fx:id="sasa" layoutX="17.0" layoutY="7.0" prefHeight="90.0" prefWidth="571.0"> 
            <columnConstraints> 
             <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
             <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
             <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
             <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
             <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> 
            </columnConstraints> 
            <rowConstraints> 
             <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> 
            </rowConstraints> 
            <children> 
             <AnchorPane prefHeight="200.0" prefWidth="200.0"> 
              <children> 
               <JFXButton fx:id="table" layoutX="37.0" layoutY="19.0" onAction="#acttable" prefHeight="90.0" prefWidth="135.0" text="TABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
              </children> 
             </AnchorPane> 
             <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1"> 
              <children> 
               <JFXButton fx:id="stock" layoutX="43.0" layoutY="32.0" onAction="#actstock" prefHeight="90.0" prefWidth="134.0" text="STOCK" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
              </children> 
             </AnchorPane> 
             <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="2"> 
              <children> 
               <JFXButton fx:id="reports" layoutX="26.0" layoutY="32.0" onAction="#actreports" prefHeight="90.0" prefWidth="134.0" text="REPORTS" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
              </children> 
             </AnchorPane> 
             <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="3"> 
              <children> 
               <JFXButton fx:id="users" layoutX="26.0" layoutY="32.0" onAction="#actusers" prefHeight="90.0" prefWidth="134.0" text="USERS" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
              </children> 
             </AnchorPane> 
             <AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="4"> 
              <children> 
               <JFXButton fx:id="logout" layoutX="28.0" layoutY="32.0" onAction="#actlogout" prefHeight="90.0" prefWidth="134.0" text="LOGOUT" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" /> 
              </children> 
             </AnchorPane> 
            </children> 
           </GridPane> 
         <HBox fx:id="hboxx" layoutX="17.0" layoutY="110.0" prefHeight="398.0" prefWidth="571.0" /> 
          </children> 
         </AnchorPane> 
        </children> 
       </StackPane> 
      </children> 
     </AnchorPane> 
    </children> 
</AnchorPane> 

我將要對齊的按鈕在橫向盒,但我在這裏

@Override 
    public void initialize(URL url, ResourceBundle rb) { 
    hboxx.getChildren().add(buttons) 
    } 

得到一個錯誤的任何幫助,謝謝。

+0

顯示你的嘗試。然後,人們將能夠幫助您編寫代碼,而不是爲您編寫代碼。 – Tavo

+0

讓我告訴你我試過的東西 –

回答

2

你想實現的工作流程:

  1. 檢索基於一個動作事件
  2. 對於每一行從返回從DB一些信息(1) - >創建一個按鈕
  3. 將所有創建的按鈕添加到某個容器

    @FXML 
    void acttable(ActionEvent event) { 
    
    String sqlQuery = "SELECT * FROM restauranttables;"; 
    List<Button> buttonlist = new ArrayList<>(); //our Collection to hold newly created Buttons 
    try { 
        connection = database.getConnection(); 
        statement = connection.createStatement(); 
        resultSet = statement.executeQuery(sqlQuery); 
        while (resultSet.next()) { //iterate over every row returned 
         String restaurant = resultSet.getString("restaurantname"); //extract button text, adapt the String to the columnname that you are interested in 
         buttonlist.add(new Button(restaurant)); 
    
        } 
        hboxx.getChildren().clear(); //remove all Buttons that are currently in the container 
        hboxx.getChildren().addAll(buttonlist); //then add all your Buttons that you just created 
    
    } catch (SQLException e) { 
        e.printStackTrace(); 
    } finally{ 
        resultSet.close(); 
        statement.close(); 
        connection.close; 
    } 
    } 
    

所以工作流程,有什麼發生了:

  1. 您創建一個列表,將保留您的所有新的按鈕。
  2. 你執行一個查詢
  3. 從2
  4. 遍歷ResultSet中你搶,你要設置爲按鈕文本字符串值
  5. 你,按鈕添加到列表
  6. 你清楚你的HBox中從先前添加的按鈕
  7. 添加的所有新的按鈕到您的HBox中

編輯:不要忘記關閉ResultSet,語句和連接。如果您想將try/catch轉換爲try with resources,則可以避免顯式的.close(),因爲所有三個對象都實現了Autoclosable接口。我添加了一個「finally」語句,當您的try塊退出時將執行該語句。你也可以檢查resultSet/statement/connection是否爲空。

+0

它工作謝謝 –

0

你需要一個循環。基本上,你可以遍歷你的項目,併爲每個項目創建一個按鈕,並將其添加到窗格。

List<Element> databaseItems = getElementFromDataBase(); 
int i = 0; 
int j = 0; 
for(Element databaseItem : databaseItems) { 
    Button tempButton = new Button(databaseItem.getName()); 
    tempButton.setOnAction(new MyClickListener()) //handle button click 
    gridPane.add(i, j); 
    i ++; 
    if(i == GridPane.getRowIndex()) { 
     j++; // get to the new row. 
    } 
} 

該代碼可能不準確,但它給出了一個大致的瞭解。

0

// connectivity for mysql database coding 
 
String category_name= "Stock"; 
 
    
 
Class.forName("com.mysql.jdbc.Driver"); 
 
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sonoo","root","root"); 
 
Statement stmt=con.createStatement(); 
 
ResultSet rs=stmt.executeQuery("select * from category where category_name=" + "'" + category_name + "'"); 
 
rs.next(); 
 

 

 
int num_of_rows = rs.getInt(3); (Fetched from database according to category) 
 

 
Panel p1=new Panel(); 
 
p1.setLayout(new GridLayout(Math.Ceil(num_of_rows/6),6,10,10)); 
 

 
Button b[]; 
 

 
b=new Button[num_of_rows]; 
 

 
for(int i=0;i<num_of_rows;i++) 
 
{ 
 
\t b[i]=new Button(category_name + "" + (i+1)); 
 
\t p1.add(b[i]); 
 
\t b[i].addActionListener(this); // needed if to perform button click 
 
} 
 

 
//adding the panel to the container 
 
add(p1); 
 

 

 
public void actionPerformed(ActionEvent ae) 
 
{ 
 
\t // The event click code goes here 
 
}

+0

其實你提供了一個可以在swing上運行的代碼,我想要一個javafx一個謝謝 –

+0

@Jalin寫一個答案不僅僅是寫正確的代碼。解釋你的代碼的作用同樣重要。 – Tavo

相關問題