Java Swing Gurus?

siftings81

Member
Sep 3, 2001
61
0
0
I'm working on a scheduling program (using Java/Swing) that simply has several tables that I drag and drop information pulled from a database. In one table I have it set up so that the user can drag multiple data (people?s names) into a single cell (just concatenate the new name with the existing names). However, I now need the ability to drag a specific name outside the table (to delete it). Since I treat the contents of the whole cell as one big string, I'm stumped as what to do next. I'm new to Java, but have been reading about custom editors and was wondering if it would be possible to write one that would work in my situation. I'm also open to any other ways that would make specific selection possible. Any help would be greatly appreciated.
 

znaps

Senior member
Jan 15, 2004
414
0
0
I don't think you'd need a cell editor to do what you need. I think you need to:

- find a method of detecting which name is underneath the mouse when a cell is clicked on. You'd need a custom table model such that every cell returns some collection of custom objects representing each name.
- write a cell renderer so that you can highlight specific names in a cell when the mouse is clicked on one
- add your drag and drop code using the above two features
 

siftings81

Member
Sep 3, 2001
61
0
0
Thanks for the help znaps...

I understand what I need to do to write the custom table model, however, I'm a bit lost about how to find a method of detecting which name is underneath the mouse when a cell is clicked on. I've only read about getting the actual cell the mouse click on, not the coordinates inside the cell.
 

znaps

Senior member
Jan 15, 2004
414
0
0
MouseEvents pass the mouse x and y coordinates so if you add a MouseListener to your table you could use store those x and y values in your cell renderer component. Your cell renderer could be a JPanel subclass which stores some JLabels (each representing a name) and the labels would be laid out using a FlowLayout in the panel - that way the names would "wrap" in the table cell. You could create a method in your JPanel subclass to return which component is at (x,y), and highlight that component in your renderer getCellRenderComponent() method.

This is a rough idea just off the top of my head and there would be other things you'd need to do, but it's the approach I'd take.
 

siftings81

Member
Sep 3, 2001
61
0
0
I'm unclear on how to find which component is at (x,y). I already set up a mouse listener to tell me where in the table the user clicks. Thanks again.
 

znaps

Senior member
Jan 15, 2004
414
0
0
Your myRenderer class will extend JPanel, instead of JLabel. Maintain a list of JLabels which you'll add and remove dynamically to the panel in the getTableCellRendererComponent method.

Sorry not much time right now...I will try and help more later.
 

siftings81

Member
Sep 3, 2001
61
0
0
That certainly clears several things up. Just a couple more questions.

Should I explicitly create a new Jpanel to add/delete the labels from the list? And what class should I create it in?

public JPanel thepanel = new JPanel(new FlowLayout());
public LinkedList labelList = new LinkedList();

public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
// 'value' is value contained in the cell located at
// (rowIndex, vColIndex)

if (isSelected) {
// cell (and perhaps other cells) are selected

System.out.println("trying to add to list");
labelList.add(value);
JLabel lab=new JLabel(value.toString());
thepanel.add(lab);
}

if (hasFocus) {
// this cell is the anchor and the table has the focus

}

return thepanel;
}

I'm going to have to modify my TableModel somehow right?

TableModel dataModel = new AbstractTableModel() {
..
public Object getValueAt(int row, int col)
{
return data[row][col].getText(); // return labelList.get()
}

instead of a static array of elements, I will be returning items from a list?

public void setValueAt(Object aValue, int row, int column) {
// what about this method?
}

I'm not sure why I would add/delete labels in the getTableCellRendererComponent method since when I tried it, if the cell with the custom cell renderer was selected, then when I moved the table it seemed to call this method repeatly... filling up the cell.
 

znaps

Senior member
Jan 15, 2004
414
0
0
Your cell renderer will actually extend JPanel - there is no requirement for it to extend JLabel - notice that the getCellRenderer() method signature returns Component and that the method says "return this;" at the end. Also instead of creating new JLabels in the getCellRenderer method, create a pool of JLabels when the renderer is instantiated, and re-use those in the getCellRenderer method by just setting the label text and adding/removing them from the renderer (panel).

getValueAt() in your table model can return a List/String[] for every name in the cell. The renderer can work that out and return itself containing the corresponding amount of JLabels.
 

siftings81

Member
Sep 3, 2001
61
0
0
getCellRenderer() method signature returns this, not the panel? I can only get the labels to show up if I return a panel I've created and added labels to it. If I return "this", and add Labels using add(label), I see nothing....
 

znaps

Senior member
Jan 15, 2004
414
0
0
OK, it nearly was correct but the renderer shouldn't override the validate and revalidate methods because we are adding and removing components dynamically to our renderer component. Also don't set the layout everytime in the getCellRenderer() method - do it in the constructor for the class. Here is the a fixed version plus a table model, and a test class which should be enough for you to go on for now.
 

siftings81

Member
Sep 3, 2001
61
0
0
Thank you for taking the time to do that. Works, but now to find a method of detecting which name is underneath the mouse when a cell is clicked on. What exactly has to be done in the TableModel to return such info?
 

znaps

Senior member
Jan 15, 2004
414
0
0
I was thinking about that and you may actually need a custom editor, not to edit the contents of the table, but to determine which of the JLabel components are underneath the mouse pointer on a mouse click. So create an editor component which displays the info exactly as the renderer.

Use a MouseListener on the table to create an editor on the selected cell on mouseclicks and then ask the editor for the JLabel contents. You'll have to work out how to convert the global table x, y coorinates to local cell coordinates.

You shoudn't have to modify your table model in anyway (I think).
 

siftings81

Member
Sep 3, 2001
61
0
0
Originally posted by: znaps
I was thinking about that and you may actually need a custom editor, not to edit the contents of the table, but to determine which of the JLabel components are underneath the mouse pointer on a mouse click. So create an editor component which displays the info exactly as the renderer.

Like this?

 

siftings81

Member
Sep 3, 2001
61
0
0
Edit: Found the labels... coordinates were off since I'm only using my cell editor in one column, not two. I just subtracted the column width from the coordinates the mouse listener gave me.
 

siftings81

Member
Sep 3, 2001
61
0
0
edit: Got all the issues worked out. Just simple mistakes. Now, I just need to get the drag and drop working, along with handling dynamic data. Thanks
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |