سؤال

I have got three Java classes:

  • The First has a setter and a getter
  • The Second has a JComboBox
  • The third has a MySQL query and generating list.

I used the setter to set the value and I set the value from the combo box class.

Now, I would like to get this value in another class.

Here is my code:

public class Settings {

    private static String RootName;

    public static void setRootName(String rootName){
     RootName = rootName;
    }

    public static String getRootName(){
     return RootName;
    }
}

combobox.java

public class ComboBoxDemo extends JPanel
                          implements ActionListener {
    String connectionURL = "jdbc:mysql://localhost:3306/Trainpis";
    JLabel picture;
    public static String i="hello";
public String rootname;
    public ComboBoxDemo()  {

 JComboBox combo=new JComboBox();
combo.addActionListener(this);



JFrame f=new JFrame();
JPanel p=new JPanel();
try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection(connectionURL, "root", "");
Statement st=conn.createStatement();
ResultSet rs=st.executeQuery("Select route from route");
while(rs.next()){
combo.addItem(rs.getString("route"));
//System.out.println(rs.getString("route"));
}
}
catch(Exception e){}
p.add(combo);
f.add(p);
  f.setExtendedState(JFrame.MAXIMIZED_BOTH); 
  f.setUndecorated(true);
f.setVisible(true);

 }

    /** Listens to the combo box. */
    public void actionPerformed(ActionEvent e) {
     JComboBox cb = (JComboBox)e.getSource();
        String selectedRoute = (String)cb.getSelectedItem();
       // System.out.println(rootname);
        String root1="Huda City Center - Noida City Center";
       if(selectedRoute.equalsIgnoreCase(root1))
       {
           System.out.println("hello");
     //new Test();
           //Settings mysettings = new Settings();
   Settings.setRootName(selectedRoute);

    RootSelection1 r1 = new RootSelection1();
        r1.print();
       }
       else{
       System.out.println("bye");
       } 
    }

   public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ComboBoxDemo();
            }
        });
    }
}

Now I want to use the selected value of combo box:

String rootSelection = Settings.getRootName();
String selectStoredProc = "SELECT sino,stationname,distance from station where route ='"+rootSelection+"'";
String [] root;

try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(connectionURL, "root", "");

    PreparedStatement ps = conn.prepareStatement(selectStoredProc);
    ResultSet rs=ps.executeQuery();
    while(rs.next()){
        String s1=rs.getString("stationname"); 
        nameList.add(s1);
        root = nameList.toArray(new String[nameList.size()]);
    }
}
catch(Exception e){}

I want to do all of this on combo box's selected item.

How can I achieve this?

هل كانت مفيدة؟

المحلول

In your second class create a Settings object

public void actionPerformed(ActionEvent e) {
    JComboBox comboBox = (JComboBox)e.getSource();
    String selectedRoute = (String)comboBox.getSelectedItem();
    RootSelection r1=new RootSelection();

    //Settings object
    Settings mySettings = new Settings();
    mySettings.setRootName(selectedRoute);
}

Then pass this object to your method in your third class

A side note:

you shouldn't make your Settings class full of static things

E.g. This is better

public class Settings {

    private String RootName;

    public void setRootName(String rootName){
     RootName = rootName;
    }

    public String getRootName(){
     return RootName;
    }
}

نصائح أخرى

Static variables are class levels. So You are changing the fist class static variable in second calls. So you can access the first class variable in third class. You can get by using Settings.getRootName(); in your third class

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top