0

This is the Code. I am getting error while trying to import database items into the combo box. I am not able to take the name of the combob ox in (cmbProjectDetails.additems(name);).

Please see below code.

import java.awt.Color;

public class ProjectDetails_3_1 {

public JFrame frame;

private Connection connection=null;
private Statement statement;
private ResultSet resultSet=null;
private PreparedStatement pst=null;

private JSplitPane splitPaneRightMaster;

private JSplitPane splitPaneMaster;
private String sqlQuery;
private JPanel panelLeft;

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ProjectDetails_3_1 window = new ProjectDetails_3_1();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public ProjectDetails_3_1() {
    initialize();
}


    public  void getDatabaseConnection() {    
        try
               {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); //Load Driver
            Connection con = DriverManager.getConnection("jdbc:odbc:PRMS Build 8"); //Create Connection with Data Source Name : HOD_DATA
            Statement s = con.createStatement(); // Create Statement
            String query = "select PROJ_NAME from PROJECT"; // Create Query
            s.execute(query); // Execute Query 
            ResultSet rs = s.getResultSet(); //return the data from Statement into ResultSet
            while(rs.next()) // Retrieve data from ResultSet
            {
                System.out.print("Project Name : "+rs.getString(1)); //1st column of Table from database
                System.out.println();//System.out.print(" , Name : "+rs.getString(2)); //2nd column of Table 

            }
            s.close();
            con.close();
        }
        catch (Exception e) 
                {
            System.out.println("Exception : "+e);
        }
    }


// Function to get database connection.
 /*public void getDatabaseConnection() {
            //Registering Oracle JDBC driver class
            try {
                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            }catch(ClassNotFoundException ex) {
                System.out.println("JDBC-ODBC driver not found: " + ex);
            }

            //Opening database connection
            try {            
                connection = DriverManager.getConnection(
                     "jdbc:ucanaccess://D:/workspace/PRMS v1.0 Build 8/PRMS Build 8.mdb;memory=false",
                     "",
                     ""
                     ); 
                connection.setAutoCommit(false);
                statement = connection.createStatement();
            }catch(Exception ex){
                ex.printStackTrace();
            }

            return;
    }


    */
private void initialize() {

    frame = new JFrame();
    frame.getContentPane().setBackground(new Color(255, 255, 255));
          frame.setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\Eclipse_Workspace\\PRMS v1.0 Build 9\\resources\\Logo_48.png"));
    frame.setResizable(false);
    frame.setTitle("PRMS[Build 9] - User Portal");
    frame.setSize(1000, 700);
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setUndecorated(false);
    frame.getContentPane().setLayout(null);
    JPanel panelHeader = new JPanel();
    panelHeader.setBounds(0, 0, 1000, 10);
    panelHeader.setBackground(SystemColor.textHighlight);
    frame.getContentPane().add(panelHeader);
    panelHeader.setLayout(null);
    JPanel panelFooter = new JPanel();
    panelFooter.setBounds(0, 662, 1000, 10);
    panelFooter.setBackground(SystemColor.textHighlight);
    frame.getContentPane().add(panelFooter);
    panelFooter.setLayout(null);

    splitPaneMaster = new JSplitPane();
    splitPaneMaster.setResizeWeight(0.25);
    splitPaneMaster.setVerifyInputWhenFocusTarget(false);
    splitPaneMaster.setFocusable(false);
    splitPaneMaster.setDividerSize(0);
    splitPaneMaster.setBorder(null);
    splitPaneMaster.setOpaque(false);
    splitPaneMaster.setBounds(0, 7, 1000, 655);
    frame.getContentPane().add(splitPaneMaster);

    panelLeft = new JPanel();
    panelLeft.setRequestFocusEnabled(false);
    panelLeft.setBackground(SystemColor.textHighlight);
    splitPaneMaster.setLeftComponent(panelLeft);
    panelLeft.setLayout(null);

    splitPaneRightMaster = new JSplitPane();
    splitPaneRightMaster.setResizeWeight(0.084);
    splitPaneRightMaster.setVerifyInputWhenFocusTarget(false);
    splitPaneRightMaster.setFocusable(false);
    splitPaneRightMaster.setDividerSize(0);
    splitPaneRightMaster.setOrientation(JSplitPane.VERTICAL_SPLIT);
    splitPaneRightMaster.setBorder(null);
    splitPaneRightMaster.setOpaque(false);
    splitPaneMaster.setRightComponent(splitPaneRightMaster);        

    JPanel panelDetails3_1 = new JPanel();
    panelDetails3_1.setBackground(Color.WHITE);
    splitPaneRightMaster.setRightComponent(panelDetails3_1);
    panelDetails3_1.setLayout(null);

    JLabel lblProjectDetails = new JLabel("Project Details");
    lblProjectDetails.setFont(new Font("Segoe UI Symbol", Font.BOLD, 18));
    lblProjectDetails.setBounds(80, 26, 178, 27);
    panelDetails3_1.add(lblProjectDetails);

    JLabel lblSelectAnyProject = new JLabel("Select any of the projects tagged to you to view the details.");
    lblSelectAnyProject.setFont(new Font("SansSerif", Font.BOLD, 12));
    lblSelectAnyProject.setBounds(80, 53, 466, 27);
    panelDetails3_1.add(lblSelectAnyProject);

    JLabel lblProjectName = new JLabel("Project Name:");
    lblProjectName.setBounds(80, 102, 83, 16);
    panelDetails3_1.add(lblProjectName);

    JComboBox cmbProjectDetails = new JComboBox<String>();
    //cmbProjectDetails.addItem("1111");
    //cmbProjectDetails.addItem("2222");
    cmbProjectDetails.setBounds(173, 97, 85, 27);
    panelDetails3_1.add(cmbProjectDetails); 


}
private void fillCombo(){
    try{ 
        String sql= "select PROJ_NAME from PROJECT";
        pst=connection.prepareStatement(sql);
        resultSet=pst.executeQuery();

        while(resultSet.next()){
            String name= resultSet.getString("PROJ_NAME");
            cmbProjectDetails.additems(name);**//Error is here**

        }
    }
    catch(Exception e){
        System.out.println("Error releasing connection: " + e);
    }
}
// Function to release database connection and resources. 
public void releaseDatabaseConnection() {
    try{
         if(resultSet != null){ resultSet.close();  }
        }catch(Exception ex){
    System.out.println("Error releasing resultSet: " + ex);
    }

    try{
    if(statement != null){  statement.close();  }
        }catch(Exception ex){
    System.out.println("Error releasing statement: " + ex);
    }

    try{
    if(connection != null){ connection.close(); }
        }catch(Exception ex){
    System.out.println("Error releasing connection: " + ex);
    }

    return;
}

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • `frame.getContentPane().setLayout(null);` Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 30 '17 at 18:13

1 Answers1

1

I am not able to take the name of the combobox

Define the combo box variable as an instance variable, not a local variable.

An instance variable can be accessed by any method of the class. A local variable can only be accessed in the method where it is defined.

For example, you define these as instance variables:

private JSplitPane splitPaneRightMaster;

private JSplitPane splitPaneMaster;
private String sqlQuery;
private JPanel panelLeft;

Why can't you define the combo box variable here as well?

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Correct, also method `additems` doesn't exist in `JComboBox`, the OP needs to fix that too – BackSlash Mar 30 '17 at 18:04
  • thanks for the help. Now it doesnt give an error. Also to check database connectivity i called its method and it works fine. Thank You!!! @BackSlash how do I add items now that add items method doesnt exist for JComboBox. – Anshuman Behera Mar 30 '17 at 18:26
  • @AnshumanBehera, `how do I add items now that add items method doesnt exist for JComboBox.` - You can create a `DefaultComboBoxModel` and add it the to the combo box. Or you create a loop and add one item at a time. – camickr Mar 30 '17 at 18:46
  • @BackSlash how do i do it then rather than using addItems for my combobox? – Anshuman Behera Mar 30 '17 at 18:50
  • I am getting nullpointerexception error while executing this code. I tried debugging. Its occurring in the fillcombo method when trying to parse "String sql= "select PROJ_NAME from PROJECT";" Help Please? – Anshuman Behera Mar 30 '17 at 19:04
  • @AnshumanBehera, `how do i do it then rather than using addItems for my combobox?` - I already gave you the answer, why are you asking again??? Pay attention to the suggestions. `I am getting nullpointerexception` - this is not 20 questions. Do some basic debugging. The Exception tells you the statement causing the problem. Determine which variable is null and fix the problem. We can't help you because we don't have all the information. If the comboBox is null it is probably because you have both an "instance" and a "local" variable. Get rid of the local variable as my answer suggested. – camickr Mar 30 '17 at 19:09