Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #24193 > unrolled thread

Code Included: Freeze column

Started byHaircuts Are Important <clusardi2k@aol.com>
First post2013-05-29 12:55 -0700
Last post2013-05-31 12:30 -0700
Articles 20 on this page of 33 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-05-29 12:55 -0700
    Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-05-29 16:29 -0400
      Re: Code Included: Freeze column clusardi2k@aol.com - 2013-05-29 23:53 -0700
        Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-05-30 21:40 -0400
          Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-05-31 11:19 -0700
            Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-05-31 23:59 -0400
              Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-06-03 10:18 -0700
                Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-06-03 12:48 -0700
                Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-06-03 23:03 -0400
                  Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-06-04 05:05 -0700
                    Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-06-04 23:04 -0400
                      Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-06-05 11:53 -0700
                        Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-06-05 16:02 -0400
                          Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-06-05 13:09 -0700
                          Re: Code Included: Freeze column clusardi2k@aol.com - 2013-06-05 16:27 -0700
                            Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-06-05 17:35 -0700
                            Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-06-06 21:42 -0400
                              Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-06-10 09:34 -0700
                                Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-06-10 13:49 -0400
                  Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-06-04 12:17 -0700
                    Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-06-04 12:54 -0700
                      Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-06-04 14:17 -0700
                        Re: Code Included: Freeze column Joerg Meier <joergmmeier@arcor.de> - 2013-06-05 00:12 +0200
                          Re: Code Included: Freeze column "John B. Matthews" <nospam@nospam.invalid> - 2013-06-04 21:39 -0400
                          Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-06-04 19:48 -0700
                            Re: Code Included: Freeze column Joerg Meier <joergmmeier@arcor.de> - 2013-06-05 07:36 +0200
                              Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-06-04 22:46 -0700
          Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-05-31 11:35 -0700
            Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-05-31 11:43 -0700
              Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-05-31 11:50 -0700
                Re: Code Included: Freeze column Lew <lewbloch@gmail.com> - 2013-05-31 12:37 -0700
                  Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-05-31 12:44 -0700
            Re: Code Included: Freeze column Haircuts Are Important <clusardi2k@aol.com> - 2013-05-31 12:30 -0700

Page 1 of 2  [1] 2  Next page →


#24193 — Code Included: Freeze column

FromHaircuts Are Important <clusardi2k@aol.com>
Date2013-05-29 12:55 -0700
SubjectCode Included: Freeze column
Message-ID<566ff119-faa8-4116-9761-65002a3c1cb3@w8g2000yqf.googlegroups.com>
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);
    }
}

[toc] | [next] | [standalone]


#24194

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-05-29 16:29 -0400
Message-ID<nospam-D730B4.16293729052013@news.aioe.org>
In reply to#24193
In article 
<566ff119-faa8-4116-9761-65002a3c1cb3@w8g2000yqf.googlegroups.com>,
 Haircuts Are Important <clusardi2k@aol.com> 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>

[toc] | [prev] | [next] | [standalone]


#24198

Fromclusardi2k@aol.com
Date2013-05-29 23:53 -0700
Message-ID<b838090c-9d8e-48b2-bc22-582f7c61efb4@googlegroups.com>
In reply to#24194
On Wednesday, May 29, 2013 4:29:37 PM UTC-4, John B. Matthews wrote:
> In article <566ff119-faa8-4116-9761-65002a3c1cb3@w8g2000yqf.googlegroups.com>, Haircuts Are Important <clus@aol.com> 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>


>> 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  

[toc] | [prev] | [next] | [standalone]


#24200

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-05-30 21:40 -0400
Message-ID<nospam-6B3694.21402330052013@news.aioe.org>
In reply to#24198
In article <b838090c-9d8e-48b2-bc22-582f7c61efb4@googlegroups.com>,
 clusardi2k@aol.com 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.]

[toc] | [prev] | [next] | [standalone]


#24206

FromHaircuts Are Important <clusardi2k@aol.com>
Date2013-05-31 11:19 -0700
Message-ID<3e3557ff-431c-4a9f-bf7c-777eeaeb65f7@w15g2000vbn.googlegroups.com>
In reply to#24200
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();
         }
      }
};










[toc] | [prev] | [next] | [standalone]


#24219

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-05-31 23:59 -0400
Message-ID<nospam-7A73BC.23591631052013@news.aioe.org>
In reply to#24206
In article 
<3e3557ff-431c-4a9f-bf7c-777eeaeb65f7@w15g2000vbn.googlegroups.com>,
 Haircuts Are Important <clusardi2k@aol.com> 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>

[toc] | [prev] | [next] | [standalone]


#24276

FromHaircuts Are Important <clusardi2k@aol.com>
Date2013-06-03 10:18 -0700
Message-ID<653739d6-b70e-4e57-9af2-2dfaa66fec79@k4g2000yqb.googlegroups.com>
In reply to#24219
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" <nos...@nospam.invalid> wrote:
> In article
> <3e3557ff-431c-4a9f-bf7c-777eeaeb6...@w15g2000vbn.googlegroups.com>,
>  Haircuts Are Important <clusard...@aol.com> 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();
         }
      }
};

[toc] | [prev] | [next] | [standalone]


#24278

FromHaircuts Are Important <clusardi2k@aol.com>
Date2013-06-03 12:48 -0700
Message-ID<ca303608-e196-4b80-88c9-f8e11a65f44a@d10g2000yqb.googlegroups.com>
In reply to#24276
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,

[toc] | [prev] | [next] | [standalone]


#24280

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-06-03 23:03 -0400
Message-ID<nospam-6D2463.23033403062013@news.aioe.org>
In reply to#24276
In article 
<653739d6-b70e-4e57-9af2-2dfaa66fec79@k4g2000yqb.googlegroups.com>,
 Haircuts Are Important <clusardi2k@aol.com> 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>

[toc] | [prev] | [next] | [standalone]


#24282

FromHaircuts Are Important <clusardi2k@aol.com>
Date2013-06-04 05:05 -0700
Message-ID<dac8d849-cae0-48df-855b-07a3c0115d1a@d10g2000yqb.googlegroups.com>
In reply to#24280
On Jun 3, 11:03 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <653739d6-b70e-4e57-9af2-2dfaa66fe...@k4g2000yqb.googlegroups.com>,
>  Haircuts Are Important <clusard...@aol.com> 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,

[toc] | [prev] | [next] | [standalone]


#24297

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-06-04 23:04 -0400
Message-ID<nospam-1D56F4.23042504062013@news.aioe.org>
In reply to#24282
In article 
<dac8d849-cae0-48df-855b-07a3c0115d1a@d10g2000yqb.googlegroups.com>,
 Haircuts Are Important <clusardi2k@aol.com> wrote:

> 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.

Summarizing some of my earlier suggestions on shared models and timers, 
the sscce below illustrates how I would proceed. I've also used the 
event dispatch thread and shown an alternate approach to scrolling.

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.Timer;
import javax.swing.table.DefaultTableModel;

class ParallelTables {

    private final DefaultTableModel modelA = initModel(1);
    private JTable tableA = new JTable(modelA) {
        @Override
        public Dimension getPreferredScrollableViewportSize() {
            Dimension d = super.getPreferredScrollableViewportSize();
            return new Dimension(d.width / 2, d.height);
        }
    };
    private final DefaultTableModel modelB = initModel(5);
    private JTable tableB = new JTable(modelB);

    private DefaultTableModel initModel(int n) {
        DefaultTableModel model = new DefaultTableModel(100, n);
        for (int row = 0; row < model.getRowCount(); row++) {
            model.setValueAt(row, row, 0);
        }
        return model;
    }

    private void display() {
        final JScrollPane scrollA = new JScrollPane(tableA);
        JScrollPane scrollB = new JScrollPane(tableB);
        scrollA.setVerticalScrollBarPolicy(
            JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        scrollA.getVerticalScrollBar().setModel(
            scrollB.getVerticalScrollBar().getModel());
        tableA.setSelectionModel(tableB.getSelectionModel());
        JPanel panel = new JPanel(
            new FlowLayout(FlowLayout.LEFT, 0, 0));
        panel.add(scrollA);
        panel.add(scrollB);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(panel);
        f.pack();
        f.setLocationByPlatform(true);
        f.setVisible(true);

        int last = modelA.getRowCount() - 1;
        tableA.getSelectionModel().setSelectionInterval(last, last);
        tableA.scrollRectToVisible(
            tableA.getCellRect(last, last, true));
        Timer t = new Timer(1000, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                modelA.addRow(new Object[]{42});
                modelB.addRow(new Object[]{42, 42, 42, 42, 42});
                int last = modelA.getRowCount() - 1;
                tableA.scrollRectToVisible(
                    tableA.getCellRect(last, last, true));
            }
        });
        t.setInitialDelay(0);
        t.start();
    }

    public static void main(String[] args) throws Exception {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ParallelTables().display();
            }
        });
    }
}

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#24309

FromHaircuts Are Important <clusardi2k@aol.com>
Date2013-06-05 11:53 -0700
Message-ID<c4f1b073-9e07-4c7a-b1ee-c41ce897f98c@a8g2000yqp.googlegroups.com>
In reply to#24297
On Jun 4, 11:04 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <dac8d849-cae0-48df-855b-07a3c0115...@d10g2000yqb.googlegroups.com>,
>  Haircuts Are Important <clus@aol.com> wrote:
>
> > 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.
>
> Summarizing some of my earlier suggestions on shared models and timers,
> the sscce below illustrates how I would proceed. I've also used the
> event dispatch thread and shown an alternate approach to scrolling.
>
> import java.awt.Dimension;
> import java.awt.EventQueue;
> import java.awt.FlowLayout;
> import java.awt.event.ActionEvent;
> import java.awt.event.ActionListener;
> import javax.swing.JFrame;
> import javax.swing.JPanel;
> import javax.swing.JScrollPane;
> import javax.swing.JTable;
> import javax.swing.Timer;
> import javax.swing.table.DefaultTableModel;
>
> class ParallelTables {
>
>     private final DefaultTableModel modelA = initModel(1);
>     private JTable tableA = new JTable(modelA) {
>         @Override
>         public Dimension getPreferredScrollableViewportSize() {
>             Dimension d = super.getPreferredScrollableViewportSize();
>             return new Dimension(d.width / 2, d.height);
>         }
>     };
>     private final DefaultTableModel modelB = initModel(5);
>     private JTable tableB = new JTable(modelB);
>
>     private DefaultTableModel initModel(int n) {
>         DefaultTableModel model = new DefaultTableModel(100, n);
>         for (int row = 0; row < model.getRowCount(); row++) {
>             model.setValueAt(row, row, 0);
>         }
>         return model;
>     }
>
>     private void display() {
>         final JScrollPane scrollA = new JScrollPane(tableA);
>         JScrollPane scrollB = new JScrollPane(tableB);
>         scrollA.setVerticalScrollBarPolicy(
>             JScrollPane.VERTICAL_SCROLLBAR_NEVER);
>         scrollA.getVerticalScrollBar().setModel(
>             scrollB.getVerticalScrollBar().getModel());
>         tableA.setSelectionModel(tableB.getSelectionModel());
>         JPanel panel = new JPanel(
>             new FlowLayout(FlowLayout.LEFT, 0, 0));
>         panel.add(scrollA);
>         panel.add(scrollB);
>         JFrame f = new JFrame();
>         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
>         f.add(panel);
>         f.pack();
>         f.setLocationByPlatform(true);
>         f.setVisible(true);
>
>         int last = modelA.getRowCount() - 1;
>         tableA.getSelectionModel().setSelectionInterval(last, last);
>         tableA.scrollRectToVisible(
>             tableA.getCellRect(last, last, true));
>         Timer t = new Timer(1000, new ActionListener() {
>             @Override
>             public void actionPerformed(ActionEvent e) {
>                 modelA.addRow(new Object[]{42});
>                 modelB.addRow(new Object[]{42, 42, 42, 42, 42});
>                 int last = modelA.getRowCount() - 1;
>                 tableA.scrollRectToVisible(
>                     tableA.getCellRect(last, last, true));
>             }
>         });
>         t.setInitialDelay(0);
>         t.start();
>     }
>
>     public static void main(String[] args) throws Exception {
>         EventQueue.invokeLater(new Runnable() {
>             @Override
>             public void run() {
>                 new ParallelTables().display();
>             }
>         });
>     }
>
> }
>

This appears to be useful, but I have a few improvements to ask about.

(1) The table automatically scrolls to one less than the number of
rows in the table. It does not scroll to the last row!

(2) When resizing the window, how do you get both tables to show more
rows. The tables show only about 25 rows and never the last row.

(3 When I, myself, do begin to add more code to this project where
should I be concerned about exceptions occurring. Will adding "try-
catch" pairs get me around all exceptions. What exceptions can I
ignore. When should I use a SwingWorker.

[toc] | [prev] | [next] | [standalone]


#24311

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-06-05 16:02 -0400
Message-ID<nospam-AD9E78.16020405062013@news.aioe.org>
In reply to#24309
In article 
<c4f1b073-9e07-4c7a-b1ee-c41ce897f98c@a8g2000yqp.googlegroups.com>,
 Haircuts Are Important <clusardi2k@aol.com> wrote:

> On Jun 4, 11:04 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > In article
> > <dac8d849-cae0-48df-855b-07a3c0115...@d10g2000yqb.googlegroups.com>,
> >  Haircuts Are Important <clus@aol.com> wrote:
> >
> > > 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.
> >
> > Summarizing some of my earlier suggestions on shared models and 
> > timers, the sscce below illustrates how I would proceed. I've also 
> > used the event dispatch thread and shown an alternate approach to 
> > scrolling.
> >
> > [...]
> 
> This appears to be useful, but I have a few improvements to ask about.
> 
> (1) The table automatically scrolls to one less than the number of 
> rows in the table. It does not scroll to the last row!
>
> (2) When resizing the window, how do you get both tables to show more 
> rows. The tables show only about 25 rows and never the last row.

My example starts with 100 rows, numbered 0 .. 99; added rows are 
numbered 42. I don't recall the numbering used in your examples.

Apropos to this, I would reiterate Lew's question about your now deleted 
examples. As Joerg Meier helpfully observed, they can be retrieved from 
one's NNTP server, but you might clarify your preference.
 
> (3 When I, myself, do begin to add more code to this project where 
> should I be concerned about exceptions occurring. Will adding "try- 
> catch" pairs get me around all exceptions. What exceptions can I 
> ignore. When should I use a SwingWorker.

Without context, the short answers are these: don't swallow exceptions 
and don't block the EDT.

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#24312

FromLew <lewbloch@gmail.com>
Date2013-06-05 13:09 -0700
Message-ID<05709200-d970-46bf-8117-4c223bd9b81a@googlegroups.com>
In reply to#24311
John B. Matthews wrote:
> Apropos to this, I would reiterate Lew's question about your now deleted 
> examples. As Joerg Meier helpfully observed, they can be retrieved from 
> one's NNTP server, but you might clarify your preference.

I figure he just doesn't want to answer this question. He's ignored it at least thrice so far.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#24314

Fromclusardi2k@aol.com
Date2013-06-05 16:27 -0700
Message-ID<fe197e07-c44d-4852-b901-e9cd5c8478ba@googlegroups.com>
In reply to#24311
On Wednesday, June 5, 2013 4:02:04 PM UTC-4, John B. Matthews wrote:
> In article <c4f1b073-9e07-4c7a-b1ee-c41ce897f98c@a8g2000yqp.googlegroups.com>, Haircuts Are Important <clus@aol.com> wrote: > On Jun 4, 11:04 pm, "John B. Matthews" <nos...@nospam.invalid> wrote: > > In article > > <dac8d849-cae0-48df-855b-07a3c0115...@d10g2000yqb.googlegroups.com>, > >  Haircuts Are Important <clus@aol.com> wrote: > > > > > 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. > > > > Summarizing some of my earlier suggestions on shared models and > > timers, the sscce below illustrates how I would proceed. I've also > > used the event dispatch thread and shown an alternate approach to > > scrolling. > > > > [...] > > This appears to be useful, but I have a few improvements to ask about. > > (1) The table automatically scrolls to one less than the number of > rows in the table. It does not scroll to the last row! > > (2) When resizing the window, how do you get both tables to show more > rows. The tables show only about 25 rows and never the last row. My example starts with 100 rows, numbered 0 .. 99; added rows are numbered 42. I don't recall the numbering used in your examples. Apropos to this, I would reiterate Lew's question about your now deleted examples. As Joerg Meier helpfully observed, they can be retrieved from one's NNTP server, but you might clarify your preference. > (3 When I, myself, do begin to add more code to this project where > should I be concerned about exceptions occurring. Will adding "try- > catch" pairs get me around all exceptions. What exceptions can I > ignore. When should I use a SwingWorker. Without context, the short answers are these: don't swallow exceptions and don't block the EDT. -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>

Thanks for your code

I think I can get the vertical scrolling to work by adding an extra row at the very top of the two tables. I'll then insert data into that empty row and create a new empty row each time after I add the data. So, the automatic vertical scrolling will never show the empty bottom row.

About only showing about 25 rows. I meant to say that the scrolling works. I do see all the rows created, but the project only shows 25 at a time. So, I see 0 through 25, then 1 through 26, then 2 through 27, ..., then n through n+25. So, I was wondering if it would be possible to be able to see more than 25 rows at a time apon a resize via a mouse drag.

[toc] | [prev] | [next] | [standalone]


#24315

FromLew <lewbloch@gmail.com>
Date2013-06-05 17:35 -0700
Message-ID<7dd3a6d0-b068-460f-ba0b-04a47d76ae6a@googlegroups.com>
In reply to#24314
clusa...@aol.com wrote:
> John B. Matthews wrote:
> > In article <c4f1b073-9e07-4c7a-b1ee-c41ce897f98c@a8g2000yqp.googlegroups.com>, Haircuts Are Important <clus@aol.com> wrote: > On Jun 4, 11:04 pm, "John B. Matthews" <nos...@nospam.invalid> wrote: > > In article > > <dac8d849-cae0-48df-855b-07a3c0115...@d10g2000yqb.googlegroups.com>, > >  Haircuts Are Important <clus@aol.com> wrote: > > > > > 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. > > > > Summarizing some of my earlier suggestions on shared models and > > timers, the sscce below illustrates how I would proceed. I've also > > used the event dispatch thread and shown an alternate approach to > > scrolling. > > > > [...] > > This appears to be useful, but I have a few improvements to ask about. > > (1) The table automatically scrolls to one less than the number of > rows in the table. It does not scroll to the last row! > > (2) When resizing the window, how do you get both tables to show more > rows. The tables show only about 25 rows and never the last row. My example starts with 100 rows, numbered 0 .. 99; added rows are numbered 42. I don't recall the numbering used in your examples. Apropos to this, I would reiterate Lew's question about your now deleted examples. As Joerg Meier helpfully observed, they can be retrieved from one's NNTP server, but you might clarify your preference. > (3 When I, myself, do begin to add more code to this project where > should I be concerned about exceptions occurring. Will adding "try- > catch" pairs get me around all exceptions. What exceptions can I > ignore. When should I use a SwingWorker. Without context, the short answers are these: don't swallow exceptions and don't block the EDT. -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
> 
> Thanks for your code
> 
> I think I can get the vertical scrolling to work by adding an extra row at the very top of the two tables. I'll then insert data into that empty row and create a new empty row each time after I add the data. So, the automatic vertical scrolling will never show the empty bottom row.
> 
> About only showing about 25 rows. I meant to say that the scrolling works. I do see all the rows created, but the project only shows 25 at a time. So, I see 0 through 25, then 1 through 26, then 2 through 27, ..., then n through n+25. So, I was wondering if it would be possible to be able to see more than 25 rows at a time apon a resize via a mouse drag.

Aaand ... 

He ignores the question again.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#24324

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-06-06 21:42 -0400
Message-ID<nospam-AA176E.21421806062013@news.aioe.org>
In reply to#24314
In article <fe197e07-c44d-4852-b901-e9cd5c8478ba@googlegroups.com>,
 clusardi2k@aol.com wrote:

> About only showing about 25 rows. I meant to say that the scrolling 
> works. I do see all the rows created, but the project only shows 25 
> at a time. So, I see 0 through 25, then 1 through 26, then 2 through 
> 27, ..., then n through n+25. So, I was wondering if it would be 
> possible to be able to see more than 25 rows at a time apon a resize 
> via a mouse drag.

Override getPreferredScrollableViewportSize(), a method in JTable's 
implementation of Scrollable, to return the desired multiple of 
getRowHeight().

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#24349

FromHaircuts Are Important <clusardi2k@aol.com>
Date2013-06-10 09:34 -0700
Message-ID<60c3c628-448d-490e-a7b6-9ce93fff8fe8@g8g2000yqa.googlegroups.com>
In reply to#24324
John, currently the last row of your project tables is not immediately
scrolled into view. In other words, the project scrolls down to the
last minus one row and not the last row. It would be nice to see the
project scroll down to the last row in the two tables. I need that
immediacy.

Alternately, I could enable the scrollbar in the left table, but since
this scrollbar will never be used, it looks bad.

Thanks,

[toc] | [prev] | [next] | [standalone]


#24351

From"John B. Matthews" <nospam@nospam.invalid>
Date2013-06-10 13:49 -0400
Message-ID<nospam-C634F6.13492410062013@news.aioe.org>
In reply to#24349
In article 
<60c3c628-448d-490e-a7b6-9ce93fff8fe8@g8g2000yqa.googlegroups.com>,
 Haircuts Are Important <clusardi2k@aol.com> wrote:

> John, currently the last row of your project tables is not 
> immediately scrolled into view. In other words, the project scrolls 
> down to the last minus one row and not the last row. It would be nice 
> to see the project scroll down to the last row in the two tables. I 
> need that immediacy.
> 
> Alternately, I could enable the scrollbar in the left table, but 
> since this scrollbar will never be used, it looks bad.

OK, I see what you mean. You can use the EventQueue to perform the 
scroll _after_ the table has a chance to respond to the update.

final int last = modelA.getRowCount();
EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        tableA.scrollRectToVisible(
            tableA.getCellRect(last, last, true));
    }
});

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

[toc] | [prev] | [next] | [standalone]


#24286

FromLew <lewbloch@gmail.com>
Date2013-06-04 12:17 -0700
Message-ID<861e3120-8f44-4b1f-a2ee-74140ac11a47@googlegroups.com>
In reply to#24280
>  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

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.java.programmer


csiph-web