Solved

Java programming

Posted on 2014-01-05
8
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39757554
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
ID: 39757610
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
ID: 39757636
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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

Author Comment

by:sargent240
ID: 39757641
I meant to say, are you saying I cannot set the table up without an empty row using the approach I have posted?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39757647
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 36

Accepted Solution

by:
mccarl earned 500 total points
ID: 39758288
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
ID: 39758383
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 36

Expert Comment

by:mccarl
ID: 39758450
Your quite welcome! ;)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

691 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