1

I am reading big data from text file by jComboBox using this code.

Text file have about 100 columns and I am reading necessary column with this code:

(substring(12, 13) related to - "A, B, C, D, ...", substring(58, 96) related to - "Some text")

jComboBox1.addItem(str.substring(12, 13) + str.substring(58, 96));

enter image description here

And in output I am getting jComboBox with empty rows and empty space. As shown in picture I don't need to read rows flagged with "AAA" (in blue line) and rows with comment (in red line).

I would like to ask is it possible to remove extra blanks and exclude rows flagged with "AAA"?

Thank you in advance.

Community
  • 1
  • 1
  • 2
    The short answer is, yes. You have two choices, either filter out the content when you read the file or filter out the content when you add it to your combobox – MadProgrammer May 20 '15 at 10:25
  • @MadProgrammer I found this [code](http://stackoverflow.com/questions/27753375/jcombobox-search-list) and w'd like to adopt "Decorator" class, in that case can I include your method to "Decorator" class or necessary write separately filter classes? –  May 20 '15 at 13:53
  • The decorate you've describe simply wraps a preexisting combobox, so assuming you've filtered the list already, use you can use the decorator – MadProgrammer May 20 '15 at 21:53
  • @ MadProgrammer I used decorator class but in that case I am getting combo list with all columns (my purpose is read just 2 column). So it means if JComboBox is filterable in that case JComboBox doesn't support columns? –  May 23 '15 at 14:23
  • No, `JComboBox` doens't support columns, `JTable` does. `JComboBox` is filterable by default, `JTable` is. You need to parse you text, probably into a POJO and then use that, along with a custom `ListCellRenderer` to define how it should be shown in the `JComboBox`. In your case, filter the values you don't want out first – MadProgrammer May 23 '15 at 22:16

1 Answers1

0

Try this answer :

for (String str : lines) {
   if ( (!str.startsWith("AAA")) && (!str.substring(12, 13).equals(" ")) )
      jComboBox1.addItem(str.substring(12, 13) + str.substring(58, 96));
}
  • Yes, also works. I'll edit my answer, I appreciate more your method. – Normal design May 20 '15 at 12:14
  • @Normal design Thank you very much. Your code works very well. But I cannot decide, better use your code or method of MadProgrammer? –  May 20 '15 at 13:57
  • I think that if you want to re-use the data from the file in another part of the code, it is better to apply my solution. Indeed, if the data has to be re-used, keep the original version of it instead of removing some of it from the beginning of the load. – Normal design May 20 '15 at 14:05
  • @Normaldesign I w'd like to ask some advice from you, I want to make a filterable Combo list as shown [here](http://stackoverflow.com/questions/27753375/jcombobox-search-list?lq=1). I tried to use `JComboBoxDecorator.decorate(jcb1, true, list);` but in that case in `ComboBox` I am again getting contents of text file with all columns. Is there any way to use "Decorator" class? –  May 20 '15 at 14:16
  • Have to do some research and work on it before answering you. I'll try to do it as fast as I can. – Normal design May 20 '15 at 15:03