3

I am trying to get items from a table and display them into JList. I ran the code and nothing was displayed in JList and there was no error. I debugged the code and it counted until table item length. Followed this answer.

static Connection conn = new DBConnection().connect();
private JList listDepartments = null;

public AddDepartment() {
    listDepartments = new JList();
    listDepartments.setBounds(189, 33, 1, 1);
    contentPane.add(listDepartments);

    update_departments(listDepartments);
}

private static void update_departments(JList listDepartments) {
    try {
        String sql = "Select * FROM Departments";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        DefaultListModel listModel = new DefaultListModel();
        while (rs.next()) {
            System.out.println("inside while");
            String departmentName = rs.getString("Name");
            listModel.addElement(departmentName);
        }
        listDepartments.setModel(listModel);
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(null, ex);
    }
}

Table Content: Name: Departments

Columns:

Id - INT primary key, auto increment, unique

Name - VARCHAR(255)

8
  • 4
    listDepartments.setBounds(189, 33, 1, 1); - don't use setBounds(...). Swing was designed to be used with a layout manager. And definitely don't use a width/height of 1 (this will be nothing to paint). First get a simple example working where you hard code the data in the JList. Then once you understand the basics of Swing you make the code more dynamic by getting the data from a database. If you need more help then post a proper SSCCE of your code that uses hardcoded data. Commented Oct 28, 2016 at 21:17
  • SetBounds is created automatically when I draged-droped the JList. Commented Oct 28, 2016 at 21:23
  • 1
    So fix it. Don't use the drag and drop feature of the IDE. Create the GUI on your own. Read the Swing tutorial on How to Use Lists for working example that show how to use a JList with manually created Swing components. Commented Oct 28, 2016 at 21:26
  • 1
    So what does your logic do? Is there data in the ResultSet? Is data added to the ListModel? If you get data in the ListModel then the code is exactly like using hardcoded data. If you can't get data in the model then the problem is your database. We can't debug your "while loop". Only you can do that. Commented Oct 29, 2016 at 0:55
  • 2
    .. 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 along with layout padding and borders for white space. Commented Oct 29, 2016 at 7:39

1 Answer 1

2

Changed with JTable and added followed codes in public addDepartment()

model = new DefaultTableModel();
tableDepartments = new JTable(model);

removed listDepartments.setModel(listModel); from update_departments() function.

Sign up to request clarification or add additional context in comments.

2 Comments

public addDepartment() BTW - My bad. Given that addDepartment describes an action, I mistook it for a method name. Given it is the class name/scontructor, it should start with an upper case letter, though it might better be called DepartmentHandler or DepartmentQuery or some other name that does not imply an immediate action.
Thank you again. Eclipse made me give class names starting with upper case. Thats why public AddDepartment() was generated automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.