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)

Community
  • 1
  • 1
Munchmallow
  • 311
  • 1
  • 2
  • 15
  • 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](http://sscce.org/) of your code that uses hardcoded data. – camickr Oct 28 '16 at 21:17
  • `SetBounds` is created automatically when I draged-droped the JList. – Munchmallow Oct 28 '16 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](http://docs.oracle.com/javase/tutorial/uiswing/components/list.html) for working example that show how to use a JList with manually created Swing components. – camickr Oct 28 '16 at 21:26
  • That is not the problem. I can add static items, but I need to get data from db. There is no error, thats why I am not sure where the error is even I debugged. – Munchmallow Oct 29 '16 at 00:37
  • 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. – camickr Oct 29 '16 at 00:55
  • Yes there is. I changed from JList to JTable and still nothing is shown. When I print out the rowCount, I can see that there is something in it. – Munchmallow Oct 29 '16 at 00:58
  • *"..still nothing is shown.."* For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (Pretty much as @camickr advised in their first comment.) My major concern relates to when `AddDepartment()` (BTW - Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently) is called. I'd recommend adding the list at start-up and simply change the model on receipt of data. And also about the bounds .. – Andrew Thompson Oct 29 '16 at 07:39
  • 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](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Oct 29 '16 at 07:39

1 Answers1

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.

Munchmallow
  • 311
  • 1
  • 2
  • 15
  • `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. – Andrew Thompson Oct 30 '16 at 03:49
  • Thank you again. Eclipse made me give class names starting with upper case. Thats why `public AddDepartment()` was generated automatically. – Munchmallow Oct 30 '16 at 12:46