-1

I got problem whit two dimensional array JTable refresh. The JTable values change, but they only refreshes when i mouse-click on every row in the JTable. Is there a way to refresh JTable normally, or something like reload JTable, or dose it need specific table model?

JTable is filled like this:

while(rs.next()){   
                rows[row][col] = rs.getString("Level"); col++;
                rows[row][col] = rs.getString("study"); col++;
                rows[row][col] = rs.getString("1pn"); col++;
                rows[row][col] = rs.getString("2pn"); col++;
                rows[row][col] = rs.getString("1pk"); col++;
                rows[row][col] = rs.getString("2pk"); col++;
                rows[row][col] = rs.getString("summ"); col++;
                rows[row][col] = rs.getString("teori"); col++;
                rows[row][col] = rs.getString("p-d"); col++;
                rows[row][col] = rs.getString("ind"); col++;
                rows[row][col] = rs.getString("gat"); col++;
                rows[row][col] = rs.getString("pav"); col++;
                col = 0;
                row++;

And JTable is changed on comboBox change:

                            while(rs.next()){
                                rows[row][col] = rs.getString("Level"); col++;
                                rows[row][col] = rs.getString("study"); col++;
                                rows[row][col] = rs.getString("1pn"); col++;
                                rows[row][col] = rs.getString("2pn"); col++;
                                rows[row][col] = rs.getString("1pk"); col++;
                                rows[row][col] = rs.getString("2pk"); col++;
                                rows[row][col] = rs.getString("kopa"); col++;
                                rows[row][col] = rs.getString("teori"); col++;
                                rows[row][col] = rs.getString("p-d"); col++;
                                rows[row][col] = rs.getString("ind"); col++;
                                rows[row][col] = rs.getString("gat"); col++;
                                rows[row][col] = rs.getString("pav"); col++;
                                col = 0;
                                row++;
                            }
Mr.Pengu
  • 107
  • 1
  • 2
  • 10
  • 1
    1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). Hard code some data to replace the DB. 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is a `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Feb 21 '15 at 11:15
  • 1
    BTW - Use a [`DefaultTableModel`](https://docs.oracle.com/javase/8/docs/api/javax/swing/table/DefaultTableModel.html) and this becomes a lot simpler. – Andrew Thompson Feb 21 '15 at 11:18
  • You're going to have to _tell_ the `JTable` that you've changed the data. – Boris the Spider Feb 21 '15 at 11:19
  • @Andrew Thompson lots of stuff i can't do in DefaultTableModel – Mr.Pengu Feb 21 '15 at 11:20
  • 2
    What exactly can you do with a `[][]` that you cannot do with a `DefaultTableModel`? You do know that the `JTable` uses `DefaultTableModel` by default right? Clue's in the name... – Boris the Spider Feb 21 '15 at 11:21
  • First of all it's easier to edit the cells, previously'i I couldn't get this far whit code... And I Know it uses it as primary, but i changed to two dimensional array – Mr.Pengu Feb 21 '15 at 11:23
  • What @BoristheSpider said. Now *..where's that MCVE?* – Andrew Thompson Feb 21 '15 at 11:23
  • [`DefaultTableModel.setValueAt`](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html#setValueAt(java.lang.Object,%20int,%20int)) is more difficult that poking the array directly? Incidentally, _that_ method will inform the `JTable` that you changed the data. – Boris the Spider Feb 21 '15 at 11:25
  • For me it's more difficult because ther aren't lots for materials on DefaultTableModel – Mr.Pengu Feb 21 '15 at 11:27
  • 1
    Sorry, that's just a lie! There's a [massive tutorial](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) from Oracle themselves to begin with. – Boris the Spider Feb 21 '15 at 11:28
  • Yeah whit static values that's inputted already in the program, that is useles – Mr.Pengu Feb 21 '15 at 11:30
  • Where date comes from make no difference. – Boris the Spider Feb 21 '15 at 11:31
  • Actually is, is it static or dynamic, it's a big difference – Mr.Pengu Feb 21 '15 at 11:33
  • 1
    Not really. Start by recreating the `Model` each time you change the data - that's the beginner's approach. Fully understand how it works. Then begin to experiment with updating. You are trying to run before you can even stand - your comments show that very clearly. – Boris the Spider Feb 21 '15 at 11:34
  • Well of course because i'm just learning and no one can't give a help at this, that oracle DefaultTableModel page doesn't give me nothing, that i need i can't find there or is more difficult, so i'm using Arrays – Mr.Pengu Feb 21 '15 at 11:41

1 Answers1

2

I got problem whit two dimensional array JTable refresh

That statement makes no sense. A JTable uses a TableModel to store the data. Maybe you have a custom TableModel that uses a two dimensional Array to hold the data. You should never ever update the data storage directly.

First of all it's easier to edit the cells,

No it isn't. If you want to change the data in the TableModel then you use the setValueAt(...) method of the TableModel. The TableModel will then notify the table that the data has changed and the table will repaint the cell.

lots of stuff i can't do in DefaultTableModel

I doubt it. The DefaultTableModel is completely dynamic. You can:

  1. easily edit cells
  2. delete rows of data
  3. add rows of data
  4. clear all the data from the model.

In your case you are doing a new query so I would suggest:

  1. you use the setRowCount(0) method of the DefualtTableModel to clear the data.
  2. As you process each row in the ResultSet you can create an Array to represent a row of data then use the addRow(...) method of the DefaultTableModel to add each row of data.

The DefaultTableModel is not perfect, but it is a good place to start until you understand how to use a TableModel.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Let me give you a hint, every one is different something for you is easy for someone it is hard and the other way around. Somehow setRowCount(0) doesn't work and breaks everything, only if replace 0 whit 1 then it works – Mr.Pengu Feb 21 '15 at 16:41
  • @qaetosh `Somehow setRowCount(0) doesn't work and breaks everything` of course it works, I use it all the time. The problem is with your code, because you still don't understand the concept of how to use a TableModel with a table. `Let me give you a hint,` - actually I'll give you a hint. It is easier to use code that has already been written then it is to rewrite the code yourself. It if was easy to do it your way you wouldn't need to ask a question. So take the time to understand how to use the DefaultTableModel first BEFORE making a judgement. – camickr Feb 21 '15 at 17:47
  • don't be so arrogant, makes me sick. I know the concept and have seen the examples of that, so that thing doesn't work as it needs to, so it is limited, it isn't universal – Mr.Pengu Feb 22 '15 at 10:12
  • @qaetosh The problem is your lack of understanding. There is nothing in your question indicating the DefaultTableModel will not work. Your unwillingness to listen to the knowledge provided by multiple people means you won't solve your problem or learn anything new. You may have heard the quote "you can lead a horse to water buy you can't force it to drink". You have been given good advice, we can't force you to use it. – camickr Feb 22 '15 at 21:18
  • your blind or what? I just said that i know the concept so it means that i have looked at the DTM and have used it instead my code but you to arrogant to understand it... i'm here to get help not get criticism from such an arrogant person, that can't read the full context of my message. And btw the oracle page of the DTM is crap... a good person not like you gived me web-pages where i can get info about DTM. If you can't assist of help and only criticisise then please don't write nothing because no one wants to know that you are just arrogant person, that can yell it works, prove it – Mr.Pengu Feb 23 '15 at 08:18
  • @qaetosh, you should spend more time reading the suggestions instead of being rude to people trying to help you. I (and others who have responded) have read the context of you message. You have completely ignored our advice thinking that you know better. We have introduced you to methods like, `setValueAt(...)`, `setRowCount(...)`, `addRow(...)`. Being able to programs means being able to read the API. You will not find a tutorial that does exactly what you want for every possible requirement. – camickr Feb 23 '15 at 16:30
  • i'm thinking that you can't read and can't see some things... if you are to blind fine by me, but please stop spamming in stackoverflow, if you got anything to say pm, and i don't get why you even keep spamming that i already told you or you told me it's like you got some social issues, if you got some then go to doctor not to express your problems on others. And again you yell about what i told you, if it works then prove it, it is pointless to just yell, if you haven't got that yet. – Mr.Pengu Feb 24 '15 at 23:13
  • Ignored advice? please show me the place where i ignored it? I searched for DTM and i'm using it where is the ignore in that? That the Oracle page doesn't have the basics not my fold, i need the pure basic level if you didn't notice it, and for showing the static values as example i think is garbage. And i still think that arrays are better then DTM. In DTM it's easier to refresh, because it does it automatically, and making Subheaders, because whit arrays i can't do it. Evrithing else so far is the same, even easier in arrays. As a gamer i can say haters gona hate – Mr.Pengu Feb 24 '15 at 23:24
  • And whit your useless criticism i forgot to thank @Andrew Thompson for the advice and for showing SSCCE and MCV, thank you very much Andrew Thompson – Mr.Pengu Feb 24 '15 at 23:27
  • @qaetosh, `i forgot to thank Andrew Thompson for the advice...` you still haven't posted a SSCCE. `please show me the place where i ignored it?` everywhere. Every person who has responded has suggested you should use the DTM. `And i still think that arrays are better then DTM.` - that comment makes no sense. You still don't understand the difference between a TableModel and a data structure. An Array just holds data, it has nothing to do with a table. A JTable needs a TableModel to display data. – camickr Feb 25 '15 at 00:29
  • `if it works then prove it` - I did prove it. I told you how to use the forum properly by actually reading other postings on the forum instead of expecting people to spoon feed you the answer. I even told you what posting to read. Once again I can't force you to read the answer. If you made more of an effort to actually listen to all the advice given (instead of making unnecessary comments), you would have had your problem solved long ago. – camickr Feb 25 '15 at 00:38
  • you even don't get what to prove... and said that you proved it, don't get your logic... why should i post SSCCE? you can't read or what i don't get your problem? I told you i went to DTM, so why should i place SSCCE here, if i changed, how to say... my style of work. Well btw... from one side Arrays are tables... because you can call two dimensional array a real table. You can call any array a table, only that table will have more or less dimensions. – Mr.Pengu Feb 25 '15 at 23:03
  • spoon feed the answer? You really are a .... I told i need the code? I asked for advice, did you see anywhere in my context that i need the answer as code? A asked for advice, not my problem that you can't read... or understand the context. And if you don't get what i was saying than in short terms read carefully now! I admitted my mistake if you didn't notice it i said that DTM is better then arrays only i like more working whit arrays then DTM. Is it hard to understand and read slowly to get the context? Next time to even say something, please read the whole text/message about 5 times. – Mr.Pengu Feb 25 '15 at 23:08
  • `if you didn't notice it i said that DTM is better then arrays` - actually you said: _`i still think that arrays are better then DTM.`_. I should not have to read your comment 5 times to understand the meaning. It should be clear the first time. ` I asked for advice` - and the advice was to use the DTM. Since the advice was used the answer should be "accepted" so we can close the discussion. – camickr Feb 26 '15 at 01:35
  • i suggest you to go and learn to read, because you can't even understand the context of the whole text, and you are so childish... can't even confess where you were wrong... For a programmer you aren't particularly smart and again i can guaranty that you won't even undesrtand this text context as always – Mr.Pengu Feb 26 '15 at 23:14
  • `can't even confess where you were wrong...` - there is nothing to confess. You should be using the DTM, that is all I have suggested. Since you stated you are using the DTM you have followed my suggestion so the answer should be "accepted". `i can guaranty that you won't even undesrtand this text context as always` - you are correct, I don't know why you keep commenting, you aren't saying anything useful. You have your answer. Instead of wasting your time with all these unnecessary comments, why don't you try answering some questions so you can help others. – camickr Feb 27 '15 at 01:12
  • well i won't accept you answer because there was some else how gave the idea faster, so why would you get the credit? You are too arrogant. `you aren't saying anything useful` haven't seen anything useful in your comments... only one comment that got useful stuff and that is it all the rest comments are showing how arrogant you are and how a big criticisiser you are. – Mr.Pengu Feb 28 '15 at 10:41
  • @qaetosh, you rejected the initial suggestion to use the DTM because you didn't understand the API and you said `...so i'm using Arrays`, so you rejected the answer given.I could have continued to let you do things the wrong way, but my answer was more specific explaining the methods of the DTM that you needed to use and was intended to force you to take a second look at the DTM. The answer given was to point you in the right direction. It worked because you since commented you are using the DTM. So my persistence paid off even though you tried your best to ignore the advice give. – camickr Feb 28 '15 at 16:07
  • you are to arrogant... i sawed your advice after i used DTM, so you still get no credit, that you posted. And your advice is misleading, because some things work only in some occasions, so your answer isn't actually good... so if some new to this would saw your answer and would use all the things that you mentioned he would be stuck. So stop spamming, or fix your re-work your answer and maybe then i could accept it if it would be good. – Mr.Pengu Mar 01 '15 at 18:28
  • @qaetosh, `because some things work only in some occasions,` - you say that but you have not actually stated what situation. Without posting a proper `SSCCE` demonstrating the problem we can only assume the problem is most likely your code since you don't understand the concepts of how to use a TableModel properly. `re-work your answer` - there is nothing wrong with my answer. All the suggestions are correct and valid and have been made dozens of time before. – camickr Mar 01 '15 at 19:46
  • please before commenting read all the comments before and re-think your comment answers – Mr.Pengu Mar 03 '15 at 16:31
  • @qaetosh, I have and have nothing to change. Instead of wasting your time with ridiculous comments, spend your time helping other an you will learn how to ask/answer questions. You only learn when you do something. – camickr Mar 03 '15 at 16:35
  • before you write this advice, please do it your self you got some lack at it – Mr.Pengu Mar 04 '15 at 10:37
  • @qaetosh `before you write this advice, please do it your self ` - I do it daily, so yes I know what I'm talking about. You on the other hand have never done it, so you have no idea what you are talking about. `you got some lack at it` - the majority of other people would disagree with you: http://stackoverflow.com/tags/swing/topusers. Until you become an active member of the forum you really don't know what you are talking about which is made very clear by your last comment. – camickr Mar 04 '15 at 15:46
  • stop being arrogant ... i see that you don't get a thing... please read some psihilogy books, then say something – Mr.Pengu Mar 05 '15 at 19:06
  • @qaetosh `i see that you don't get a thing` actually you don't get a thing. I just proved that my answers/comments have helped thousands of people. Show me where your comments/answer have helped anybody? `please read some psihilogy books,` - try reading and answering some questions. Then maybe your comments will make some sense. Right now I have no idea why you leave these comments since you have nothing to say. All you do is call people names and that is not very productive. – camickr Mar 05 '15 at 21:44
  • sorry but i can't stand this, you are like a child.... please behave as grownup as you are... `Right now I have no idea why you leave these comments since you have nothing to say` you are the one who spamms here btw... I asked you to stop spamming but i guess you didn't se that or didn't get that – Mr.Pengu Mar 06 '15 at 11:28
  • @qaetosh, Take a look at two of my answers from yesterday: http://stackoverflow.com/a/28893076/131872 and http://stackoverflow.com/a/28892920/131872. This is the normal interaction with people as has been demonstrated thousands of times over the years. I stand by my record. I wake up search the forum for question where I can help people. I am a productive member of the forum community. I am not spamming. I gave, and continue to give, valid and correct advice. There is no need for you to reply or make any comment unless you have something positive to say.Be a positive member of the forum. – camickr Mar 06 '15 at 15:57