Code Included: Freeze column

Discussion in 'Java' started by Haircuts Are Important, May 29, 2013.

  1. I attempted to modify 2 different projects on the Internet.

    As you know, I have been trying to duplicate the Excel spreadsheet
    freeze column capability while maintaining scrollability (vertical
    across the 2 tables).

    First, my problem with a modified version (added dynamically adding
    rows to the 2 tables using "insert") of the code at
    http://www.java2s.com/Code/Java/Swing-Components/FixedTableColumnExample.htm
    produced code where the 2 tables wouldn't do exactly what I wanted.
    Basically, I have been unable to get the the 2 tables to scroll
    vertically together etc.

    Second below, if I change the dimension of either JScrollPane it
    changes the second one. So, the result is I either get a large left
    JScrollPane or a small right JScrollPane.

    I'd also like to remove any spacing between these 2 tables.

    Third, I also tried to put each table into a separate JPanel and put
    all this into a JScrollPane, but it (appeared to) yield the same
    results as the second thing I tried.

    What are the details to accomplish this.

    Thanks,

    class ParallelTables {
    static JScrollPane createTableA() {
    DefaultTableModel model = new DefaultTableModel(100, 1);
    for (int row=model.getRowCount(); --row>=0;) {
    model.setValueAt(row, row, 0);
    }
    JTable table = new JTable(model);
    return new JScrollPane(table);
    }

    static JScrollPane createTableB() {
    DefaultTableModel model = new DefaultTableModel(100, 5);
    for (int row=model.getRowCount(); --row>=0;) {
    model.setValueAt(row, row, 0);
    }
    JTable table = new JTable(model);
    return new JScrollPane(table);
    }

    public static void main(String[] args) throws Exception {

    JScrollPane scrollerA = createTableA();
    JScrollPane scrollerB = createTableB();
    scrollerA.setVerticalScrollBarPolicy(
    JScrollPane.VERTICAL_SCROLLBAR_NEVER);
    // the following statement binds the same BoundedRangeModel to
    both vertical scrollbars.
    scrollerA.getVerticalScrollBar().setModel(
    scrollerB.getVerticalScrollBar().getModel());
    JPanel panel = new JPanel();
    panel.add(scrollerA);
    panel.add(scrollerB);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
    }
    }
     
    Haircuts Are Important, May 29, 2013
    #1
    1. Advertising

  2. In article
    <>,
    Haircuts Are Important <> wrote:

    > Basically, I have been unable to get the the 2 tables to scroll
    > vertically together etc.


    Your example using a shared model seems to do exactly this.

    > Second below, if I change the dimension of either JScrollPane it
    > changes the second one. So, the result is I either get a large left
    > JScrollPane or a small right JScrollPane.


    JTable implements Scrollable; you can override the method
    getPreferredScrollableViewportSize() to make table A narrower:

    final JTable table = new JTable(model) {
    @Override
    public Dimension getPreferredScrollableViewportSize() {
    Dimension d = super.getPreferredScrollableViewportSize();
    return new Dimension(d.width / 2, d.height);
    }
    };

    > I'd also like to remove any spacing between these 2 tables.


    The space comes from the default FlowLayout; try this:

    JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));

    > Third, I also tried to put each table into a separate JPanel and put
    > all this into a JScrollPane, but it (appeared to) yield the same
    > results as the second thing I tried.


    I don't understand.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, May 29, 2013
    #2
    1. Advertising

  3. Haircuts Are Important

    Guest

    On Wednesday, May 29, 2013 4:29:37 PM UTC-4, John B. Matthews wrote:
    > In article <>, Haircuts Are Important <> wrote: > Basically, I have beenunable to get the the 2 tables to scroll > vertically together etc. Your example using a shared model seems to do exactly this. > Second below, if I change the dimension of either JScrollPane it > changes the second one. So,the result is I either get a large left > JScrollPane or a small right JScrollPane. JTable implements Scrollable; you can override the method getPreferredScrollableViewportSize() to make table A narrower: final JTable table = new JTable(model) { @Override public Dimension getPreferredScrollableViewportSize() { Dimension d = super.getPreferredScrollableViewportSize(); return new Dimension(d.width / 2, d.height); } }; > I'd also like to removeany spacing between these 2 tables. The space comes from the default FlowLayout; try this: JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0)); > Third, I also tried to put each table into a separate JPanel and put > all this into a JScrollPane, but it (appeared to) yield the same > results as the second thing I tried. I don't understand. -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>



    >> Basically, I have been unable to get the the 2 tables to scroll
    >> vertically together etc.


    > Your example using a shared model seems to do exactly this.


    Yes, but I have to throw away that model in favor of another one because I have to continually add rows to the tables as the project runs. The shared model in the example doesn't allow me to use the "insert" method which is available in another model.

    I.E.: The shared model example populates the tables all at once and not sporadically!

    How can I add new rows to the two tables of the shared model example every few minutes and not all at once.

    Thanks
     
    , May 30, 2013
    #3
  4. In article <>,
    wrote:

    > Yes, but I have to throw away that model in favor of another one
    > because I have to continually add rows to the tables as the project
    > runs. The shared model in the example doesn't allow me to use the
    > "insert" method which is available in another model.
    >
    > I.E.: The shared model example populates the tables all at once and
    > not sporadically!
    >
    > How can I add new rows to the two tables of the shared model example
    > every few minutes and not all at once.


    Given two row-conformal models, I'd use javax.swing.Timer:

    Timer t = new Timer(1000, new ActionListener() { // ~ 1 Hz
    @Override
    public void actionPerformed(ActionEvent e) {
    modelA.addRow(new Object[]{42});
    modelB.addRow(new Object[]{42, 42, 42, 42, 42});
    JScrollBar vertical = scrollerA.getVerticalScrollBar();
    vertical.setValue(vertical.getMaximum());
    }
    });
    t.start();

    <http://stackoverflow.com/a/5150437/230513>

    --
    [Please omit signatures.]
     
    John B. Matthews, May 31, 2013
    #4
  5. I have modified the code at the below link. Everything seems to work
    except horizontal scrolling across the two tables:
    http://www.java2s.com/Code/Java/Swing-Components/FixedTableColumnExample.htm.

    Again, at run-time (via the routine attached to the timer) the project
    will add different data to the bottom of each table. The bottom row is
    highlighted in each table. The frozen columns are doing what they're
    suppose to.

    But, when I use the scrollbar on the right table, the left table does
    not move.

    Here is a non-compilable summary of the resulting code. (The timer
    class is not there) Can you get the left table to match the scrolling
    on the right table?

    Thanks

    package test;

    class Test extends JFrame{
    public static FixedModel fixedModel;//FixedModel extends
    AbstractTableModel see below
    public static ModelWithScrollbar mainModel;//ModelWithScrollbar
    extends AbstractTableModel see below
    public static JTable fixedTable; //Has the frozen columns
    public static JTable scrollTable; //Has a visible scrollbar

    public Test ()[

    t.startTimer();

    this.setVisible(true);
    this.setTitle ("An example");

    fixedModel = new FixedModel ();

    fixedTable = new JTable (fixedModel){
    @Override
    public void valueChanged(ListSelectionEvent e){
    super.valueChanged(e);
    checkSelection(true);
    }
    };

    mainModel = new ModelWithScrollbar();

    scrollTable = new JTable (mainModel){
    @Override
    public void valueChanged(ListSelectionEvent e){
    super.valueChanged(e);
    checkSelection(false);
    }
    };

    Dimension dimensin = new Dimensin(0,0);
    fixedTable.getTableHeader().setPreferredSize(dimension);
    scrollTable.getTableHeader().setPreferredSize(dimension);

    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    scrollTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    scrollTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    scrollTable.setFocusable(false);

    JScrollPane jScrollPane1 = new JScrollPane(scrollTable);

    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedTable,getPreferredSize());
    jScrollPane1.setRowHeaderView(viewport);

    jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORNER,fixedTable.getTableHeader());
    getContentPane().add(jScrollPane1,BorderLayout.CENTER);
    }

    //Indirectly called via Timer t
    public static void add_Rows_To_Tables (){

    String[] tmp1 = new String [3];
    String[] tmp2 = new String [90];

    //...
    fixedModel.addRow(Arrays.asList(tmp1));
    mainModel.addRow(Arrays.asList(tmp2));

    fixedTable.getSelectionMode().setSelectionInterval(
    fixedTable.getRowCount () - 1,
    fixedTable.getRowCount () - 1);

    scrollTable.getSelectionMode().setSelectionInterval(
    scrollTable.getRowCount () - 1,
    scrollTable.getRowCount () - 1);

    fixedTable.scrollRectToVisible(
    new Rectangle (
    fixedTable.getCellRect(
    fixedTable.getRowCount() - 1,
    0,
    true)));

    scrollTable.scrollRectToVisible(
    new Rectangle (
    scrollTable.getCellRect(
    scrollTable.getRowCount() - 1,
    0,
    true)));
    }

    private void checkSelection(boolean isFixedTable){
    int fixedSelectedIndex = fixedTaboe.geSelectedRow();
    int selectedIndex = scrollTable.getSelectedRow();

    if (fixedSelectedIndex != selectedIndex){
    if (isFixedTable){

    scrollTable.setRowSelectionInterval(fixedSelectedIndex,fixedSelectedIndex);
    } else {

    fixedTable.setRowSelectionInterval(selectedIndex,selectedIndex);
    }
    }
    }

    public static main (String[] args){
    Test frame = new Test ();

    frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    frame.setVisible (true);
    }
    }




    public class FixedModel extends AbstractTableModel{
    static public List<String> columnNames = new ArrayList();
    static public List<List> data = new ArrayList();

    {//annonymous class
    for (int i = 0;i < 3; i++){
    columnNames.add(" ");
    }
    }

    public void addRow(List rowData){
    data.add(rowData);
    fireTableRowsInserted(data.size() - 1,data.size() - 1);
    }

    public int getColumnCount() {
    return columnNames.size();
    }

    public int getRowCount() {
    return data.size();
    }

    @Override
    public String getColumnName(int col) {
    try {
    return columnNames.get(col);
    } catch (Exception exception) {
    return null;
    }
    }

    public Object getValueAt(int row, int col) {
    return data.get(row).get(col);
    }

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

    public Class getColumnClass(int c) {
    return getValueAt(0,c).getClass();
    }
    };





    public class ModelWithScrollbar extends AbstractTableModel{
    static public List<String> columnNames = new ArrayList();
    static public List<List> data = new ArrayList();

    {//annonymous class
    for (int i = 0;i < 90; i++){
    columnNames.add(" ");
    }
    }

    public void addRow(List rowData){
    data.add(rowData);
    fireTableRowsInserted(data.size() - 1,data.size() - 1);
    }

    public int getColumnCount() {
    return columnNames.size();
    }

    public int getRowCount() {
    return data.size();
    }

    @Override
    public String getColumnName(int col) {
    try {
    return columnNames.get(col);
    } catch (Exception exception) {
    return null;
    }
    }

    public Object getValueAt(int row, int col) {
    return data.get(row).get(col);
    }

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

    public Class getColumnClass(int c) {
    try {
    return getValueAt(0,c).getClass();
    }
    catch (Exceptin exception) {// MAY LEAD TO ERROR
    return this.getClass();
    }
    }
    };
     
    Haircuts Are Important, May 31, 2013
    #5
  6. I have modified the code at the below link. Everything seems to work
    except horizontal scrolling across the two tables:
    http://www.java2s.com/Code/Java/Swing-Components/FixedTableColumnExam....

    Again, at run-time (via the routine attached to the timer) the
    project
    will add different data to the bottom of each table. The bottom row
    is
    highlighted in each table. The frozen columns are doing what they're
    suppose to.


    But, when I use the scrollbar on the right table, the left table does
    not move.


    Here is a non-compilable summary of the resulting code. (The timer
    class is not there) Can you get the left table to match the scrolling
    on the right table?


    Thanks


    package test;

    class Test extends JFrame{
    public static FixedModel fixedModel;//FixedModel extends
    AbstractTableModel see below
    public static ModelWithScrollbar mainModel;//ModelWithScrollbar
    extends AbstractTableModel see below
    public static JTable fixedTable; //Has the frozen columns
    public static JTable scrollTable; //Has a visible scrollbar


    public Test (){

    t.startTimer();

    this.setVisible(true);
    this.setTitle ("An example");

    fixedModel = new FixedModel ();

    fixedTable = new JTable (fixedModel){
    @Override
    public void valueChanged(ListSelectionEvent e){
    super.valueChanged(e);
    checkSelection(true);
    }
    };

    mainModel = new ModelWithScrollbar();

    scrollTable = new JTable (mainModel){
    @Override
    public void valueChanged(ListSelectionEvent e){
    super.valueChanged(e);
    checkSelection(false);
    }
    };


    Dimension dimension = new Dimension(0,0);
    fixedTable.getTableHeader().setPreferredSize(dimension);
    scrollTable.getTableHeader().setPreferredSize(dimension);

    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    scrollTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    scrollTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    scrollTable.setFocusable(false);

    JScrollPane jScrollPane1 = new JScrollPane(scrollTable);

    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedTable,getPreferredSize());
    jScrollPane1.setRowHeaderView(viewport);


    jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORNER,fixedTable.getTableHea­
    der());
    getContentPane().add(jScrollPane1,BorderLayout.CENTER);


    }


    //Indirectly called via Timer t
    public static void add_Rows_To_Tables (){

    String[] tmp1 = new String [3];
    String[] tmp2 = new String [90];

    //...
    fixedModel.addRow(Arrays.asList(tmp1));
    mainModel.addRow(Arrays.asList(tmp2));

    fixedTable.getSelectionMode().setSelectionInterval(
    fixedTable.getRowCount () - 1,
    fixedTable.getRowCount () - 1);

    scrollTable.getSelectionMode().setSelectionInterval(
    scrollTable.getRowCount () - 1,
    scrollTable.getRowCount () - 1);

    fixedTable.scrollRectToVisible(
    new Rectangle (
    fixedTable.getCellRect(
    fixedTable.getRowCount() - 1,
    0,
    true)));


    scrollTable.scrollRectToVisible(
    new Rectangle (
    scrollTable.getCellRect(
    scrollTable.getRowCount() - 1,
    0,
    true)));
    }


    private void checkSelection(boolean isFixedTable){
    int fixedSelectedIndex = fixedTable.geSelectedRow();
    int selectedIndex = scrollTable.getSelectedRow();

    if (fixedSelectedIndex != selectedIndex){
    if (isFixedTable){

    scrollTable.setRowSelectionInterval(fixedSelectedIndex,fixedSelectedIndex);
    } else {

    fixedTable.setRowSelectionInterval(selectedIndex,selectedIndex);
    }
    }
    }


    public static main (String[] args){
    Test frame = new Test ();

    frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    frame.setVisible (true);
    }
    }


    public class FixedModel extends AbstractTableModel{
    static public List<String> columnNames = new ArrayList();
    static public List<List> data = new ArrayList();

    {//annonymous class
    for (int i = 0;i < 3; i++){
    columnNames.add(" ");
    }
    }


    public void addRow(List rowData){
    data.add(rowData);
    fireTableRowsInserted(data.size() - 1,data.size() - 1);
    }


    public int getColumnCount() {
    return columnNames.size();
    }

    public int getRowCount() {
    return data.size();
    }

    @Override
    public String getColumnName(int col) {
    try {
    return columnNames.get(col);
    } catch (Exception exception) {
    return null;
    }
    }

    public Object getValueAt(int row, int col) {
    return data.get(row).get(col);
    }


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

    public Class getColumnClass(int c) {
    return getValueAt(0,c).getClass();
    }
    };


    public class ModelWithScrollbar extends AbstractTableModel{
    static public List<String> columnNames = new ArrayList();
    static public List<List> data = new ArrayList();

    {//annonymous class
    for (int i = 0;i < 90; i++){
    columnNames.add(" ");
    }
    }


    public void addRow(List rowData){
    data.add(rowData);
    fireTableRowsInserted(data.size() - 1,data.size() - 1);
    }

    public int getColumnCount() {
    return columnNames.size();
    }

    public int getRowCount() {
    return data.size();
    }

    @Override
    public String getColumnName(int col) {
    try {
    return columnNames.get(col);
    } catch (Exception exception) {
    return null;
    }
    }

    public Object getValueAt(int row, int col) {
    return data.get(row).get(col);
    }

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

    public Class getColumnClass(int c) {
    try {
    return getValueAt(0,c).getClass();
    }
    catch (Exception exception) {// MAY LEAD TO ERROR
    return this.getClass();
    }
    }
    };
     
    Haircuts Are Important, May 31, 2013
    #6
  7. Haircuts Are Important

    Lew Guest

    Haircuts Are Important wrote:
    > public class ModelWithScrollbar extends AbstractTableModel{
    > static public List<String> columnNames = new ArrayList();
    > static public List<List> data = new ArrayList();
    >
    > {//annonymous [sic] class


    There is no anonymous class here. Why the comment?

    > for (int i = 0;i < 90; i++){
    > columnNames.add(" ");
    > }
    > }
    >
    > public void addRow(List rowData){
    > data.add(rowData);
    > fireTableRowsInserted(data.size() - 1,data.size() - 1);
    > }

    ....
    > };
     
    Lew, May 31, 2013
    #7
  8. On May 31, 2:43 pm, Lew <> wrote:
    > Haircuts Are Important wrote:
    > > public class ModelWithScrollbar extends AbstractTableModel{
    > >    static public List<String> columnNames = new ArrayList();
    > >    static public List<List> data = new ArrayList();

    >
    > >    {//annonymous [sic] class

    >
    > There is no anonymous class here. Why the comment?
    > >        for (int i = 0;i < 90; i++){
    > >           columnNames.add(" ");
    > >        }
    > >    }


    If it's not an annonymous class what is it called.

    Thanks,
     
    Haircuts Are Important, May 31, 2013
    #8
  9. On May 31, 2:35 pm, Haircuts Are Important <> wrote:
    > I have modified the code at the below link. Everything seems to work


    Let me try to clarify the problem, when I use the vertical
    scrollbar on the right table, the left table does not move.

    Any assistance would be greatly appreciated!

    Thanks,
     
    Haircuts Are Important, May 31, 2013
    #9
  10. Haircuts Are Important

    Lew Guest

    Haircuts Are Important wrote:
    >Lew wrote:
    >> Haircuts Are Important wrote:
    >>> public class ModelWithScrollbar extends AbstractTableModel{
    > > >    static public List<String> columnNames = new ArrayList();
    > > >    static public List<List> data = new ArrayList();

    > >
    > > >    {//annonymous [sic] class

    > >
    > > There is no anonymous class here. Why the comment?

    >
    > > >        for (int i = 0;i < 90; i++){
    > > >           columnNames.add(" ");
    > > >        }
    > > >    }

    >
    > If it's not an annonymous [sic] class what is it called.


    If by "it" you mean the block with the 'for' loop in it, that is an initializer.
    It is not any kind of class, let alone an anonymous one.

    It is also a bug. You are using an instance initializer to initialize a static
    construct.

    That means that every instance of that class, should there ever be more
    than one, will add another 90 items to the 'columnNames' collection, resulting
    in a total size in excess of 90.

    It is usually a bad idea to initialize a static structure in an instance context.

    Study the following terms to be clear on these matters: "initializer" (both"static initializer"
    and "instance initializer"), "class", "anonymous class", "static member".

    --
    LEw
     
    Lew, May 31, 2013
    #10
  11. On May 31, 3:37 pm, Lew <> wrote:
    > Haircuts Are Important wrote:
    > >Lew wrote:
    > >> Haircuts Are Important wrote:
    > >>> public class ModelWithScrollbar extends AbstractTableModel{
    > > > >    static public List<String> columnNames = new ArrayList();
    > > > >    static public List<List> data = new ArrayList();

    >
    > > > >    {//annonymous [sic] class

    >
    > > > There is no anonymous class here. Why the comment?

    >
    > > > >        for (int i = 0;i < 90; i++){
    > > > >           columnNames.add(" ");
    > > > >        }
    > > > >    }

    >
    > > If it's not an annonymous [sic] class what is it called.

    >
    > If by "it" you mean the block with the 'for' loop in it, that is an initializer.
    > It is not any kind of class, let alone an anonymous one.
    >
    > It is also a bug. You are using an instance initializer to initialize a static
    > construct.
    >
    > That means that every instance of that class, should there ever be more
    > than one, will add another 90 items to the 'columnNames' collection, resulting
    > in a total size in excess of 90.
    >
    > It is usually a bad idea to initialize a static structure in an instance context.
    >
    > Study the following terms to be clear on these matters: "initializer" (both "static initializer"
    > and "instance initializer"), "class", "anonymous class", "static member".
    >


    Thanks,
     
    Haircuts Are Important, May 31, 2013
    #11
  12. In article
    <>,
    Haircuts Are Important <> wrote:

    > Everything seems to work except horizontal scrolling
    > across the two tables


    You can tinker with setAutoResizeMode(); but absent some reason to
    the contrary, I usually rely on the default.

    For improved visual coherence, the tables can share a selection
    model, much as you have done with the scrollbars' BoundedRangeModel:

    tableA.setSelectionModel(tableB.getSelectionModel());

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 1, 2013
    #12
  13. I'm lost here, can you give me more ideas? Problem: Both horizontal
    and vertical scrolling works on the right table. But, vertical
    scrolling on the right table does not scroll the left table!

    On May 31, 11:59 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >  Haircuts Are Important <> wrote:
    >
    > > Everything seems to work except horizontal scrolling
    > > across the two tables

    >
    > You can tinker with setAutoResizeMode(); but absent some reason to
    > the contrary, I usually rely on the default.
    >



    Horizontal scrolling is working on the right table, so I don't think I
    should be modifying setAutoResizeMode().


    > For improved visual coherence, the tables can share a selection
    > model, much as you have done with the scrollbars' BoundedRangeModel:
    >
    > tableA.setSelectionModel(tableB.getSelectionModel());
    >



    Scrolling the right table vertically does not affect the row that is
    highlighted.
    So applying your idea via the below code is useless!

    SharedClass implements ListselectionListener {
    public void valueChanged (ListSelectionEvent evt){
    int index = evt.getFirstIndex();
    System.out.println ("Row = " + index);
    }
    }

    listSelectionModel listSelectionModel = scrollTable.getSelectionModel
    ();
    listSelectionModel.addListSelectionListener(new SharedClass());
    scrollTable.setSelectionModel(listSelectionModel);
    fixedModel.setSelectionModel (scrollTable.getSelectionModel());



    //ORIGINAL CODE BELOW
    package test;

    public class ATimer {
    public sttic void ATimer (){}
    public static void startTimer(){
    Timer timer;
    timer = new Timer ();
    timer.schedule (new TheData(),0,5000);
    }
    }

    public class TheData extends TimerTask{//REALLY UNUSED CLASS
    public void run(){//Data broken up to send to tables separately
    //...
    }
    }

    class Test extends JFrame{
    public static FixedModel fixedModel;
    public static ModelWithScrollbar mainModel;
    public static JTable fixedTable;
    public static JTable scrollTable;

    public Test (){

    ATimer.startTimer(); // Details omitted

    this.setVisible(true);
    this.setTitle ("An example");

    fixedModel = new FixedModel ();

    fixedTable = new JTable (fixedModel){
    @Override
    public void valueChanged(ListSelectionEvent e){
    super.valueChanged(e);
    checkSelection(true);
    }
    };

    mainModel = new ModelWithScrollbar();

    scrollTable = new JTable (mainModel){
    @Override
    public void valueChanged(ListSelectionEvent e){
    super.valueChanged(e);
    checkSelection(false);
    }
    };

    Dimension dimension = new Dimension(0,0);
    fixedTable.getTableHeader().setPreferredSize(dimension);
    scrollTable.getTableHeader().setPreferredSize(dimension);

    fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    scrollTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

    fixedTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    scrollTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

    scrollTable.setFocusable(false);

    JScrollPane jScrollPane1 = new JScrollPane(scrollTable);

    JViewport viewport = new JViewport();
    viewport.setView(fixedTable);
    viewport.setPreferredSize(fixedTable,getPreferredSize());
    jScrollPane1.setRowHeaderView(viewport);

    jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORNER,
    fixedTable.getTableHea­­der());
    getContentPane().add(jScrollPane1,BorderLayout.CENTER);
    }

    //Indirectly called via Timer t
    public static void add_Rows_To_Tables (){

    String[] tmp1 = new String [1]; //A, B, C, D, E ,F etc
    String[] tmp2 = new String [10]; //

    //...
    fixedModel.addRow(Arrays.asList(tmp1));
    mainModel.addRow(Arrays.asList(tmp2));

    fixedTable.getSelectionMode().setSelectionInterval(
    fixedTable.getRowCount () - 1,
    fixedTable.getRowCount () - 1);

    scrollTable.getSelectionMode().setSelectionInterval(
    scrollTable.getRowCount () - 1,
    scrollTable.getRowCount () - 1);

    fixedTable.scrollRectToVisible(
    new Rectangle (
    fixedTable.getCellRect(
    fixedTable.getRowCount() - 1,
    0,
    true)));

    scrollTable.scrollRectToVisible(
    new Rectangle (
    scrollTable.getCellRect(
    scrollTable.getRowCount() - 1,
    0,
    true)));
    }

    private void checkSelection(boolean isFixedTable){
    int fixedSelectedIndex = fixedTable.geSelectedRow();
    int selectedIndex = scrollTable.getSelectedRow();

    if (fixedSelectedIndex != selectedIndex){
    if (isFixedTable){


    scrollTable.setRowSelectionInterval(fixedSelectedIndex,fixedSelectedIndex);
    } else {

    fixedTable.setRowSelectionInterval(selectedIndex,selectedIndex);
    }
    }
    }

    public static main (String[] args){
    Test frame = new Test ();

    frame.addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });
    frame.setVisible (true);
    }
    }

    public class FixedModel extends AbstractTableModel{
    static public List<String> columnNames = new ArrayList();
    static public List<List> data = new ArrayList();

    {
    for (int i = 0;i < 1; i++){
    columnNames.add(" ");
    }
    }

    public void addRow(List rowData){
    data.add(rowData);
    fireTableRowsInserted(data.size() - 1,data.size() - 1);
    }

    public int getColumnCount() {
    return columnNames.size();
    }

    public int getRowCount() {
    return data.size();
    }

    @Override
    public String getColumnName(int col) {
    try {
    return columnNames.get(col);
    } catch (Exception exception) {
    return null;
    }
    }

    public Object getValueAt(int row, int col) {
    return data.get(row).get(col);
    }

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

    public Class getColumnClass(int c) {
    return getValueAt(0,c).getClass();
    }

    };


    public class ModelWithScrollbar extends AbstractTableModel{
    static public List<String> columnNames = new ArrayList();
    static public List<List> data = new ArrayList();

    {
    for (int i = 0;i < 10; i++){
    columnNames.add(" ");
    }
    }

    public void addRow(List rowData){
    data.add(rowData);
    fireTableRowsInserted(data.size() - 1,data.size() - 1);
    }

    public int getColumnCount() {
    return columnNames.size();
    }

    public int getRowCount() {
    return data.size();
    }

    @Override
    public String getColumnName(int col) {
    try {
    return columnNames.get(col);
    } catch (Exception exception) {
    return null;
    }
    }

    public Object getValueAt(int row, int col) {
    return data.get(row).get(col);
    }

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

    public Class getColumnClass(int c) {
    try {
    return getValueAt(0,c).getClass();
    }
    catch (Exception exception) {// MAY LEAD TO ERROR
    return this.getClass();
    }
    }
    };
     
    Haircuts Are Important, Jun 3, 2013
    #13
  14. Per a definitoin for JViewport, do you think that my class
    ModelWithScrollbar and class FixedModel should be sharing the exact
    same members "data" and "columnNames" instead of having two pairs.

    Thank you,
     
    Haircuts Are Important, Jun 3, 2013
    #14
  15. In article
    <>,
    Haircuts Are Important <> wrote:

    > > For improved visual coherence, the tables can share a selection
    > > model, much as you have done with the scrollbars'
    > > BoundedRangeModel:
    > >
    > > tableA.setSelectionModel(tableB.getSelectionModel());

    >
    > Scrolling the right table vertically does not affect the row that is
    > highlighted. So applying your idea via the below code is useless!


    I mistakenly thought you wanted to synchronize vertical scrolling
    between the two tables. I don't use Excel enough to understand its
    "freeze column capability."

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Jun 4, 2013
    #15
  16. On Jun 3, 11:03 pm, "John B. Matthews" <> wrote:
    > In article
    > <>,
    >  Haircuts Are Important <> wrote:
    >
    > > > For improved visual coherence, the tables can share a selection
    > > > model, much as you have done with the scrollbars'
    > > > BoundedRangeModel:

    >
    > > > tableA.setSelectionModel(tableB.getSelectionModel());

    >
    > > Scrolling the right table vertically does not affect the row that is
    > > highlighted. So applying your idea via the below code is useless!

    >
    > I mistakenly thought you wanted to synchronize vertical scrolling
    > between the two tables. I don't use Excel enough to understand its
    > "freeze column capability."
    >


    The capability that I am trying to get is this. I have two tables with
    the right one being a lot larger than the left one. What I have been
    attempting to do is scroll the right table vertically and have the
    left table scroll vertically at the same time.

    I already have the "freeze" column capability working in the code
    above.

    I have not modified the code that I have recently posted.

    Thanks for any help,
     
    Haircuts Are Important, Jun 4, 2013
    #16
  17. Haircuts Are Important

    Lew Guest

    > Haircuts Are Important wrote:
    > > > For improved visual coherence, the tables can share a selection
    > > > model, much as you have done with the scrollbars'
    > > > BoundedRangeModel:

    ....

    What happened to all your posts?

    --
    Lew
     
    Lew, Jun 4, 2013
    #17
  18. On Jun 4, 3:17 pm, Lew <> wrote:
    > >  Haircuts Are Important wrote:
    > > > > For improved visual coherence, the tables can share a selection
    > > > > model, much as you have done with the scrollbars'
    > > > > BoundedRangeModel:

    >
    > ...
    >
    > What happened to all your posts?


    I'm still listening.

    Thanks,
     
    Haircuts Are Important, Jun 4, 2013
    #18
  19. Haircuts Are Important

    Lew Guest

    Haircuts Are Important wrote:
    > Lew wrote:
    > > >  Haircuts Are Important wrote:
    > > > > > For improved visual coherence, the tables can share a selection
    > > > > > model, much as you have done with the scrollbars'
    > > > > > BoundedRangeModel:

    >
    > > ...

    >
    > > What happened to all your posts?

    >
    > I'm still listening.


    That's nice.

    What happened to all your posts?

    --
    Lew
     
    Lew, Jun 4, 2013
    #19
  20. Haircuts Are Important

    Joerg Meier Guest

    On Tue, 4 Jun 2013 14:17:29 -0700 (PDT), Lew wrote:

    > Haircuts Are Important wrote:
    >> Lew wrote:
    >>> >  Haircuts Are Important wrote:
    >>> > > > For improved visual coherence, the tables can share a selection
    >>> > > > model, much as you have done with the scrollbars'
    >>> > > > BoundedRangeModel:
    >>> ...
    >>> What happened to all your posts?

    >> I'm still listening.

    > That's nice.


    > What happened to all your posts?


    If you have issues with the posts of multiple people, the issue might not
    be on their eind.

    Liebe Gruesse,
    Joerg

    --
    Ich lese meine Emails nicht, replies to Email bleiben also leider
    ungelesen.
     
    Joerg Meier, Jun 4, 2013
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmVu?=

    Freeze column(s) in a Datagrid

    =?Utf-8?B?QmVu?=, Jun 5, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,024
    Alvin Bruney [MVP]
    Jun 5, 2004
  2. alphanode
    Replies:
    1
    Views:
    480
    G.Strand
    Apr 25, 2005
  3. Wagner Dias
    Replies:
    3
    Views:
    3,062
    Andre Kostur
    Mar 28, 2005
  4. Seth Williams

    freeze column headings

    Seth Williams, Oct 17, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    439
  5. Replies:
    5
    Views:
    255
    Olivier Renaud
    Mar 15, 2007
Loading...

Share This Page