KirkPatrick 28 Junior Poster

I can't see the close in your posted code, but it sounds like that needs to be moved out of the loop as well.

Aw you are correct. Thank you. I left my close inside that loop without realizing it.

As for leaving 1 copy - well, yes, maybe. But the open/close within the loop is definitely wrong, so fix that first & see what happens.
Maybe its writing a blank/empty line in its last pass???

I do find it rather odd how it left my file modified without actually adding something to it though.


Thank you to all three of you who posted. Its always nice when one can get something working correctly. And much easier to fix problems with multiple people helping out

//for the meantime I am going to leave this unsolved to give BestJewSinceJC a chance to reply about making them into methods. I had tried this before resorting to putting it altogether and didn't have much success.

If he would like to give it a shot he can post it, if not just post saying so and I will mark it as solved.

New Questions

1.
As you can see when I split the file I give it how many columns it was have. Well I just noticed some of the new data I have doesn't always have 3, sometimes it has 2 or even 1. How can I fix this so that it doesn't …

KirkPatrick 28 Junior Poster

com.csvreader.CsvWriter writer = new ...

seems to be inside your innermost loop, so you re-open (and thus overwrite) the file contents every time you go thru the loop.
I think you should open the stream just once, at the beginning.

By moving the new writer up to the top it leaves me with a bunch of the following exceptions:

java.io.IOException: This instance of the CsvWriter class has already been closed.

I have a feeling that for the most part, the way that i had it was correct. Because when I print out the arraylist they have the correct information in them, it just never saves it to the file correctly.

So even if it was constantly overwritten, shouldn't it at least leave one copy when I close the program?

KirkPatrick 28 Junior Poster

I didn't read your code very carefully, but this sounds like a problem I experience from time to time. You need to close the file when you're done writing to it. Otherwise you will just create it, but it will remain empty.

You are correct in stating that I don't close my writer. I added writer.close() and it still doesn't seem to be saving to a file :/

However I put in an arraylist just to see if the data was catching and the arraylist gets the proper information. So the problem remains that it just isn't saving to a file.

By the way, when I check the file properties it says that the file was last modified when I last run the program. How can it be modifying the file if it doesn't save the information? My problem seems to be very similar to yours, I wish it was as simple as closing the writer like you mentioned

Any other ideas?

It also looks like you need to separate your logic into methods that each do one specific task. Right now your code is pretty hard to read and understand.

I tried making separate methods but when I did that I couldn't find a proper way to make it compare values[0] and info[0]

All the code is suppose to do is:

  • Read file1.csv
  • Read file2.csv
  • Check if column 1 from file1.csv is the same as column 1 from file2.csv
  • If they are the same, …
KirkPatrick 28 Junior Poster

Alright I am having my program read two text files (in csv format) and once it reads them, I am wanting it to do a check to see if two columns match, if they do I want it to write it to a new file.

I have coded it, but am having problems getting it to function properly. I have set up print statements throughout the code (taken them out in the code I am posting so you don't get thrown off) and fixed some small issues here and there but still can't quite seem to find why its not working.

public class BeanInfoMerge {

    private String itemA = "";
    private String itemB = "";
    private String itemC = "";
    private String dataA = "";
    private String dataB = "";
    private String dataC = "";


    public BeanInfoMerge() throws FileNotFoundException {

        BufferedReader reader = new BufferedReader(new FileReader("C:\\file1.csv"));

        ArrayList<String> list1 = new ArrayList<String>();

        try {
            boolean bHeadersDone = false;
            while (reader.ready()) {
                String headerInfo = reader.readLine();

                if (!bHeadersDone) {
                    if (headerInfo.contains("Option 3")) {
                        bHeadersDone = true;
                    }
                }
                else {
                    String[] info = list1Info.split("," , 3);
                    itemA = info[0];
                    itemB = info[1];
                    itemC = info[2];
                    
                    if(!list1.equals(0)) {

                        list1.add(itemA);
                        list1.add(itemB);
                        list1.add(itemC);

        BufferedReader reader2 = new BufferedReader(new FileReader("C:\\file2.csv"));
        ArrayList<String> list2 = new ArrayList<String>();


        try {
            boolean bHeadersDone2 = false;
            while (reader2.ready()) {
                String headerInfo2 = reader2.readLine();

                if (!bHeadersDone2) {
                    if (headerInfo2.contains("Characteristic 3")) {
                        bHeadersDone2 = true;
                    }
                }
                else {
                    String[] values = hostInfo.split("," , 3);
                    dataA = values[0];
                    dataB = values[1]; …
KirkPatrick 28 Junior Poster

I'll explain what I am doing so that you don't have to read all the above posts.

I have 6 columns total (5 normal and 1 that is a jComboBox). The jComboBox is more for looks than actual purpose. The info that is in the jComboBox is taken from textfields after they are filled in.

The only reason I have the jComboBox is so that it doesn't take up an extra 10 columns. This way the user can just click on the cell and it will drop down and show all the info in it. However when I save the info, it only saves the selected item. I had attempted to do a for loop to get around it but it didn't quite work.

So I'm currently stuck on how to save that data that is in the jComboBox. I don't want just the selected item, but all of the items in it.

Ezzaral has been a pretty big help thus far, however, we have yet to come up with a decent solution for it.

KirkPatrick 28 Junior Poster

Okay I'm going to bump this as I'm attempting to take a slightly different approach.

Lets say I read file one in which i also split the data into columns and then save it to an arraylist like so:

ArrayList<String> arrayList1 = new ArrayList<String>();

String[] values = viewObject1.split("," , 3);
                    dataA = values[0];
                    dataB = values[1];
                    dataC = values[2];

                    if (!dataC.contains("[n/a]")) {

                        arrayList1.add(dataA);
                        arrayList1.add(dataB);

and then I read my second file and do the same thing:

ArrayList<String> arrayList2 = new ArrayList<String>();

String[] values = viewObject2.split("," , 4);
                    itemA = info[0];
                    itemB = info[1];
                    itemC = info[2];
                    itemD = info[3];


                    if (!itemD.contains("[n/a]")) {

                        arrayList2.add(itemA);
                        arrayList2.add(itemB);
                        arrayList2.add(itemC);

And the next thing I want to do is see if dataA and itemA are the same thing, but thats where I am confused because as far as I know it would compare the lines of each arraylist instead of columns correct?

I can't write these to separate arraylists for each column as the info in each file has to line up.

The ultimate goal is that if dataA and itemA match up, I will write each line that goes with each file to a new file. Thus combining them and saving it.

Any ideas or help?

KirkPatrick 28 Junior Poster

To be honest I just want to have the last column have the ability to drop down and show the values in it when clicked and then have an easy way to save it. Whether it be a jComboBox or some other way.

I'm just not too familiar with my options, which is why I came here :]

Do you know any alternatives I could take?

KirkPatrick 28 Junior Poster

Not to hijack this thread, but is there a way to do what his original question is asking? Reading two files at the same time? The reason I ask is because I am wanting to compare two text files and then write the new information to one file

KirkPatrick 28 Junior Poster

So there wouldn't be the possibility of doing something simple such as the following code?

(I haven't test the code yet, just logically thought of how it could possibly be done and threw it together)

int itemCount = comboBox.getItemCount();
        ArrayList<String> list = new ArrayList<String>();

        for (int j = 0; j < itemCount; j++) {
            String abc = (String) comboBox.getItemAt(j);
            list.add(abc);
            System.out.println(list);
        }

This obviously would just print it out, but am wondering if this same technique could be used for writing it to a file?

KirkPatrick 28 Junior Poster

I have it on a scroll pane but thats for vertical purposes instead of horizontal.

The reason I don't want it horizontally is because the user would have to scroll over every time they want to add some new data or edit data.

By having it a jComboBox one can just click on the cell and view it quickly and move onto the next step of deciding whether to edit it or add new information.

To keep it clean, simple, and efficient for the user; I am looking for a way to write all the information contained in the cell with the jComboBox into the same file as the information of the columns next to it.

I take it there isn't a way to do this?

KirkPatrick 28 Junior Poster

Well I am trying to cut out some of the columns because 15 just makes it look messy. So condensing it to 6 total columns and the last one being a jComboBox it allows me to have all the information there and when the person wants to view the extra data they just have to click the jComboBox to see the info.

This lines up all the information that I need and after lining it up, I want to save it to my csv file so that I can load it whenever I would like and have the table fill up automatically.

So instead of just writing the selected value I am wanting to know how to save all the values in the jComboBox.

The jComboBox is just there to conserve space and make the GUI present itself cleaner. The information it contains is just as important as the other info which is why I need to have a way to write all the values to text and not just the one that is selected.

I have been searching for ways to do so and I haven't really found a good way of going about it.

If you don't want all of the values visible at once as a table, I would personally probably go with a list or second table that loads the additional entries as the primary table selection changes.

I want all the values visible, just in a 'clean' atmosphere so that …

KirkPatrick 28 Junior Poster

Alright then I am set and this issue is solved.

Thank you for all your help, it is much appreciated

KirkPatrick 28 Junior Poster

I'll explain my reasoning then, (perhaps there is a better option than a jComboBox that I'm not aware of)

I have a table in which I have 5 columns and another table with 10 columns. Instead of having two tables, I want to combine them without taking up 15 columns. Thus, I make a jComboBox to display the other values from the table with 10 columns.

Since the data in the second table are all small strings its easy to click the jComboBox and view what has been put in there. However, I want to save all of them to a file along with the other 5 columns.

When I do this:

for (int i = 0; i < tableModel1.getRowCount(); i++) {
                    String itemA = (String) tableModel1.getValueAt(i, 0);
                    String itemB = (String) tableModel1.getValueAt(i, 1);
                    String itemC = (String) tableModel1.getValueAt(i, 2);
                    String itemD = (String) tableModel1.getValueAt(i, 3);
                    String itemE = (String) tableModel1.getValueAt(i, 4);
                    String itemF = (String) tableModel1.getValueAt(i, 5);

                    writer.writeRecord(new String[] { itemA, itemB, itemC , itemD , itemE , itemF } );

                    }

writer.close

For:

String itemF = (String) tableModel1.getValueAt(i, 5);

It only writes the value that is selected, I want it to write down however many values I have in the combobox.

Is there a way to do that? Or is there another editor that is a better choice?

KirkPatrick 28 Junior Poster

I have fixed the timer issue by putting a timer around my method that sets off the update for the beans.

Which leads me to my next question, is it bad practice to have a timer within a timer?

KirkPatrick 28 Junior Poster

Sorry for the late reply, I see what you're saying now. So now my question has changed a little

Is there a way to write all the values of the jComboBox to a file instead of just the one that is selected?

KirkPatrick 28 Junior Poster

Alright I actually found out a few problems.

As for the ArrayIndexOutOfBounds exception, you seemed to be on the right track. The reason why I couldn't find the problem, well the only thing I can come up with is that when deleting the data it must have stopped the line after the last piece of data. Leaving a new line ('/n') as a line of data instead of going all the way up to the previous line. So that part is fixed and I'm no longer getting that exception.

That was one of those tedious ones where you have to pay very close attention to whats happening to catch.

I was also right that I had to clear my arrayLists. In order to clear beans and add beans back correctly I had to clear my 2 arrayLists and the panel that I add the bean to in my timer.

Now I'm stuck with one problem, it only updates my beans through 2 cycles :s after that the timer seems to keep going (i have it displaying the countdown) however when it hits the time it is supposed to update the beans on the 3rd cycle it doesn't do it.

Any idea what the problem could be there? If you need me to post source of my method with the timer let me know

KirkPatrick 28 Junior Poster

Alright I'll keep digging deeper and question each part.

Also note I edited my post above probably as you were posting. I'm pointing it out because do you think that it could be the arrayList?

KirkPatrick 28 Junior Poster

The thing is that its the exact same information, all I'm doing is removing parts of it or adding parts back to test if it works. When I remove or add data I do it be lines.

However each line contains the right amount of commas.

Here is something odd, when I printed out beanInfo it gave me a full list of the information in the csv file. The reason this is weird is because I removed half of it right before the timer set it to reading the info in the csv.

So it works when more information is added to the file but not when information is taken away from the file.


*Edit*

However when I just print out infoB it gives just the information that is actually in that column.


Do you think it might be due to my arrayList perhaps already having that information added to it and maybe I need to clear it after or before each timer scan?

KirkPatrick 28 Junior Poster

Hmm I can see it bolded, but instead of doing it that way what tag do I use to show the number next to each line?

The code that I bolded was:

infoB = values[1];

To answer your question, I have the beans on a loop. Basically it takes the length of the result arraylist and adds a bean for every line. In short for every 'infoA' it will place a bean on the main program.

Not sure if that completely answers your question, if not I didn't fully understand it.

KirkPatrick 28 Junior Poster

Alright my program is giving me a hard time and I will have to briefly explain what it does and when the exception is thrown to give you a better understanding.

My program goes off and reads a csv file, once it reads it, it adds the information to my bean and then displays the bean on my main program. The csv file is constantly changing with what is inside it, thus I set a timer on the part where my program reads the csv file.

Here is an example of where the exception is not thrown. Lets say in my csv file there are only 2 lines of data. Then I change the csv file to contain 6 lines of data, the main program updates the beans and runs fine without exceptions or warnings.

Here is an example of where the exception is thrown. When the csv file starts out with 6 lines of data and then I remove some lines (any amount) then the main program does not update my beans and throws this exception: ArrayIndexOutOfBoundsException: 1


My csv file always contains only 3 columns of information, however the lines (rows) will vary.

I will post the source where this exception happens. And hopefully you guys will be able to help out.

public ParseResult[] parseResult(BufferedReader reader) throws FileNotFoundException, IOException {
    

        ParseResult[] out;
        ArrayList<ParseResult> infoFound = new ArrayList<ParseResult>();
        
        try {
            boolean bHeadersDone = false;
            while (reader.ready()) {
                String beanInfo = reader.readLine(); …
KirkPatrick 28 Junior Poster

The reason I am asking is because out of a normal jTable cell you can get that info and save it to a file simply by doing this:

String a = (String) tableModel1.getValueAt(i, 0);
writer.writeRecord(new String[] { a } );

However when I tried that with the column that was a jComboBox it saves the whole array of objects as "" (blank) instead of the information that was entered into the jComboBox.

So would my example from my previous post work/be logical?

KirkPatrick 28 Junior Poster

So then appending wouldn't be practical anymore since I need to add to the end of certain lines.

Instead something like this could work? (typed it out on a whim) :

BufferedReader reader = new BufferedReader(new FileReader ("C:\\myFile.csv"));

        try {
            boolean bHeadersDone = false;
            while (reader.ready()) {
                String beanInfo = reader.readLine();
                
                if (!bHeadersDone) {
                    if (beanInfo.contains("Ping")) {
                        bHeadersDone = true;
                    }
                } 
                else {
                    String[] values = beanInfo.split("," , 3);
                    dataA = values[0];
                    dataB = values[1];
                    dataC = values[2];

                  // textField.getText() is data1 
                  // add whatever is in column 2 to my program GUI via textfield
                  // add whatever is in column 3 to my program GUI via textfield

		    if (dataA.equals(textField.getText())) {  		
			textField1.getText(dataB);			
			textField2.getText(dataC);			

javax.swing.table.DefaultTableModel tableModel = (javax.swing.table.DefaultTableModel)jTable1.getModel();

tableModel.addRow(new Object[]{textField1.getText(), textField2.getText(),textField3.getText(),textField4.getText(),textField5.getText(),textField6.getText(),textField7.getText(),textField8.getText()});

			com.csvreader.CsvWriter writer = new com.csvreader.CsvWriter("C:\\myFile.csv");

			writer.writeRecord(new String[]{"dataB", "dataC", "data2", "data3", "data4", "data5", "data6", "data7", "data7"});
			}
                  
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

Sorry for how bad the code got squished, however its due to the forum :/

KirkPatrick 28 Junior Poster

Posting in code tags would help and posting what line the error is in will also be helpful to those answering.

The error is saying that you either haven't imported that package, or are trying to use one that doesn't exist.

Thats just my guess :) as I'm fairly 'new' to it as well

KirkPatrick 28 Junior Poster

Alright that makes a bit more sense and cleared some things up, however before I put it all into action, will it be able to put that data at the end of certain lines? Or just the end of the file?

I assume using a simple if statement should allow me to do the comparison but not quite sure how it would add to that line exactly

KirkPatrick 28 Junior Poster

Can you elaborate a bit? I'm not quite sure I follow you.

I took a look into FileOutputStream, I took note on a couple of things:

  • FileOutputStream is meant for writing bytes of raw data such as images, FileWriter is preferred for streams of characters
  • Bytes will be written to the end of the file

I figure the first listed object isn't a big deal. And the second listed object is more than likely solved with the way you explained it. I don't quite follow, if you don't mind explaining it a little I would appreciate it.


Further Info About My Program (may or may not be useful) :

My original data is saved by an external (third party) program. My secondary data will be coming from a class I have created (basically taking information that I have on a table)

So currently file1.csv looks something like:

Col = Column

[Col 1] [Col 2] [Col 3]

dataA, dataB, dataC,
dataD, dataE, dataF,

and the data from my class (from the table) saves to a separate file2.csv file like so:

[Col 1] [Col 2] [Col 3] [Col 4] [Col 5] [Col 6] [Col 7] [Col 8]

data1, data 2, data 3, data4, data5, data6, data7, data8,
data9, data10 , data11 , data12, data13, data14, data 15, data16,

If [Col 1] (from file1.csv) is equal to [Col 1] in file2.csv I want the information added to that line in file1.csv

KirkPatrick 28 Junior Poster

Question One (50% Solved):
Alright well it shouldn't cause a problem when writing it to a csv file would it? (meaning when saving it, would it also save a blank value?)

If it doesn't save a blank space then I shouldn't have a problem.

Question Two (Solved) :
I solved that one. I added the if statement to when I was adding the info to the arraylist instead which worked fine.

Question Three (Not Solved) :
For question three I am still a bit confused. How to I get the information from the jComboBox.

Would something like the following work? (I'm not sure how many items will be in the jComboBox at any given time):

// using CsvReader/Writer so that it can be written to a file

com.csvreader.csvWriter writer = new com.csvreader.CsvWriter("C:\\"); 

for (int j = 0; jComboBox.getItemCount(); j++) {
	String itemA = (String) jComboBox.getItemAt(j); 

	writer.writeRecord(new String[] { itemA } );
}

writer.close
KirkPatrick 28 Junior Poster

I forgot to say thank you for this bud.

I had implemented it and forgot to get back to the thread and post how it went.

The only problem I saw was that when I click my insert data button (to the table) the first index is blank until it is clicked. For the most part it won't be clicked.

Question 1:
Will this cause a problem when I write the data to a file?

Meaning will it write a blank spot for index[0]? Or will it skip that and only write the other indexes?

Another note that I saw was that it also added indexes that might not have been filled out, and I was having problems throwing an if statement in there to filter it out due to the loop:

for (int i = 0; i < list.size(); i++){
            if (!list.equals("")) {
                comboBox.addItem(list.get(i));
            }
           comboBox.addItem(list.get(i));
        }

The above won't work because they are incompatible types obviously.

Question 2:
But what would be a way to solve this issue?

Question 3:
And lastly, when I want to use tableModel.getValueAt() how exactly would I do that for a drop down menu (being as its the 5th column in a table?

Previously I had used getValueAt() like so:

for (int i = 0; i < tableModel1.getRowCount(); i++) {
                    String a = (String) tableModel1.getValueAt(i, 0);
                    String b = (String) tableModel1.getValueAt(i, 1);
                    String c = (String) tableModel1.getValueAt(i, 2);
                    String d …
KirkPatrick 28 Junior Poster

I'm using CsvReader/CsvWriter to read and write my data to csv files. And I have a question about appending lines of data to other certain lines.

Lets say I have some data already saved, now I open my other program which has more info that I would like to add to that same line.

This program has textfields that add to a table, then when I click save it takes the information from the table and saves it to a separate csv file. I'm planning on making it save that info in the first csv file so that I can read all the information at one time.

So in short, is there a way to make my program append (add on) my new data to the old csv file in a specific lines?

When I read the info from file1.csv, I split it into columns like this:

String[] values = viewObject.split("," , 3);
                    dataA = values[0];
                    dataB = values[1];
                    dataC = values[2];

Perhaps by throwing something like the following in?

if (dataAtextField == dataA) {
   // make csv writer append to that line somehow
}

Any ideas or helpful hints?

KirkPatrick 28 Junior Poster

Thank you for all your help, I have modified it slightly and it works exactly how I was wanting.

Much appreciated :)

-Solved-

KirkPatrick 28 Junior Poster

Once it hits 0, I want it to delay for a brief amount of time (2 seconds max) and then restart at 60 and count down again.

Basically what it will be doing is when it hits 0, it will refresh my GUI. I am having some beans load information from a file and that file will be updated every so often. So I want it to update it every minute in case there is new information.

KirkPatrick 28 Junior Poster

I'm double posting so that you see there is another post in here (you can merge them once you see it if you'd like)

I'm a bit confused about this, I can't get it to use that if statement that was created. It counts all the way down to 0, but sticks on 0.


I tried: cdt.reset(); after cdt.start(); and it doesn't reset it. So I then tried.


CountdownTimer cdt = new CountdownTimer();
cdt.start();

I tried: cdt.actionPerformed(); but it says it can't be applied.


Sorry for all the questions, I just want to understand this as best I can, so that I don't have to question it later.

KirkPatrick 28 Junior Poster

I actually just realized that right before I read your post. My apologies for being confusing about the inner classes, I went ahead and made it a top-level class and tried bringing it in the way I posted above.

Thank you very much for all the help you provided me with. I'm going to test it out and will be back with an update shortly

KirkPatrick 28 Junior Poster

But my jLabel countdown is from that other class which would create the same problem just in reverse, wouldn't it?


One class is StatusBar, which contains my jLabel (countdown) and then the new class that was just created, CountdownTimer.

KirkPatrick 28 Junior Poster

Can you point me in the right direction on what I should search for in google or a tutorial on accessing components from inner classes? As google doesn't seem to give me any good sources.

Knowing this information would help me make my program look much cleaner. I had tried to find out how to access components from inner classes awhile back and I couldn't find any decent sources on how to get it to work.

I had tried something like:

CountdownTimer cdt = new CountdownTimer();
        String timerCounter = Integer.toString(counterLimit);
        countdown.setText(timerCounter);

And it tells me that it cannot find the symbol variable counterLimit

Thanks again

KirkPatrick 28 Junior Poster

If it is in a separate class then how would I get my jLabel to display it? (my jLabel displaying it is the most important part of it)

I had previously wanted to make it a separate class because it does make it much cleaner, but I didn't know how to go about making my jLabel display it from another class.

KirkPatrick 28 Junior Poster

Doing it that way would mean refreshTimer isn't accessible if I'm doing it correctly

ActionListener refreshListener = new ActionListener() {

            int counter = 60;

            public void actionPerformed(ActionEvent e)
            {
                counterLimit--;
                String timerCounter = Integer.toString(counterLimit);
                countdown.setText(timerCounter + "(s)");
                System.out.println(counterLimit + "(s)");
                
[B]                if (counter == 0) {
                    refreshTimer.stop();
                    refreshTimer.restart();[/B]
                            
                }
            }
            
        };
  
         javax.swing.Timer refreshTimer = new javax.swing.Timer(1000, refreshListener);
        refreshTimer.setInitialDelay(0);
        refreshTimer.start();

I do realize that it hasn't been declared in the actionPerformed, however when declaring it in there instead of outside it then has a problem with 'refreshListener'

I have a feeling I made a dumb mistake though, I appreciate your patience.

KirkPatrick 28 Junior Poster

Wouldn't there be a problem with the scope?

ActionListener refreshListener = new ActionListener() {

            int counterLimit = 60;

            public void actionPerformed(ActionEvent e)
            {
                counterLimit--;
                String timerCounter = Integer.toString(counterLimit);
                countdown.setText(timerCounter);
                System.out.println(counterLimit);
            }
        };

        javax.swing.Timer refreshTimer = new javax.swing.Timer(1000, refreshListener);
        refreshTimer.setInitialDelay(0);
        refreshTimer.start();
       
         if ([B][U]counter[/U][/B] == 0) {
            refreshTimer.stop();
            refreshTimer.restart();
        }

Where counter isn't accessible since it is outside of the scope where I defined it?

KirkPatrick 28 Junior Poster

That was the problem, initially I had intentions of adding it to a jButton but changed my mind and was just going to run it automatically but forgot to change it to do so.

You are correct about the syntax error, was in a bit of a rush and didn't notice it.

Anyways I went ahead and rewrote it to look like so:

ActionListener refreshListener = new ActionListener() {

            int counterLimit = 60;

            public void actionPerformed(ActionEvent e)
            {
                counterLimit--;
                String timerCounter = Integer.toString(counterLimit);
                countdown.setText(timerCounter);
                System.out.println(counterLimit);
            }
        };

        javax.swing.Timer refreshTimer = new javax.swing.Timer(1000, refreshListener);
        refreshTimer.setInitialDelay(0);
        refreshTimer.start();

It also doesn't stop at 0 like you said, I went ahead and tried to put a for loop in there instead of just counterLimit--; but that automatically makes it count much faster than a second.

Is there an easy way to make it stop at 0? I'm planning on making it reset to 60 once it hits 0.

KirkPatrick 28 Junior Poster

The data is acquired through two separate csv files (basically text files)

I'm having a problem of figuring out how to match them up, so that they can display properly.

My conclusion was perhaps a hashtable. Is there a better way than that?

KirkPatrick 28 Junior Poster

I'm having a problem with my countdown timer and am figuring that I'm missing something obvious.

All I'm trying to do here is get my jLabel to countdown from 60 to 0.

ActionListener refreshListener = new ActionListener() {
            int counterLimit = 60;
            public void actionPerformed(ActionEvent e)
            {
                javax.swing.Timer refreshTimer = new javax.swing.Timer(1000, new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    counteLimitr--; 
                    
                    String timerCounter = Integer.toString(counterLimit);
                    countdown.setText(timerCounter);
            
                }
            });
            refreshTimer.start();

        }
    };

Compiles fine, just doesn't countdown.

KirkPatrick 28 Junior Poster

Perhaps I can explain it better.

Right now I have my viewObject. Upon opening of the program, it gets information from file1.csv file and displays it. So we will say the file1.csv contains the type of fruit.

Once that type of fruit is displayed is there a way to make that a key so that my other getters/setters can pull the information from the hashtable and put it on my viewobject?

If not, my overall problem is that I have a third party program that picks up certain information and saves it to a csv, while I have another program that is like a database and saves the info to another csv file. And I'm not quite sure how to display the information together without it getting the wrong information. (If that makes sense)

KirkPatrick 28 Junior Poster

To keep track of the Fruit objects you could have a Hashtable with the Fruit's name as key and the actual Fruit instance as the value.

First of all thank you for your reply, you seem to be helpful in each of the threads I have posted. I've been a member and staff of multiple forums and it's hard to find people who are dedicated to helping. It's definitely appreciated, just thought I would share that with you.

As for my original question, I was sort of wondering how to do what I quoted above. As I have not yet toyed with hashtables, but read up on them. I kind of understand how they work, but don't feel I fully understand it. My viewObject has certain getters and setters which I am wanting to pull up the type of fruit and if that one is pulled up the other jLabels will get the shape, color, etc.

(fruit aren't what my program is about, but it was just an easier way of explaining the example)

KirkPatrick 28 Junior Poster

I'm having a problem deciding how to handle a situation. I'll briefly explain whats going on before I ask the question(s).

I have a viewObject (bean) and what it does is display information that is gathered. To keep it from getting too complicated I'll say I have 3 jLabels.

One jLabel (informationA) is gathered from a program that scans and saves to a file.csv.

jLabel two (informationB) and jLabel three (informationC) both are gathered from a database that is created by the user which is saved in file2.csv

To correctly have the information line up on this viewObject I am looking for a way to match them up.

[Question]
I was suggested to use a hashTable. So I looked it up and it seems fairly self explanatory, but was looking to see if someone could explain how to use it more specifically for my situation, or perhaps explain if there is a better solution.

If what I am wanting to do is still unclear I'll explain using an analogy. Lets say the first jLabel (informationA) is on the first object, I then want a certain informationB and informationC to be displayed on the same viewObject bean.

For instance lets say I have these strings:
[informationA]
Apple
Orange
Grape
Cherry

[informationB]
Round
Square
Triangle
Oval

[informationC]
Green
Orange
Purple
Red

Now obviously I want these to match up correctly so …

KirkPatrick 28 Junior Poster

Sorry I don't mean to bump this but seeing as it won't let me edit my last post I have to.

I think it has something to do with my for loop.

ArrayList<Mal> malList = new ArrayList();
        for (int i = 0; i < result.length; i++) {
            MyArrayList mal = new MyArrayList(result[i]);   
              
            malList.add(mal);

For some reason I believe that is what is creating all the extra space in between. I'm not sure how to fix this, but I at least know its the problem now.

gridBagConstraints.gridy = i;

By setting it to 'i' it should line them up right under each other every time a new bean is there. I believe its thinking the bean is bigger than it really is, like it has some sort of external padding. Any ideas?

KirkPatrick 28 Junior Poster

Sorry for the late reply as I have been out of town.

To clear things up a bit, I have my beans being displayed on a jPanel. When they display with the settings above (including weight) they are placed in the jPanel but they are spaced out to where you see about three times the space of the actual bean in between each bean.

I can post up a picture of it when I get on my other computer (will be a few hours from now so I'll just try and show you what I mean with text)

This is what it looks like at the moment:

[bean]
[jPanel]
[jPanel]
[jPanel]
[bean]
[jPanel]
[jPanel]
[jPanel]
[bean]

So basically instead of them being stacked under each other it leaves jPanel space in between them.

I would like to get them to display like so: http://img89.imageshack.us/img89/729/programcopy.png

KirkPatrick 28 Junior Poster

Thanks for the reply, however that leaves gaps in between each bean. (gaps where you could fit about 5 beans)

The funny thing is I tried creating separate panels with weights for x and y earlier and came up with something close to the result of what that gave me.

I've been messing around with those properties for awhile and can't quite figure out how to eliminate that space in between them (vertical space/y). I've also been reading through layout managers to see if there is one that might be more probable for what I'm doing. But so far the only ones that seem reasonable are the gridbaglayout/grid layout and possibly the box layout

Changing the weighty to less doesn't seem to help it and if the weighty is completely removed then the beans all go to the center instead of the northwest corner

KirkPatrick 28 Junior Poster

Hey guys, got another question for you.

I have a program that displays a new bean on my jPanel for each piece of data that it picks up from a file. I was wondering if there is an easy way to always have it display my bean to the northwest of the jPanel.

The thing is, I can get it to set my first bean in the northwest corner, but not the rest of the beans.

I've been messing around with gridbaglayout and I have my anchor set to northwest.

I am using netbeans which makes it fairly simple to see, but thus far I can't quite figure out how to get it to display the way I want it to.

Very briefly, this is how I'm getting the bean to display on the jPanel:

GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.gridx = 0;
            gridBagConstraints.gridy = i;
            gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
            basePanel.add(mvl, gridBagConstraints);

mvl is my bean.

Any suggestions or help is appreciated as always

KirkPatrick 28 Junior Poster

ah yes, that would make sense since I want it to continue going through each case below it without stopping. Thanks for informing me bud.

Anyways I think my problem was setting all my textfields to true above my switch statement. For some reason that was throwing it off. I might have to override it when doing a switch.

On a side note I went ahead and test some things out. (After going through it and put some print statements in on the switch statement which proved it to be working just not being able to over ride the setVisible(true) above it.

I did run into a problem though as I was testing it with if else statements. Since the textfields were automatically set true I had a problem with just trying to set certain ones true or false and had to make it specific for each text field like so:

if (jComboBox1.getSelectedIndex() == 0) {
        Dcc1TextField.setVisible(true);
        Dcc2TextField.setVisible(false);
        Dcc3TextField.setVisible(false);
        Dcc4TextField.setVisible(false);
        Dcc5TextField.setVisible(false);
        Dcc6TextField.setVisible(false);
        Dcc7TextField.setVisible(false);
        Dcc8TextField.setVisible(false);
        Dcc9TextField.setVisible(false);
        Dcc10TextField.setVisible(false);   
    }
    else if (jComboBox1.getSelectedIndex() == 1) {
        Dcc1TextField.setVisible(true);
        Dcc2TextField.setVisible(true);
        Dcc3TextField.setVisible(false);
        Dcc4TextField.setVisible(false);
        Dcc5TextField.setVisible(false);
        Dcc6TextField.setVisible(false);
        Dcc7TextField.setVisible(false);
        Dcc8TextField.setVisible(false);
        Dcc9TextField.setVisible(false);
        Dcc10TextField.setVisible(false); 
    }
...and so on

When trying to do it by just telling which ones to setVisible(true) or setVisible(false) it occasionally wouldn't update the GUI.

Using the switch statement also caused problems with the GUI. For example when index 9 was chosen it would display 10 textfields, then index 4 was chosen and it would show 5 textfields, and then choosing index 9 …

KirkPatrick 28 Junior Poster

Well I'm using netbeans ide so I figured actionPerformed took care of the listeners with the ActionEvent evt

public myApp() {
        initComponents();
        
        aTextField.setVisible(false);
        bTextField.setVisible(false);
        cTextField.setVisible(false);
        dTextField.setVisible(false);
        eTextField.setVisible(false);
        fTextField.setVisible(false);
        gTextField.setVisible(false);
        hTextField.setVisible(false);
        iTextField.setVisible(false);
        jTextField.setVisible(false);
        
    setTitle("MyApp");

    }
private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    
    switch (jComboBox1.getSelectedIndex()){
        case 9:
            jTextField.setVisible(true);
            break;
        case 8:
            iTextField.setVisible(true);
            break;
        case 7:
            hTextField.setVisible(true);
            break;
        case 6:
            gTextField.setVisible(true);
            break;
        case 5:
            fTextField.setVisible(true);
            break;
        case 4:
            eTextField.setVisible(true);
            break;
        case 3:
            dTextField.setVisible(true);
            break;
        case 2:
            cTextField.setVisible(true);
            break;
        case 1:
            bTextField.setVisible(true);
            break;
        case 0:
            aTextField.setVisible(true);
            break;
}
KirkPatrick 28 Junior Poster

Just making a post to note that I unchecked it from being solved (sorry for double post)

I just now got to implementing the switch statement and its not working either :/

Here is what I've done...

I set all my textfields to visibility false and then put in the switch statement in both the ActionPerformed and ItemStateChange and for some reason it just keeps my textsfields set to false.

It compiles and everything fine, just doesn't perform the action.

Oh and just an update as to my original question of the syntax not working (in my first post) it was because my syntax, just a stupid mistake. This fixes the error for the first one, however like the switch statement has no effect when something is selected in the combobox

if (jComboBox1.getSelectedIndex() == 0) {
        aTextField.setVisible(true);     
    }

Any ideas?