?
Solved

Java programming

Posted on 2014-01-05
8
Medium Priority
?
361 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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 2000 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

771 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