DefaultListModel & JTable; how to sync the items in the DefaultListModel
I have a jtable with items in it
example
name date age
Peter 01-jan-43 62
Ron 03-nov-73 32
when double clicking the name( eg peter), a new window pops up contains all relevent info of the user.
I added a sorter and when sorting the table it comes in the correct-wanted order, however, when double clicking the item - the wrong window pops up.
I take it the DefaultListModel is not synchronized with the tables elements.
Q: how to correct the DefaultListModel once the table has been sorted so it will be in sync with the jtable.
thanks
peter
thank you camicker for replying.
I have been trying for the past 4 hours to dig in the code (as well as google) to find where to implement a sync with the DefaultListModel. Nothing.
any input?
package com.shared.model;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.*;
public class TableSorter extends AbstractTableModel
protected TableModel tableModel;
public static final int DESCENDING = -1;
public static final int NOT_SORTED = 0;
public static final int ASCENDING = 1;
private static Directive EMPTY_DIRECTIVE = new Directive(-1, NOT_SORTED);
public static final Comparator COMPARABLE_COMAPRATOR = new Comparator()
public int compare(Object o1, Object o2)
return ((Comparable) o1).compareTo(o2);
public static final Comparator LEXICAL_COMPARATOR = new Comparator()
public int compare(Object o1, Object o2)
return o1.toString().compareTo(o2.toString());
private Row[] viewToModel;
private int[] modelToView;
private JTableHeader tableHeader;
private MouseListener mouseListener;
private TableModelListener tableModelListener;
private Map columnComparators = new HashMap();
private List sortingColumns = new ArrayList();
public TableSorter()
this.mouseListener = new MouseHandler();
this.tableModelListener = new TableModelHandler();
public TableSorter(TableModel tableModel)
this();
setTableModel(tableModel);
public TableSorter(TableModel tableModel, JTableHeader tableHeader)
this();
setTableHeader(tableHeader);
setTableModel(tableModel);
private void clearSortingState()
viewToModel = null;
modelToView = null;
public TableModel getTableModel()
return tableModel;
public void setTableModel(TableModel tableModel)
if (this.tableModel != null)
this.tableModel.removeTableModelListener(tableModelListener);
this.tableModel = tableModel;
if (this.tableModel != null)
this.tableModel.addTableModelListener(tableModelListener);
clearSortingState();
fireTableStructureChanged();
public JTableHeader getTableHeader()
return tableHeader;
public void setTableHeader(JTableHeader tableHeader)
if (this.tableHeader != null)
this.tableHeader.removeMouseListener(mouseListener);
TableCellRenderer defaultRenderer = this.tableHeader.getDefaultRenderer();
if (defaultRenderer instanceof SortableHeaderRenderer)
this.tableHeader.setDefaultRenderer(((SortableHeaderRenderer) defaultRenderer).tableCellRenderer);
this.tableHeader = tableHeader;
if (this.tableHeader != null)
this.tableHeader.addMouseListener(mouseListener);
this.tableHeader.setDefaultRenderer
new SortableHeaderRenderer(this.tableHeader.getDefaultRenderer())
public boolean isSorting()
return sortingColumns.size() != 0;
private Directive getDirective(int column)
for (int i = 0; i < sortingColumns.size(); i++)
Directive directive = (Directive)sortingColumns.get(i);
if (directive.column == column)
return directive;
return EMPTY_DIRECTIVE;
public int getSortingStatus(int column)
return getDirective(column).direction;
private void sortingStatusChanged()
clearSortingState();
fireTableDataChanged();
if (tableHeader != null)
tableHeader.repaint();
public void setSortingStatus(int column, int status)
Directive directive = getDirective(column);
if (directive != EMPTY_DIRECTIVE)
sortingColumns.remove(directive);
if (status != NOT_SORTED)
sortingColumns.add(new Directive(column, status));
sortingStatusChanged();
protected Icon getHeaderRendererIcon(int column, int size)
Directive directive = getDirective(column);
if (directive == EMPTY_DIRECTIVE)
return null;
return new Arrow(directive.direction == DESCENDING, size, sortingColumns.indexOf(directive));
private void cancelSorting()
sortingColumns.clear();
sortingStatusChanged();
public void setColumnComparator(Class type, Comparator comparator)
if (comparator == null)
columnComparators.remove(type);
else
columnComparators.put(type, comparator);
protected Comparator getComparator(int column)
Class columnType = tableModel.getColumnClass(column);
Comparator comparator = (Comparator) columnComparators.get(columnType);
if (comparator != null)
return comparator;
if (Comparable.class.isAssignableFrom(columnType))
return COMPARABLE_COMAPRATOR;
return LEXICAL_COMPARATOR;
private Row[] getViewToModel()
if (viewToModel == null)
int tableModelRowCount = tableModel.getRowCount();
viewToModel = new Row[tableModelRowCount];
for (int row = 0; row < tableModelRowCount; row++)
viewToModel[row] = new Row(row);
if (isSorting())
Arrays.sort(viewToModel);
return viewToModel;
public int modelIndex(int viewIndex)
return getViewToModel()[viewIndex].modelIndex;
private int[] getModelToView()
if (modelToView == null)
int n = getViewToModel().length;
modelToView = new int[n];
for (int i = 0; i < n; i++)
modelToView[modelIndex(i)] = i;
return modelToView;
// TableModel interface methods
public int getRowCount()
return (tableModel == null) ? 0 : tableModel.getRowCount();
public int getColumnCount()
return (tableModel == null) ? 0 : tableModel.getColumnCount();
public String getColumnName(int column)
return tableModel.getColumnName(column);
public Class getColumnClass(int column)
return tableModel.getColumnClass(column);
public boolean isCellEditable(int row, int column)
return tableModel.isCellEditable(modelIndex(row), column);
public Object getValueAt(int row, int column)
return tableModel.getValueAt(modelIndex(row), column);
public void setValueAt(Object aValue, int row, int column)
tableModel.setValueAt(aValue, modelIndex(row), column);
// Helper classes
private class Row implements Comparable
private int modelIndex;
public Row(int index)
this.modelIndex = index;
public int compareTo(Object o)
int row1 = modelIndex;
int row2 = ((Row) o).modelIndex;
for (Iterator it = sortingColumns.iterator(); it.hasNext();)
Directive directive = (Directive) it.next();
int column = directive.column;
Object o1 = tableModel.getValueAt(row1, column);
Object o2 = tableModel.getValueAt(row2, column);
int comparison = 0;
// Define null less than everything, except null.
if (o1 == null && o2 == null)
comparison = 0;
} else if (o1 == null)
comparison = -1;
} else if (o2 == null)
comparison = 1;
} else {
comparison = getComparator(column).compare(o1, o2);
if (comparison != 0)
return directive.direction == DESCENDING ? -comparison : comparison;
return 0;
private class TableModelHandler implements TableModelListener
public void tableChanged(TableModelEvent e)
// If we're not sorting by anything, just pass the event along.
if (!isSorting())
clearSortingState();
fireTableChanged(e);
return;
// If the table structure has changed, cancel the sorting; the
// sorting columns may have been either moved or deleted from
// the model.
if (e.getFirstRow() == TableModelEvent.HEADER_ROW)
cancelSorting();
fireTableChanged(e);
return;
// We can map a cell event through to the view without widening
// when the following conditions apply:
// a) all the changes are on one row (e.getFirstRow() == e.getLastRow()) and,
// b) all the changes are in one column (column != TableModelEvent.ALL_COLUMNS) and,
// c) we are not sorting on that column (getSortingStatus(column) == NOT_SORTED) and,
// d) a reverse lookup will not trigger a sort (modelToView != null)
// Note: INSERT and DELETE events fail this test as they have column == ALL_COLUMNS.
// The last check, for (modelToView != null) is to see if modelToView
// is already allocated. If we don't do this check; sorting can become
// a performance bottleneck for applications where cells
// change rapidly in different parts of the table. If cells
// change alternately in the sorting column and then outside of
// it this class can end up re-sorting on alternate cell updates -
// which can be a performance problem for large tables. The last
// clause avoids this problem.
int column = e.getColumn();
if (e.getFirstRow() == e.getLastRow()
&& column != TableModelEvent.ALL_COLUMNS
&& getSortingStatus(column) == NOT_SORTED
&& modelToView != null)
int viewIndex = getModelToView()[e.getFirstRow()];
fireTableChanged(new TableModelEvent(TableSorter.this,
viewIndex, viewIndex,
column, e.getType()));
return;
// Something has happened to the data that may have invalidated the row order.
clearSortingState();
fireTableDataChanged();
return;
private class MouseHandler extends MouseAdapter
public void mouseClicked(MouseEvent e)
JTableHeader h = (JTableHeader) e.getSource();
TableColumnModel columnModel = h.getColumnModel();
int viewColumn = columnModel.getColumnIndexAtX(e.getX());
int column = columnModel.getColumn(viewColumn).getModelIndex();
if (column != -1)
int status = getSortingStatus(column);
if (!e.isControlDown())
cancelSorting();
// Cycle the sorting states through {NOT_SORTED, ASCENDING, DESCENDING} or
// {NOT_SORTED, DESCENDING, ASCENDING} depending on whether shift is pressed.
status = status + (e.isShiftDown() ? -1 : 1);
status = (status + 4) % 3 - 1; // signed mod, returning {-1, 0, 1}
setSortingStatus(column, status);
private static class Arrow implements Icon
private boolean descending;
private int size;
private int priority;
public Arrow(boolean descending, int size, int priority)
this.descending = descending;
this.size = size;
this.priority = priority;
public void paintIcon(Component c, Graphics g, int x, int y)
Color color = c == null ? Color.GRAY : c.getBackground();
// In a compound sort, make each succesive triangle 20%
// smaller than the previous one.
int dx = (int)(size/2*Math.pow(0.8, priority));
int dy = descending ? dx : -dx;
// Align icon (roughly) with font baseline.
y = y + 5*size/6 + (descending ? -dy : 0);
int shift = descending ? 1 : -1;
g.translate(x, y);
// Right diagonal.
g.setColor(color.darker());
g.drawLine(dx / 2, dy, 0, 0);
g.drawLine(dx / 2, dy + shift, 0, shift);
// Left diagonal.
g.setColor(color.brighter());
g.drawLine(dx / 2, dy, dx, 0);
g.drawLine(dx / 2, dy + shift, dx, shift);
// Horizontal line.
if (descending) {
g.setColor(color.darker().darker());
} else {
g.setColor(color.brighter().brighter());
g.drawLine(dx, 0, 0, 0);
g.setColor(color);
g.translate(-x, -y);
public int getIconWidth()
return size;
public int getIconHeight()
return size;
private class SortableHeaderRenderer implements TableCellRenderer
private TableCellRenderer tableCellRenderer;
public SortableHeaderRenderer(TableCellRenderer tableCellRenderer)
this.tableCellRenderer = tableCellRenderer;
public Component getTableCellRendererComponent(JTable table,
Object value,
boolean isSelected,
boolean hasFocus,
int row,
int column)
Component c = tableCellRenderer.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
if (c instanceof JLabel) {
JLabel l = (JLabel) c;
l.setHorizontalTextPosition(JLabel.LEFT);
int modelColumn = table.convertColumnIndexToModel(column);
l.setIcon(getHeaderRendererIcon(modelColumn, l.getFont().getSize()));
return c;
private static class Directive
private int column;
private int direction;
public Directive(int column, int direction)
this.column = column;
this.direction = direction;
}
Similar Messages
-
I have the iPhone 4, and yesterday I synced my photos onto iTunes, but now I have albums on my phone that cannot be deleted, and there is not an option to delete the items in the albums individually... How do I get rid of these unwanted albums? Using Windows 7.
If you used iTunes to get the photos on the phone, then use the same iTunes and uncheck the music you don't want and do another sync.
-
I am having trouble syncing my itouch I recently purchased some songs from the itunes store. I ma trying to sync them to my itouch. When I do I get the following message "itunes sync..67 items could not be synced"Anyone have an advice on how to sync these items?
Connect your phone to iTunes, then right-click on the name of your phone on the left sidebar and select Reset Warnings. Then sync again and you should get a warning listing the problem files.
-
I recently ran into a problem with syncing my ipod touch to itunes on my Windows 7 pc. It had previously been synced to my older laptop
and I wanted to change to my newer Windows 7 laptop's itunes library that I had already been syncing with.
Anyway, I started seeing this message that prevents my from syncing:
The iPod "Bill's iPod" cannot be synced because there is not enough free space to hold all of the items in the iTunes library
(additional 2.48 GB required)
Not sure if it is talking about my iPod free space or my PC itunes library free space. Either way I have 11.0 GB free space available
on my iPod and 412 GB free space available on my Windows 7 PC.
How do I handle this problem? Please help!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ThanksIt is saying that the iPod does not have enough free storage to sync all the items yu have selected to sync to the iPod. You need 2.48 GB more free on the iPod. You need either to select less to sync or delete some content form the iPod.
-
My ipod touch shows 10.1 gb of memory, but when I try to transfer a movie from my computer to ipod touch I get a error message that says "Cannot be synced because there is not enough free space to hold all of the items in the itunes library, additional 5.81 gb required.
I have had as many as 3-4 movies on here plus my music and had no problem, there are no other movies on my ipod. Wasssup??? I am 66 years old, can build a house or a car engine, but for the life of me I don't understand this stuff....Help!!Unless the move is about 16 GB, you are trying to sync more that the movie to your iPod. I would look carefull at awhat is checked to sync to the iPod.
Also how big is the "other" category as shown in the colored bargraph in iTunes? -
I lost my contacts and calendar, and photos since the iOS update and and it states that my iPhone cannot be synced because there is not enough free space to hold all of the items in the iTunes library (additional 1 MB required). Also it shows that I am over capacity on my Capacity ruler but how do I unload some of that stuff so that I can increase my capacity and perhaps get this synching to work again.
So frustrating!!On your iPhone go to settings>general>about and next to "Available" if you dont have much space then you will need to delete stuff on the phone to make room to take photos by deleting stuff on the phone.
In terms of syncing change what you are syncing and decide what you would really want and not want on your phone which would make what is syncing to the phone smaller. -
I try to sync my nano and get: the ipod cant be synced because there is not enough free space to hold all of the items in the items library (need 100MB) - I have a new computer?? can you help me understand this message: what to do?
Hello pryan1012,
What this message means is that you have more music in your itunes library than there is free space in your ipod.
I had this same issue at one time. This is what helped me put my music on the ipod. I used manually manage.
Learn how to sync muisc here.
Hope this helps.
~Julian -
Cannot sync because there is not enough free space to hold all of the items in the iTunes library
On my last attempt to sync my iPod Touch with my laptop I got the following message:
The iPod "*******" cannot be synced because there is not enough free space to hold all of the items in the iTunes library (additional 3.18 GB required)
My iPod is the basic 8 GB version with 6.8 GB capacity. I have cleard most of my photos off the iPod. I have 536 MB available.
How do I get the iPod to sync?I found it! There is a note in the help pages. Basically turn off all sync options in iTunes. Apply that change. Then go back and turn sync's back on. WARNING.. Take note of how your sync's are set up before you turn them off.
-
I purchased some children's books from iTunes on my Mac computer and then tried to sync to my iPad and received the note that " The iPad cannot be synced because there is not enough free space to hold all of the items in the iTunes library (additional 2.53 GB required)."
I do not know what steps to take to be able to free up space so I can sync these e-books to my iPad. I do not know how to find the iTunes library on the iPad to identify what I would need to delete.
Thanks for your guidance.How much space is your Other using? You may be able to reduce.
How Do I Get Rid Of The “Other” Data Stored On My iPad Or iPhone?
http://tinyurl.com/85w6xwn
With an iOS device, the “Other” space in iTunes is used to store things like documents, settings, caches, and a few other important items. If you sync lots of documents to apps like GoodReader, DropCopy, or anything else that reads external files, your storage use can skyrocket. With iOS 5, you can see exactly which applications are taking up the most space. Just head to Settings > General > Usage, and tap the button labeled Show All Apps. The storage section will show you the app and how much storage space it is taking up. Tap on the app name to get a description of the additional storage space being used by the app’s documents and data. You can remove the storage-hogging application and all of its data directly from this screen, or manually remove the data by opening the app. Some applications, especially those designed by Apple, will allow you to remove stored data by swiping from left to right on the item to reveal a Delete button.
Cheers, Tom -
Hi,
I know similar cases were posted here but I did not find something that matched my case:
Here is the message I got:
The iPad cannot be synced because there is not enough free space to hold all of the items in the iTunes library (1 MB required, 4.29 GB available).
Obviously I have 1M space if I have 4.29GB of space.
I do not have any music or photos
I just updated the current apps I have and did an synced several new apps with a total of about 500M (which is far less then the free ~4GB I have)
Any ideas how to solve this?
Thank youYou have seem similar instances of this, but have you tried anything suggested in those discussions? I would quit ITunes, reboot your computer, reset the iPad and then try again. I would also close all apps on the iPad before resetting the device. When you try the sync again, use a different USB port on your computer.
I Know this seems to be an no brainer, but make sure that you didn't accidentally select more content to sync than the iPad can hold
In order to close apps, you have to drag the app up from the multitasking display. Double tap the home button and you will see apps lined up going left to right across the screen. Swipe to get to the app that you want to close and then swipe "up" on the app preview thumbnail to close it.
In order to reset your iPad, hold down on the sleep and home buttons at the same time for about 10-15 seconds until the Apple logo appears on the screen after which time you can release the buttons and let the iPad start up. -
The message is The Ipad cannot be synced because there is not enough free space to hold all the items in the ITunes library (6.55G required, 6.98G available). which doesn't make sense as there is enough room according to the message
How much space is your Other using? You may be able to reduce.
How Do I Get Rid Of The “Other” Data Stored On My iPad Or iPhone?
http://tinyurl.com/85w6xwn
With an iOS device, the “Other” space in iTunes is used to store things like documents, settings, caches, and a few other important items. If you sync lots of documents to apps like GoodReader, DropCopy, or anything else that reads external files, your storage use can skyrocket. With iOS 5, you can see exactly which applications are taking up the most space. Just head to Settings > General > Usage, and tap the button labeled Show All Apps. The storage section will show you the app and how much storage space it is taking up. Tap on the app name to get a description of the additional storage space being used by the app’s documents and data. You can remove the storage-hogging application and all of its data directly from this screen, or manually remove the data by opening the app. Some applications, especially those designed by Apple, will allow you to remove stored data by swiping from left to right on the item to reveal a Delete button.
Cheers, Tom -
How to delete the items in the recycle bin permanently from the windows using Diruse command
How to delete the items in the recycle bin permanently from the windows using Diruse command.
Because most of the time we get out of disk space issues.
Can somebody help me in giving with an exampleYou can right-click Recycle Bin|Properties and choose radio button for
Don't move files ....
This one may also help.
http://technet.microsoft.com/en-us/library/cc784980(v=ws.10).aspx
Regards, Dave Patrick ....
Microsoft Certified Professional
Microsoft MVP [Windows]
Disclaimer: This posting is provided "AS IS" with no warranties or guarantees , and confers no rights. -
How to delete an item in the table
hi all ,
Is anyone there to give me an idea , how to delete the item in the table , by selecting the row. , for the what i have to do , is there any documentation to follow , guide me itys really urgent.Hai Madhu,
It is pretty simple.
First u define a button. and in onAction Event of the button write the following code.
process is as follows:
1)get the node
2)get the element
3)using the method remove_element() ,remove the element.
<u><b>The Sample Code is as follows</b></u>
DATA:
node_flighttab TYPE REF TO if_wd_context_node,
elem_flighttab TYPE REF TO if_wd_context_element.
node_flighttab = wd_context->get_child_node( 'FLIGHTTAB' ).
get element via lead selection
elem_flighttab = node_flighttab->get_element( ).
node_flighttab->remove_element( element = elem_flighttab ).
Follow the above code ,it will definately help you. -
How do I remove items from the cloud without losing them permanently?
My icloud storage is almost full. How do I remove items from the cloud and not lose them?
What items?
Purchased music, movies, TV shows, apps, and books do not use up your iCloud storage.
See the link below for how to reduce the amount of storage you're using:
http://support.apple.com/kb/HT4847 -
How do you remove items from the assets panel that are duplicated?
How do you remove items from the assets panel that are duplicated?
If you add an item to a slideshow, you'll usually see 2 entries for that image in the assets panel - one represents the thumbnail, and the other represents the larger 'hero' image.
It sounds like you may have added the same image to your slideshow twice. You can select one of the hero images or thumbnail images in your slideshow and use the delete key to remove it. Then the extra 2 entries in the assets panel should disappear.
Maybe you are looking for
-
Need help in order to recovery my Satellite L450-16Q
My laptop had a virus so I used the Product Recovery disk that came with it, thinking that it would fix it but it ended up erasing the whole of my computer even windows 7. So now I have a computer I can't use I have bought windows 7 directly from mic
-
Hi, I have developed one report but i need to place tht report in another server, can any one tell me the FTP program logic , so tht i can place my report in their server.
-
In what order are msgs consumed with single sess & multiple async consumers
Sun MQ will serialize delivery of messages when you have a single session and multiple asynchronous consumers created from that session. I am trying to find out what is the order in which the messages will be delivered by the session to the message l
-
hi, i am working on one step cross docking scenorio , i am missing some where in configuration. plz guide me. steps i followed i) checked the preallocated check box in omlj transaction for 101 - movt type ii)entered the preallocated stock details in
-
New phone - iMessage and Facetime not working with number?
I had a 4s and recently got a 5s, and now iMessage on my Mac only has my email as an option. My phone number is the same. I searched around for a solution, and one involved signing out and back into both iMessage and Facetime on my Mac, so I did, and