Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2005

Tip: Looking for answers? Try searching our database.

Jtable - complex tables.

Thread view: 
farseer - 19 Mar 2005 21:47 GMT
Hi, i am not sure how to describe this, so i will give an example of
what i am trying to do:

Name  Descr   Price  Name  Descr   Price
XXX   XXXX    10     WWW   TTTT    12
YYY   YYYY    23     VVV   TTTT    13
ZZZ   ZZZZ    14     TTT   TTTT    33

Basically, i am trying to create a table that contains three columns.
After a certain number of rows has been displayed, it will then "roll
over" display it's records to the right.  i am not sure how to put this
in proper words, but i hope the example above explains what i am trying
to do.

Anyone have an idea or suggestions on how to do this?
Daniel Dyer - 20 Mar 2005 00:27 GMT
I haven't done this myself but I would guess that you could achieve the  
effect that you are looking for by writing a custom TableModel  
implementation without too much difficulty.  If you don't want the model  
to be editable it should be pretty straightforward.  The implementation of  
the getRowCount, getColumnCount and getValueAt methods would just have to  
adjust for the positioning of your elements in the table.  If you want an  
editable table model it's a bit more involved as you have to fire the  
events to make sure the table updates properly when you add and remove  
rows, but it should still be possible.

Dan.

> Hi, i am not sure how to describe this, so i will give an example of
> what i am trying to do:
[quoted text clipped - 11 lines]
>
> Anyone have an idea or suggestions on how to do this?

Signature

Daniel Dyer
http://www.footballpredictions.net

farseer - 20 Mar 2005 01:13 GMT
Hi Daniel,
thank you for responding. it won't be an editable table, so that should
reduce some of the complexity.
I will try your suggestion...though i am still a bit foggy as to how to
implement this.  you have helped me however, by pointing out the three
methods i should concentrate on..

thank you.
John McGrath - 20 Mar 2005 02:18 GMT
> I haven't done this myself but I would guess that you could achieve the
> effect that you are looking for by writing a custom TableModel
> implementation without too much difficulty.  If you don't want the model
> to be editable it should be pretty straightforward.  The implementation
> of  the getRowCount, getColumnCount and getValueAt methods would just
> have to  adjust for the positioning of your elements in the table.  

There is two issues that I think are worth considering.  If you use one
table with twice the normal amount of columns and half of the rows, there
will not be any significant visible separation between the left and right
halves of the table.  Also, the user will be able to move the columns
around, intermixing the columns from the two halves.

Instead of creating a single table with a single table model filtering the
values from an underlying table model, I would consider creating two
separate tables, and place these side-by-side in a panel that is placed in
a JScrollPane.  For each table, I would use a filtering table model that
would present either the first or second have of the rows of the
underlying table model.

As with the other solution, the table model would just have to map the
cells from one view to another.  It would be pretty simple if the table is
not editable, and somewhat more complex if editing is possible, especially
if new rows can be created.

Signature

Regards,

John McGrath

Daniel Dyer - 20 Mar 2005 16:09 GMT
> There is two issues that I think are worth considering.  If you use one
> table with twice the normal amount of columns and half of the rows, there
> will not be any significant visible separation between the left and right
> halves of the table.  Also, the user will be able to move the columns
> around, intermixing the columns from the two halves.

Yes, you are right, I didn't consider those problems.  Custom rendering  
could possibly address the first issue, I'm not sure of the best way to  
fix the second, short of preventing columns from being moved (which may or  
may not be acceptable).  A third issue would surround how selected rows  
are highlighted.  You would probably only want the left or right part to  
be highlighted.  Again, this could be achieved through custom rendering.

> Instead of creating a single table with a single table model filtering  
> the
[quoted text clipped - 4 lines]
> would present either the first or second have of the rows of the
> underlying table model.

That is probably a better solution, so long as there aren't any issues  
with the way the tables scroll.

Dan.

Signature

Daniel Dyer
http://www.footballpredictions.net

John McGrath - 21 Mar 2005 00:00 GMT
> > Instead of creating a single table with a single table model filtering
> > the values from an underlying table model, I would consider creating
[quoted text clipped - 5 lines]
> That is probably a better solution, so long as there aren't any issues
> with the way the tables scroll.

You will need to position the TableHeaders yourself, side-by-side in a
panel that you install in the JScrollPane's header, just like the JTables
are positioned side-by-side in the JScrollPane's viewport.  Once that is
done, I think they will scroll the way you expect them to.

Signature

Regards,

John McGrath

John McGrath - 21 Mar 2005 00:28 GMT
I started thinking about this, and got worried that there may be some
"hidden" gotchas that would crop up.  I would hate to convince you that
this approach was easy, and have it turn out to be much more complicated,
so I gave it a try.  It turned out to work pretty much as I had expected.
No problems at all.  FYI, here is the code:

=========================================================================
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

public class SplitTableTest
  extends JPanel
{
  public static void main( String[] args ) {
     EventQueue.invokeLater( new Runnable() {
        public void run() {
           JFrame frame = new JFrame();
           frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
           frame.setLocation( 200, 200 );
           frame.getContentPane().add( new SplitTableTest() );
           frame.pack();
           frame.setVisible( true );
        }
     } );
  }

  public SplitTableTest() {
     setLayout( new BorderLayout() );
     
     TableModel baseModel = new TestTableModel();
     JTable table0 = new JTable( new SplitTableModel( baseModel, 0, 2 ) );
     JTable table1 = new JTable( new SplitTableModel( baseModel, 1, 2 ) );

     GridLayout gridLayout = new GridLayout( 1, 2 );
     gridLayout.setHgap( 10 );
     
     JPanel headerPanel = new JPanel( gridLayout );
     headerPanel.add( table0.getTableHeader() );
     headerPanel.add( table1.getTableHeader() );

     JPanel tablePanel = new JPanel( gridLayout );
     tablePanel.add( table0 );
     tablePanel.add( table1 );
     
     JScrollPane scroller = new JScrollPane( tablePanel );
     scroller.setColumnHeaderView( headerPanel );
     add( scroller );
  }
 
  private static final class SplitTableModel
     implements TableModel
  {
     private TableModel baseModel;
     private int segment;
     private int segments;

     public SplitTableModel( TableModel baseModel, int segment,
        int segments )
    {
        this.baseModel = baseModel;
        this.segment = segment;
        this.segments = segments;
     }

     public int getRowCount() {
        int baseRows = baseModel.getRowCount();
        int rows = baseRows / segments;
        if ( ( baseRows % segments ) > segment ) {
           ++ rows;
        }
        return rows;
     }

     public int getColumnCount() {
        return baseModel.getColumnCount();
     }

     public String getColumnName( int column ) {
        return baseModel.getColumnName( column );
     }

     public Class getColumnClass( int column ) {
        return baseModel.getColumnClass( column );
     }

     public boolean isCellEditable( int row, int column ) {
        return false;
     }

     public Object getValueAt( int row, int column ) {
        return baseModel.getValueAt( mapRow(row), column );
     }

     public void setValueAt( Object aValue, int row, int column ) {
     }

     public void addTableModelListener( TableModelListener l ) {
     }

     public void removeTableModelListener( TableModelListener l ) {
     }

     private int mapRow( int row ) {
        int baseRows = baseModel.getRowCount();
        int firstRow = segment * baseRows / segments
           + Math.min( segment, baseRows % segments );
        return ( row + firstRow );
     }
  }
}
=========================================================================

Signature

Regards,

John McGrath

farseer - 21 Mar 2005 01:38 GMT
thanks john,
i really appreciate the code snippet.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.