Solved

Java programming

Posted on 2014-01-05
8
359 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe 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 35

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 35

Expert Comment

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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

733 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