Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java programming

Posted on 2014-01-05
8
Medium Priority
?
362 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

604 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