Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: jTable event help

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default jTable event help

    I have a jTable that I enable the user to sort based on which ever column they click on, I use .setAutoCreateRowSorter(true); I am also using netbeans and was wounder what event I can use on the table to determine when the user clicks on the column header to sort it. I have tried all of the mouse events and I am at a lost.

    Any suggestions would be appeciated.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jTable event help

    You could do it with a MouseEvent. What didn't work about it? Where's your SSCCE demonstrating what you've tried?

    You could also extend TableRowSorter and override toggleSortOrder().
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: jTable event help

    I tried

    private void tblProjectsMouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    System.out.print("Table headers were clicked");
    }

    when i click on the table itself, it works however not when i click on the table headers it doesn't. I have also tried all of the other mouse events and none of them seem to work

    once i know what event to use i have the code working properly for a button.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jTable event help

    That code isn't an SSCCE because it doesn't show us anything, and we can't copy and paste it to run it.

    Hint: You can add a MouseListener to the JTable's JTableHeader. Consult the API for useful functions.

    Java Platform SE 6
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. The Following User Says Thank You to KevinWorkman For This Useful Post:

    banny7 (July 28th, 2011)

  6. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: jTable event help

    Sorry I don't think I explained the situtation good enough. The font is rendered red if a there are not enough employees assigned to the project, render black when there is. This part is working, the problem is when a sort the table it renders the wrong indexes black and red. I have code set up to get the correct indexes, which work properly however the user has to click a button. Now i just having trouble determining which event to use to trigger the code so the user doesn't have to press the button. I realize now the first post doesn't give enough detail.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jTable event help

    Now I'm really confused. An SSCCE sure would help. Does adding a MouseListener to the JTable not work? And are you sure you can't just convert the view index to a model index (JTable has some functions for that, check out the API), and use that to determine what color it should be? Why do you need an event at all?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #7
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: jTable event help

    int[] resultCheck = new int[300];

    resultCheck = CheckAssignedSingle(date, metadata, projects);

    TableCellRenderer renderer = new CustomTableCellRenderer(intColTotal, resultCheck);
    try {
    tblProjects.setDefaultRenderer(Class.forName("java .lang.Object"), renderer);
    } catch (ClassNotFoundException ex) {
    this.dispose();
    }

    This code is in a method call loadProjects() which runs the query and sets the table with the data and renders each row correctly. I have this code also with the button, that I called jButton for now until I get it working. I need the event because when the user clicks on the headers for the table to sort the data wheather by project title or project number the font color doesn't sort with them. The index are saved in an array, so this is why they don't change., The event will just rerun the above code with the new indexes.

  9. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jTable event help

    Erm, I think you're probably approaching this the wrong way. But again, without an SSCCE (that's not your whole project, that's a single file that contains the bare minimum) I can't really tell. I would think you should populate your TableModel with the correct data, then do your coloring based on the information in the model. That's independent of the view.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. The Following User Says Thank You to KevinWorkman For This Useful Post:

    banny7 (July 28th, 2011)

  11. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: jTable event help

    I got an event to work this is what I have


    public frmSchedul() {


    initComponents();



    JTableHeader tblHeader = tblProjects.getTableHeader();
    tblHeader.addMouseListener(new java.awt.event.MouseAdapter() {});

    tblHeader.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
    tblHeaderMouseClicked(evt);
    }
    });

    }

    private void tblHeaderMouseClicked(java.awt.event.MouseEvent evt){
    System.out.println("The table header was clicked.");
    }

  12. #10
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: jTable event help

    It's really hard to tell from the scraps you've given us, but it sounds like you want to change the font colour of the table rows depending on the row contents. If so, then your custom table cell renderer should use the information passed to the getTableCellRendererComponent method to set the foreground and/or background colour of the cell component. This way, the colour will be independent of the sort order because it will depend on the contents of the cell, not its position. The table cell renderer is there to set how the cells are displayed and its method gets all the relevant information to decide how to do that.

    It's not clear to me how a button or a mouse listener would help with this - can you explain clearly what you expect the result of pressing the button or clicking the mouse to be, in terms of what the user should see?
    Last edited by dlorde; July 28th, 2011 at 06:45 PM.

  13. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jTable event help

    I see this is marked as solved, did you get it figured out?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #12
    Junior Member
    Join Date
    Jul 2011
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: jTable event help

    Yes it solved. The render was based on position because the table would show how many people needed to be assigned to the project. The user could select multiple job function area. ex a administrator, data collector, so on. If all the employees were assigned accordingly that index was save into an array with the index of the row. The problem was when the user sorted the rows based on the column header. The index would not change so I just needed a listener to tell the trigger the code to run the method of checking the rows again. Now it works perfectly. All the rows were being rendered correctly before they were sorted now with the listener they are being rendered correctly when the user sorts them. My problem was I didn't know you could create a listener for the column headers.

  15. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: jTable event help

    I'm glad you got it sorted out, but for the record, it sounds like you're going about it the wrong way- as other people have said, you should probably just base the rendering off the model, not the view. JTable has some methods for converting from one to the other.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Event Handling
    By maress in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 24th, 2011, 03:29 AM
  2. After a rowInserted() event!
    By MarkusHendersonicus in forum JDBC & Databases
    Replies: 1
    Last Post: December 20th, 2010, 12:55 PM
  3. Menu Event Listeners
    By Gondee in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 16th, 2010, 03:08 PM
  4. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  5. Event handling
    By subhvi in forum AWT / Java Swing
    Replies: 3
    Last Post: August 26th, 2009, 11:20 AM

Tags for this Thread