473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Is it possible to have an empty integer?

dlite922
1,584 Expert 1GB
Sorry for the lolcat title, but seriously I don't know what the best approach would be.

I'm working with a table model. In the data I have a String, 2 Integers, and 1 Boolean column.

The Boolean and one of Integer columns CAN have empty values.

When I'm populating the data I'm setting the default values of the Boolean to false and the integer to null like so:

Expand|Select|Wrap|Line Numbers
  1.  
  2. result[ri][0] = sdtSrvc.getServiceId();
  3. result[ri][1] = "   "+sdtSrvc.getCallTag();
  4. result[ri][2] = (mapSrvc != null) ? mapSrvc.getCableID() : null; //<--------- Empty Integer Needed Here instead of null
  5. result[ri][3] = (mapSrvc != null) ? mapSrvc.getEnabledFlag() : false;
  6. result[ri][4] = sdtSrvc.getTransportStreamId();
  7.  
  8.  
Symptom of problem:
The records that DO have a value, JTable treats them like a string and aligns them to the left. Yuck!

I should mention I have overridden the AbstracTableModel's getColumnClass() like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     @Override
  3.     public Class getColumnClass(int c)
  4.     {
  5.         Object val = getValueAt(0, c);
  6.  
  7.         if(val == null) {
  8.             Integer scrap = 0;
  9.             return scrap.getClass();
  10.         } else
  11.         {
  12.             return getValueAt(0, c).getClass();
  13.         }
  14.     }
  15.  
This method is called by JTable's Cell Render..er. As you can see I'm creating an int and returning it.

it works, but again Yuck! and what if there's an empty record in the String column. The cell render will treat it as an int.

If I could somehow declare my default value as an empty int that would be great and I can change the above method to just the return line:

return getValueAt(0, c).getClass();

and be done with it!

My other option is to evaluate c (the column index) and find which column it is and hard code the default type of that column, but again what if I reorder my columns or drop columns in the future? I'd rather not have this mess to maintain.

In short, can I have my cake and 'not' eat it too?

: )

Thanks for any feedback guys!


Dan
Nov 19 '10 #1
6 36987
Dheeraj Joshi
1,123 Expert 1GB
Integer can not be empty Dan. But you can display them as Strings. So if null is the value of in just display "". That is nothing to display.

Probably i am confused about your question :(

Regards
Dheeraj Joshi
Nov 22 '10 #2
dlite922
1,584 Expert 1GB
Thanks Dheeraj, That doesn't get the rid of the symptom. Which is: It makes the table think that column is a string instead of an integer, hence rending the numbers further down the list as a string!

If I hard code logic in the getColumnClass() method, then I'd hard code the column index. Something I don't want to do because it requires maintenance.

How would you go about handling this situation? At the moment I'm defaulting every empty column to an integer.

Thanks again,

Dan
Nov 22 '10 #3
Dheeraj Joshi
1,123 Expert 1GB
Why can't you render all the integers as String and display it in the table?

Yes, Overriding may not be a good option.

I would default all empty columns as a String.

Regards
Dheeraj Joshi
Nov 22 '10 #4
Dheeraj Joshi
1,123 Expert 1GB
What i am saying even if the values is int or null render it as String. (Unless your app is not very bulky).

It solves the rendering problems.

Regards
Dheeraj Joshi
Nov 22 '10 #5
dlite922
1,584 Expert 1GB
So I can do that, but it's not just the empty values that are rendered as string, but the integers in that row will now be displayed as integer (i.e. they are left aligned instead of right aligned, you can enter non-number characters instead of just digits, etc)

Thanks,
Nov 22 '10 #6
Dheeraj Joshi
1,123 Expert 1GB
I thought it is just displaying(read-only). But anyway if user can enter the values for the table then displaying them as String is not a good idea, since it involves a lot of validations when user enters some characters or symbols.

Regards
Dheeraj Joshi
Nov 23 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Randell D. | last post by:
Folks, If I have a number that includes decimal points and I want to remove everything right of the decimal, how do I do it - I know I could explode using the point as a delimiter but that...
5
by: Anton | last post by:
I have a table of zip codes, some with Canadian zips. I'd like to take a zip code and search for nearby zips. For example: Dim theZip As Integer = textbox1.text ....Parameter.Add(@ziplow,...
3
by: nephish | last post by:
Hey there, i have a simple database query that returns as a tuple the number of rows that the query selected. kinda like this >>> cursor.execute('select value from table where autoinc > 234')...
19
by: wetherbean | last post by:
Hi group..I am writing a playlist management protocol where I have a file that holds all the playlists and a file that holds all the songs....before a playlist is created I need to check to see if...
1
by: Oleg Ogurok | last post by:
Hi all, I want to use RegularExpressionValidator to enforce non-empty integer format in a TextBox. However, the validator doesn't give the error when the textbox is empty. For example, if...
3
by: Dean Slindee | last post by:
I want to build an array on the fly in code with a parameter passed for the number of elements in the array. I am trying to do this by looping, but get an array with just one element in the end....
1
by: Sivaraman.S | last post by:
Hi, Can i pass integer array to methodInfo.Invoke(obj,args()). I am able to pass only string array to this function. This is the code i have written. Dim myType As Type = objClass.GetType()...
11
by: nephish | last post by:
Hello there, i need a way to check to see if a certain value can be an integer. I have looked at is int(), but what is comming out is a string that may be an integer. i mean, it will be formatted...
1
by: =?Utf-8?B?RG9u?= | last post by:
Hello, I'm creating a web service that will allow people to enter their contact information into a SQL Server table. I get it to work when I enter all of the fields and press the invoke button,...
5
by: Joe | last post by:
does anyone have any clue??
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.