[英]Passing a jcombobox variable from one class to another class using a jbutton action in another class
So, in Java
I have two classes is created using swing components
. 因此,在
Java
我有两个使用swing components
创建的类。 In one class, named SearchPage
, I set up parameters for a search in the Jtextfield
or Jcombobox
provided and click the apply jbutton
. 在一个名为
SearchPage
类中,我在提供的Jtextfield
或Jcombobox
设置了用于搜索的参数,然后单击apply jbutton
。 When I do that, depending on what field contains data, I want to send that variable over to the ItemPage
class to be used in my pop_tree method to use that variable to search my mysql database and populate the tree with the search results. 当我这样做时,我想根据哪个字段包含数据,将该变量发送到
ItemPage
类,以在我的pop_tree方法中使用该变量,以使用该变量搜索我的mysql数据库并用搜索结果填充树。 How can I accomplish this? 我该怎么做?
Here is the code for the pop_tree
method: 这是
pop_tree
方法的代码:
public final void pop_tree() throws SQLException {
//creating root node
DefaultTreeModel dbtree = (DefaultTreeModel)DBTree.getModel();
dbtree.reload();
dbtree.getRoot();
DefaultMutableTreeNode content = new DefaultMutableTreeNode("Content");
//let's see if I can create these child nodes
SearchPage s = new SearchPage();
try {
con = DBconnect.getConnection();
stm = con.createStatement();
s.apply_search_button.doClick();
if(s.apply_search_button.getModel().isPressed()){
//Grab what's in search field of Search Page
try{
String search = s.search_field.getText();
if (search == null){
return;
}else{
ResultSet rs = stm.executeQuery("SELECT * from displaydetails WHERE item LIKE '%"+search+"%'");
while (rs.next()){
DefaultMutableTreeNode itemNode = new DefaultMutableTreeNode (rs.getString("item"));
content.add(itemNode);
}
}
}catch(SQLException e){
System.out.println(e);
}
try{
//If a genre selection is made in the combo box:
if ((s.option_box.getSelectedItem())== "genre"){
String genre2 = s.result_box.getSelectedItem().toString();
if (genre2.equals(s.result_box.getSelectedItem())) {
ResultSet rs1 = stm.executeQuery("SELECT * from displaydetails WHERE genre = '" + genre2 + "';");
while (rs1.next()){
DefaultMutableTreeNode itemNode = new DefaultMutableTreeNode (rs1.getString("item"));
content.add(itemNode);
}
}
}
}catch(SQLException e){
System.out.println(e);
}
I have the search form created to setup the search parameters, but when I click the apply_search_button
on the form, I want to send the variable to the proper location in the pop_tree()
method to perform the search. 我已经创建了用于设置搜索参数的搜索表单,但是当我单击
apply_search_button
上的apply_search_button
时,我想将变量发送到pop_tree()
方法中的适当位置以执行搜索。 Can anyone help me with this? 谁能帮我这个? Thanks.
谢谢。
To get answer to "Passing a jcombobox variable from one class to another class using a jbutton action in another class" much of the code posted, is not needed. 要获得“在另一个类中使用jbutton动作将jcombobox变量从一个类传递到另一个类”的答案,不需要编写许多代码。 Always consider posting [mcve] .
始终考虑发布[mcve] 。
Here is one that demonstrates just that : 这是一个说明:
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class SearchPage extends JFrame {
private JTextField searchField;
private JButton searchButton;
private ItemPage itemPage;
SearchPage(){
setSize(200,100);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
searchField = new JTextField(9);
add(searchField, BorderLayout.NORTH);
searchButton = new JButton("Search");
searchButton.addActionListener(e -> {
String searchFor = searchField.getText();
if ((searchFor != null) && ! searchFor.isEmpty()) {
itemPage.search(searchFor);
}
});
add(searchButton, BorderLayout.SOUTH);
itemPage = new ItemPage();
setVisible(true);
}
public static void main(String[] args) {
new SearchPage();
}
}
class ItemPage {
public void search(String searchFor) {
System.out.println("Searching for "+ searchFor);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.