Results 1 to 11 of 11
Thread: Text over JTable
- 07-14-2010, 05:08 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 27
- Rep Power
- 0
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.
- 07-14-2010, 06:49 PM #2
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 07-14-2010, 06:53 PM #3
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
- 07-14-2010, 06:57 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 07-14-2010, 09:49 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 27
- Rep Power
- 0
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.
- 07-15-2010, 12:05 AM #6
Member
- Join Date
- Mar 2010
- Posts
- 27
- Rep Power
- 0
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.
- 07-15-2010, 12:10 AM #7
Member
- Join Date
- Mar 2010
- Posts
- 27
- Rep Power
- 0
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.
- 07-15-2010, 06:29 AM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 07-15-2010, 12:41 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 27
- Rep Power
- 0
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?
- 07-15-2010, 01:04 PM #10
Member
- Join Date
- Mar 2010
- Posts
- 27
- Rep Power
- 0
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);
}
}
}
- 07-15-2010, 01:07 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 27
- Rep Power
- 0
For:
UIManager.put("MenuItem.selectionBackground", Color.WHITE);
see more in:
How to change System colors of swing?
Similar Threads
-
insert data from text field to jtable
By mackinas in forum New To JavaReplies: 2Last Post: 06-09-2010, 04:30 AM -
JTable vs JTextArea scrolling text question
By adonos in forum AWT / SwingReplies: 2Last Post: 05-24-2010, 08:15 PM -
Reading and writing the contents of jtable into a text file
By Manfizy in forum NetBeansReplies: 6Last Post: 12-12-2008, 03:35 PM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks