Solved

Java programming

Posted on 2014-01-05
8
349 Views
Last Modified: 2014-01-05
I am setting up a table in java and it starts with a blank row.  When I go to add my data the first row is always blank.  How do I correct that.  I'm not sure what code to attach so if someone sees some code I have not attached I will do so.  Thanks.

    String homePlayerData[][] = {{"", "", "", "", ""}};
    String homePlayerCol[] = {"Number", "Player", "Points", "Fouls"};

    DefaultTableModel homeTable = new DefaultTableModel(homePlayerData,     homePlayerCol);

            while (rec.next()) {
                if (team.equals("home")) {
                    homeTable.addRow(new Object[]{
                        rec.getString("number"),
                        rec.getString("name"),
                        rec.getString("points"),
                        rec.getString("fouls"),
                    });
            } else {
                    visitorTable.addRow(new Object[]{
                        rec.getString("number"),
                        rec.getString("name"),
                        rec.getString("points"),
                        rec.getString("fouls"),
                    });
                }
            }
0
Comment
Question by:sargent240
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I'm wondering why you're surprised. If the TableModel contains an empty row and then you add (append) to it, then why should the empty row disappear?

I would generally use the Vector type of TableModel rather than Object[]. The latter is for fixed conditions. Also your else-if is odd: you do precisely the same thing in each case

http://technojeeves.com/joomla/index.php/free/59-resultset-to-tablemodel
0
 

Author Comment

by:sargent240
Comment Utility
The reason I am surprised is because I am trying to learn a little about java programming and am not familiar with a lot of how it works.  I will look at the web site you posted and thanks.
0
 

Author Comment

by:sargent240
Comment Utility
In the if I write the data to the home table and the else to the visitors table.  Are you saying I cannot set the table up without an empty row?  I'm not certain how the empty row gets there in the first place.  Is it put there because of the way I am setting up the table?
0
 

Author Comment

by:sargent240
Comment Utility
I meant to say, are you saying I cannot set the table up without an empty row using the approach I have posted?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
I meant to say, are you saying I cannot set the table up without an empty row using the approach I have posted?

There's some confusion here. You can't set the table up without an empty row using the approach you posted because ... your approach uses an empty row.

You can set a table up without an empty row simply by only using filled rows for the TableModel. See the code i posted. Set the resulting TableModel to your table
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
You have an empty row because of those empty strings in your homePlayerData array. There is a difference between an array that contains one row of empty strings and a array that does not have an strings in it at all.

To do it in a similar way to what you are doing, you can actually get rid of that homePlayerData altogether. Then replace the line that creates your DefaultTableModel with this...
DefaultTableModel homeTable = new DefaultTableModel(homePlayerCol, 0);

Open in new window

It uses a different constructor that takes your array of column names and an integer of how many rows the table should initially have. Since you add your rows in the code after this, you should start with zero rows here.






On a slightly different note, just a hint to make that code a little easier to maintain later (by reducing the code redundancy) you can use the following loop...
            while (rec.next()) {
                Object[] rowData = new Object[]{
                        rec.getString("number"), 
                        rec.getString("name"), 
                        rec.getString("points"), 
                        rec.getString("fouls"),
                    };

                if (team.equals("home")) {
                    homeTable.addRow(rowData);
                } else {
                    visitorTable.addRow(rowData);
                } 
            }

Open in new window


And if what you posted above is pretty close to your actual code (and therefore "team" never changes while you are inside that "while" loop), even better to do this...
            DefaultTableModel teamTable;
            if (team.equals("home")) {
                    teamTable = homeTable;
            } else {
                    teamTable = visitorTable;
            }

            while (rec.next()) {
                teamTable.addRow(new Object[]{
                        rec.getString("number"), 
                        rec.getString("name"), 
                        rec.getString("points"), 
                        rec.getString("fouls"),
                    });
            }

Open in new window

(If you not following what I said above, can you post more of your code so that we can see exactly how homeTable, visitorTable and team variables are defined/used and we can help further)
0
 

Author Closing Comment

by:sargent240
Comment Utility
The first line of your code, replacing the homePlayerData with a zero did exactly what I was looking for and a simple adjustment.  The balance of your code I follow exactly and was a nice touch.  Thanks as usual your a hand my friend.  Cheers
0
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Your quite welcome! ;)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now