Results 1 to 11 of 11
  1. #1
    Iskatel is offline Member
    Join Date
    Mar 2010
    Posts
    27
    Rep Power
    0

    Default Text over JTable

    Hi all,

    I am trying to show the number of rows and columns selected at the tip of the mouse on JTable. I managed to do it using pop up menu by just putting one menu item which displays the numbers as the number of selected cells changes. But it is not smooth, too many clicks, as it needs one extra click to do new selection, etc. I wonder if I can use Text Field or something like that just to show a text box instead. Tool tip seems to be too slow to react. Are there any alternatives, maybe other components?

    Thanks,
    Iskatel.

  2. #2
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    If you need to display it at the tip of the mouse, there are no alternatives that I know of. However, you could put a JLabel above/below/beside the table and update that to display the number of selections...

    Personally, I'd go with tool tip text, but if its not fast enough, I can't think of anything besides a writing a MouseMotionListener AND subclassing JTable. You'd then have to override the paintComponent method (and draw the tooltip yourself) which would just be way too complicated to be worth it.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Can't you set the delay in the ToolTipManager?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Not a clue. I don't use tooltips that much. Though now that you mention it, you probably can... I think the OP will have to look through the API docs for that one though!
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  5. #5
    Iskatel is offline Member
    Join Date
    Mar 2010
    Posts
    27
    Rep Power
    0

    Default

    Thank you for your comments. I thought about the delay thing in tooltip, I ll try to find out how to reduce the delay to zero. The label thing is a bit too much for me at the moment as I am adding the table on the fly to the tab pane and then I guess I will have to do the same with the label, and getting, setting, locations and sizes, I don't really like it as I am sure I will screw up with the proportions when the window changes the size.

    Just to mention, the thing with the pop menu, as I am using ListSelectionListener it causes conflict when I set "parent" or host (not sure how to call it) as JTable, I am guessing two listeners can't work together, so I put the parent for the pop up the tab pane which contains the table. I get the last cell rectangle and position the pop up there, but sometimes, as I randomly play around with changing the selection size, it pops up in wrong places.

    Anyhow, just one more pretty random question, is it possible to make the pop up ignore all mouse actions. I tried setting MouseListener with empty functions, didn't work. I am not sure, I am guessing there is a kind queue for events. For example, when pop up is on I need to click anywhere to get pass it before say I am able to select another cell. Can I remove it from the queue if there is any?!

    I will play around with the tooltip for now.

    Thanks again.

  6. #6
    Iskatel is offline Member
    Join Date
    Mar 2010
    Posts
    27
    Rep Power
    0

    Default

    This works fine:

    private class SelectionListener implements ListSelectionListener {
    JTable table;
    SelectionListener(JTable table) {
    this.table = table;
    }
    public void valueChanged(ListSelectionEvent e) {
    int nCols = table.getSelectedColumnCount();
    int nRows = table.getSelectedRowCount();
    JPopupMenu pM = new JPopupMenu();
    pM.setInvoker(table);
    JMenuItem mI = new JMenuItem();
    mI.setText(nRows + " X " + nCols);
    pM.add(mI);
    PointerInfo pI = MouseInfo.getPointerInfo();
    Point pos = pI.getLocation();
    pM.setLocation(pos);
    pM.setVisible(true);
    }

    Couldnt make tooltip work, as it requires hoovering over the cells, so its not dynamic.

  7. #7
    Iskatel is offline Member
    Join Date
    Mar 2010
    Posts
    27
    Rep Power
    0

    Default

    The only minor thing I don't like is that when I go over the pop up menu it goes blue in XP, can't find a way to change the selection color to transparent or opaque though.

  8. #8
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Possibly call setEnabled(false) on the JMenuItem (mI). It would change the text color to gray in XP, but it won't change color on mouse-overs.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  9. #9
    Iskatel is offline Member
    Join Date
    Mar 2010
    Posts
    27
    Rep Power
    0

    Default

    That doesn't really do it. I tried to change the DefaultButtonModel properties for the menu item, didn't work either. Tried MouseListener on JMenuItem, where I tried to change the mouseEnter function, which works to some extent, I can just keep pushing the popup menu away as the mouse goes over it, it doesn't look nice when the selection is finished and you go over the popup menu, it runs away from the selection area. Does anyone know why in the mouseEnter function setBackground doesn't take effect?

  10. #10
    Iskatel is offline Member
    Join Date
    Mar 2010
    Posts
    27
    Rep Power
    0

    Default

    Final works just like I wanted it to:

    //Classes and functions for counting the number of selected columns and rows
    private class SelectionListener implements ListSelectionListener {
    JTable table;

    SelectionListener(JTable table) {
    this.table = table;
    }

    public void valueChanged(ListSelectionEvent e) {
    int nCols = table.getSelectedColumnCount();
    int nRows = table.getSelectedRowCount();
    JPopupMenu popupMenu = new JPopupMenu();
    JMenuItem menuItem = new JMenuItem();
    menuItem.setBackground(Color.WHITE);
    menuItem.setForeground(Color.RED);
    UIManager.put("MenuItem.selectionBackground", Color.WHITE);
    UIManager.put("MenuItem.selectionForeground", Color.RED);
    popupMenu.setInvoker(table);
    popupMenu.add(menuItem);
    if (menuItem==null){
    popupMenu.remove(menuItem);
    }
    menuItem.setText(nRows + " X " + nCols);
    PointerInfo pointerInfo = MouseInfo.getPointerInfo();
    Point mousePosition = pointerInfo.getLocation();
    menuActionListener menuAction = new menuActionListener(popupMenu,mousePosition);
    menuItem.addActionListener(menuAction);
    popupMenu.setLocation(mousePosition);
    popupMenu.setVisible(true);
    menuItem.addMenuDragMouseListener(null);
    }
    private class menuActionListener implements ActionListener{
    JPopupMenu popupMenu;
    Point mousePosition;

    menuActionListener(JPopupMenu popupMenu, Point mousePosition) {
    this.popupMenu = popupMenu;
    this.mousePosition=mousePosition;

    }
    public void actionPerformed(ActionEvent e) {
    popupMenu.setLocation(mousePosition);
    popupMenu.setVisible(true);
    }
    }

    }

  11. #11
    Iskatel is offline Member
    Join Date
    Mar 2010
    Posts
    27
    Rep Power
    0

Similar Threads

  1. insert data from text field to jtable
    By mackinas in forum New To Java
    Replies: 2
    Last Post: 06-09-2010, 04:30 AM
  2. JTable vs JTextArea scrolling text question
    By adonos in forum AWT / Swing
    Replies: 2
    Last Post: 05-24-2010, 08:15 PM
  3. Replies: 6
    Last Post: 12-12-2008, 03:35 PM
  4. Replies: 0
    Last Post: 05-26-2008, 04:26 PM
  5. Replies: 2
    Last Post: 05-15-2008, 10:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •