Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Jtable filled with JButtons each having their own ImageIcon from a SQL server  RSS feed

 
Joe Meany
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am developing an application that will populate a JTable with jbuttons. Each one of these jbuttons will have an imageicon of the picture of a person.

I am having trouble--i believe-- with the jtableButtonRenderer.
Here is what i believe my problem code looks like.



I realize that the code is slightly messy, but i have been pulling my hair out for the past week trying to get this to work correctly.
As of now, each JButton receives the same imageIcon, and i understand that the way i have it set up now will always allow for this, i am not sure how to go about doing it another way.

Any help would be much appreciated, and thanks for your time.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First off, and unrelated to your stated problem:
  • You have a TableCellRenderer that extends JButton but is never used as a JButton, instead constructing another JButton ...
  • ... in the getTableCellRendererComponent(...) method, which is a bad place to construct components ...
  • ... and also a terrible place to perform disk I/O


  • I recommend that you populate the table's model with Images or ImageIcons instead.
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I agree with you completely. The JButton addition in TableCellRenderer was a recent attempt to solve a problem using an example i found online....admittedly should have omitted that, but i wasnt thinking.

    Also, i was not initally constructing components within getTableCellRenderer, but a last ditch ever to make some progress(a certain level of frustration is setting in ).

    And, regarding your last statement, are you suggesting that instead of populating my JTable with jbuttons that have imageicons atop them, i populate the JTable using imageIcons only and then using, say, a mouseListener to complete actions.

    By the way, thank you for your response and help, and i apologize for my awful code at the moment.
     
    Rob Spoor
    Sheriff
    Posts: 20893
    81
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Putting buttons in JTables is not that easy. I can know, I've struggled with it myself in the past. Rob Camick, a valued member here, has written this. With a little modification you can add icons to the buttons. You can even change the getTableCellEditorComponent and getTableCellRendererComponent methods to make better use of the current value; if that's an Icon, instead of setting the text to the toStirng() value of the icon, clear the text and set the icon:
    getTableCellRendererObject would get similar code. Or, if you only want to display one single icon for all rows, just modify the button once in the constructor and don't modify it in these two methods.
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much for your reply. I will give this a shot.

    I am awfully new to java gui's so this whole thing has been a struggle to me. Thankful for a forum like this.
     
    Rob Camick
    Ranch Hand
    Posts: 2753
    11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob Camick, has written a button render/editor. With a little modification you can add icons to the buttons.


    Rob, thanks for the suggestion.

    I actually added this to my class a while ago, although I forgot to update the version on my website so it didn't do anybody any good. Anyway, my updated version is now available.

     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Great! all of my buttons work great and the actionListeners are responsive!

    I guess the only problem I am having is trying to determine what to change in order to put a separate image in each JButton. I can get it to display the same picture in all of them, but as i do not have much experience with JTables, or renderers of any type I'm a little lost.

    I noticed that you gave a snippet of code for a JTableButtonRenderer, and i see that it is setting the icon of the button to whatever icon it may be. But if anyone could give me a little push in setting an image(stored in a SQL database) to each button that would be cool.

    I am not sure if the renderers are rendering every button as the same thing(thus why i have the same image on every cell) or if i am just missing something.

    Sorry for being so helpless in the matter, but i do enjoy a good learn!
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joe Meany wrote:I guess the only problem I am having is trying to determine what to change in order to put a separate image in each JButton.


    I already answered that.

    Darryl Burke wrote:I recommend that you populate the table's model with Images or ImageIcons instead.


    Ot didn't you notice that one of the parameters to getTableCellRenderer/EditorComponent is Object value?
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I did notice that, but i thought that the JButton was the object in that case.
    I'll look at it all some more, im sure there is something very dumb that i am just bypassing.

    Thanks guys
     
    Darryl Burke
    Bartender
    Posts: 5167
    11
    Java Netbeans IDE Opera
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't think I can be more direct than that without actually buckling down and writing an SSCCE. But if you can come up with a SSCCE, I'll be happy to point out any changes I recommend.

    OK here's a line or two I would expect to see in the method I named (typed here, watch out for typos) If your renderer still extends JButton --or pssibly
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    oh geeze. thank you very much. I totally misunderstood a good half of what you were explaining to me. Thank you very much for your help Darryl, and im sorry, i really wasnt trying to seem that helpless!

    Thanks again!
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey guys,

    Sorry for writing back so soon, but I just have one other question.

    When i click on any of the JButtons that I have placed in my JTable,
    all of the images either change to blank, or to a different image.
    Now, I know that every time a button is pushed the renderer is called,
    but the way i am storing my images is i think what the problem is.
    As of now i am storing all of my images(in this case, and according to
    the database that is 15) into an array, so of course, once the table is filled with all 15 images,
    the renderer has no more to use. Great, i understand that.

    I suppose my question to you all is what is a more efficient way to store all of my
    images so that this does not occur.
    I realize that the way i thought would work for this was obviously wrong but i did give it a shot there.
    Ill post a snippet just in case anyone does have some time to help me out with this.

    Again i appreciate all of the help and time you guys are giving!



    These were the two methods that i thought were relevant to my situation.
     
    Rob Spoor
    Sheriff
    Posts: 20893
    81
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Are you actually reading a file each time you want to display an image? That's definitely going to negatively impact the responsiveness of your application.

    Why don't you just store the Icon in your TableModel for that column, as you've already been advised to do before?
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, this is my first time using JTables so i didnt understand what it meant when it was brought up the first time. But now that i have a slight clue i will google exactly what you are saying. However, the table model can hold more than just one image then correct?

    Thanks for the quick reply, I've spent my entire java career(1 year) not touching GUI's, so I really do appreciate all of your help.
     
    Rob Spoor
    Sheriff
    Posts: 20893
    81
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The TableModel should hold one image per row. You can still share one image object for multiple rows; after all, the TableModel only stores references to objects.
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I was hoping to get 3 images per row, is that possible in the way which you describe?
     
    Rob Camick
    Ranch Hand
    Posts: 2753
    11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    > I am developing an application that will populate a JTable with jbuttons. Each one of these jbuttons will have an imageicon of the picture of a person.

    If you want the image rendered as a button, this implies that the user can click the button and some Action will happen. This is relatively complex as it requires knowledge on how renderers and editors work. 3 days ago Rob Spoor provided you with a link to a solution for text only buttons. I replied a couple of hours later stating that the code had been updated to also support Icons on a button.

    All you do is add an ImageIcon to the TableModel and use my class. The basic code for creating the image displayed in the link is:



    If on the other hand you just want to show different icons in different columns that are not clickable, then you don't need to create any custom renderers, you just tell the table which columns contain Icons and the table will choose the appropriate renderer:



    If you have more problems then post your SSCCE demonstrating the problem.
     
    Joe Meany
    Greenhorn
    Posts: 25
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ahh you guys are incredible!

    Thank you so much, almost finished with this jtable now!

    All thats left to do is get each pictures path from the database and cause some action.

    Thanks again, I'm really glad i decided to join and fork over some pride for some help!
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!