0

I wrote a method, that creates my DefaultTableModel and there I'm going to add my records. When I set the model to my JTable, the data rows are blank. After scrolling the data gets displayed correct.

How can I avoid this and display the data from the first moment?

EDIT: I imported the javax.swing.table.DefaultTableModel --> is this correct?

private DefaultTableModel _dtm;

private void loadTable(Vector<Member> members) {
    loadTableModel();

    try {
        lbl_state.setText("Please wait");

        for (Member actMember : members) {
            String gender = "";
            if (actMember.getGender() == MemberView.MEMBER_MALE) {
                gender = "männlich";
            } else {
                gender = "weiblich";
            }

            _dtm.addRow(new Object[]{
                        actMember.getNname(),
                        actMember.getVname(),
                        actMember.getCity(),
                        actMember.getStreet(),
                        actMember.getPlz(),
                        actMember.getMail(),
                        actMember.getPhonenumber(),
                        actMember.getBirthdayString(),
                        actMember.getStartDateString(),
                        gender,
                        actMember.getBankname(),
                        actMember.getAccountnumber(),
                        actMember.getBanknumber(),
                        actMember.getGroup().toString(),
                        (actMember.hasAccess() ? "JA" : "NEIN"),
                        actMember.getWriteDateString(),
                        (actMember.hasDrinkAbo() ? "JA" : "NEIN")
                    });
        }
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
    }

    tbl_results.setModel(_dtm);
}

private void loadTableModel() {
    _dtm = new DefaultTableModel(new Object[]{"Nachname",
                "Vorname",
                "Ort",
                "Straße",
                "PLZ",
                "E-Mail",
                "Telefon",
                "Geburtsdatum",
                "Beitrittsdatum",
                "Geschlecht",
                "Bankname",
                "Kontonummer",
                "Bankleitzahl",
                "Gruppe",
                "hat Zugriff",
                "Einschreibdatum",
                "Getränkeabo"}, 0);

    tbl_results.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Christian 'fuzi' Orgler
  • 1,682
  • 8
  • 27
  • 51
  • 4
    for better help sooner post an [SSCCE](http://sscce.org/) , otherwise everything here could be shots to the dark, – mKorbel Oct 28 '12 at 16:19
  • 1
    Yes the SSCCE is really necessary, here, as the shown code does not indicate any particular problem. – Guillaume Polet Oct 28 '12 at 18:49
  • This could possibly by a EDT violation problem. Make sure that you add data to the table model from within the EDT IF you set the table model before adding the data, or set the model within the EDT if you set the model after filling it – MadProgrammer Oct 28 '12 at 18:53
  • After my rush, incorrect answer the only explanation I can come up with is that you perhaps have your own implementation of JTable. What is the type of `tbl_results`? Is it a JTable or something else? If it is something else, does it handle table structure change event?? As a subclass, it should but perhaps you override the handler, or you remove that listener? – DejanLekic Oct 28 '12 at 19:24
  • no it's a original JTable type, because I added it with the gui-designer... the next weird thing is, that i cannot select any row without changing some editors etc. – Christian 'fuzi' Orgler Oct 29 '12 at 23:38

3 Answers3

2

The short answer is that it should work. You shouldn't be required to do any extra action.

The way the code is, I don't see any reason for it not to work. I have just tried it with my own example and it works fine.

However, please make sure the following:

  • the JTable is added to a JScrollPane correctly
  • the JScrollPane is added correctly to a panel
  • you are setting the model to the right JTable
Dan D.
  • 32,246
  • 5
  • 63
  • 79
0

You can "cheat" and simulate scrolling using simple code, or you can repaint the frame after populating the table. Maybe check this out: JTable How to refresh table model after insert delete or update the data.

Community
  • 1
  • 1
Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
  • I didn't find some code or hint, how I can 'cheat' the scrolling..!? – Christian 'fuzi' Orgler Oct 28 '12 at 16:36
  • Post SSCCE, as suggested. I'm no expert but nine times out of ten redrawing elements solves things. "Cheating" would be scrolling using code. To find out how just google it (-: – Reut Sharabani Oct 28 '12 at 16:40
  • 1
    @Christian'fuzi'Orgler Don't cheat, that's a lame solution that will only cause you more problems than solutions. If you really want to fix this, post an [SSCCE](http://sscce.org). And no you don't need to call fireTableStructureChanged() and fireTableDataChanged when you use the API of DefaultTableModel, it will do that on its own for you. – Guillaume Polet Oct 28 '12 at 18:43
0

TABLE WAS INVISIBLE

I checked every single line in my code, and it showed up that i set my table not visible. Only when I added the data and set it visibile. So I couldn't see my data and select rows.

Christian 'fuzi' Orgler
  • 1,682
  • 8
  • 27
  • 51