I'm using a TableViewer to create a table but whenever I hover with the mouse over a cell the whole row gets highlighted but I consider this more confusing than helpful.
What I want is either to highlight nothing when simply hovering or to highlight only the cell the mouse is currently over.
I've looked into several code snippets but I never found something that does what I want.
So how can I achieve the above described behaviour?
Related
I need to customise the selection behaviour of the cells inside a JTable descendant. I have a custom cell editor using a JTextField descendant as the editorComponent; there's a focus listener registered on it which manages the desired selection behaviour.
I need different behaviour when tabbing around the table than when another window or application comes to the front and then goes away again.
This is currently not possible, because the cell editor's editorComponent seems to have no off-the-shelf way of knowing (or telling it) that it's the editor for a table cell, so it doesn't know that it's "inside" the table, so my focusGained() and focusLost() think focus is moving between different windows even if I'm just tabbing around in the table.
SwingUtilities.windowForComponent() returns null for the editorComponent.
Before I roll my own solution, is there an accepted way of dealing with this issue? I can't be the first person to need to do this...
I need your help guys.
I am currently working on a simple JTable in Java with 3 Columns and 128 rows. Now there is one problem i cannot solve at the moment. The thing i want is that when I click on a single cell it changes its background color to Green.
So i need to implement a MouseListener which reacts to the selected cell and sets its background to green. Sort of a "CellListener" I mean.
I've tried so many things and searched many blogs etc. but none of them gave me a satisying answer.
Do you have any tips for me?
I doubt you will find a direct link between you being able to click on the table and it respond. Because JTables aren't going to listen out for it like that. If you want to do this. You will need an intermediate step. I would suggest that you use the mouse listener to get the mouse location on the screen when the user clicks. And then check IF that location is the same as a particular cell of the JTable.
I'm writing an application that has a JTable, and an edit button that sets the current selected row to be editable. Then once the user is done altering the data, they can click the edit button again (with text that now says "Save") to save the data.
The problem is though, when I set a row to be editable, there isn't a visible difference. I could add some code to the renderer to draw the editable cells a little differently, but I don't know what the proper way to make a cell look editable is. Change the color? Make it look like a JTextField? What's the standard method?
Thanks!
Really this is a user interface design question, not a programming one.
To do what you want you need to supply an appropriate cell renderer with the changes you desire but you will need to decide on your own settings. One option might just be to look at the difference between an editable and non-editable text area and apply those to all the cells on the table. This may be as simple as setting the renderers to disabled for any read-only rows.
I have a JTable in my code.
And whenever there is an update to any specific column in the row (cell basically), I will update the corresponding icon in that cell.
so I'm basically following these steps.
Step 1: I update the model.
Step2 2: I'm calling
tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
This works fine.
But problem comes when I drag and drop the columns from one position to another in Table header. And whenever there is an update to any specific cell, I follow the same steps as I mentioned before.
Problem: I'm not seeing the Icon painted. But if I bring the focus on top of that row in table it is painting the icon.
Observation: I see the tableRowIndex and tableColumnIndex are correct after dragging the columns.
Just for testing I added this piece of code in the problem scenario.
examTable.repaint(examTable.getCellRect(examTableRowIndex, examTableColumnIndex, true));
This is repainting the cell properly.
But this is not the right solution I guess. I tried to debug the code I didn't find much about the problem
I'm calling tableModel.fireTableCellUpdated(tableRowIndex, tableColumnIndex);
That is wrong. You should never invoke the firXXX methods directly. That is the job of the TableModel to invoke the appropriate event when the data is changed.
I will update the corresponding icon in that cell.
All you need to do is invoke model.setValueAt(...) method to change the Icon and the model will notify the table that data has changed so the table can repaint itself.
examTable.repaint(...)
Again you should not need to manually invoke repaint on the table
But problem comes when I drag and drop the columns from one position to another in Table header.
Not sure why you need special code for this if you follow the advice from above. But if for some reason it is still necessary then you need to look at the convertColumnIndex...(...) methods to make sure you are using the proper index for your column.
Right now, I'm using Java Swing to create a JEditorPane primarily for its ability to have hyperlinks. I've successfully been able to display links and have them execute behavior upon a click, but I'm running into a few problems with formatting.
How can I set the cursor so that it normally is an arrow, but changes to a text cursor when hovering over text? (In essence, the behavior a cursor has within a web browser). I tried
EditorPane.setCursor(new Cursor(Cursor.TEXT_CURSOR))
but that made it a text cursor everywhere, even when not hovering over text. Right now, hovering over a link shows a pointer hand; I'd like to maintain that functionality as well.
What is the best way to show tooltips or mouseover text when hovering over a link? I tried modifying the title attribute of the link but nothing showed up.
I was trying to implement links to skip down to a subsection of the page, much like http://en.wikipedia.org/wiki/Xkcd#History would take you directly to the History subsection of Wikipedia's xkcd page. How can I do this?
An answer to any of these would be great (and multiple would be awesome xP). Thanks a lot for your help!
As you said one can simply give answers to a single point as well, let me try one by one, here is the answer for your last Point 3
Just provide an id to your tag like this
<h1><a id = "top"></a>First Line</h1>
Now somewhere in the bottom of your page write this :
<p>Return to TOP</p>
Clicking this link, you will reach the above area of the PAGE.
Points 1 & 2 may be addressed using the approach mentioned here. In particular, the view/model conversion methods will let you condition setCursor() and getToolTipText(), respectively.
You can get source from here http://java-sl.com/JEditorPaneStructureTool.html
It shows how to obtain text view bounds. First you get caret position for current mouse poiunter using viewToModel() method. Then go down the Views tree achieving leaf view and calcualte it's bounds. See this http://java-sl.com/tip_view_rectangle.html
If your mouse pointer in the view's rectangle then your mouse over text.
You can check whether the text in caret position is link and show your tooltip.
Use this http://java-sl.com/tip_links_in_editable.html to see how to detect whether mouse is over link.
Point 3.rd is answered by #nIcE cOw