Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Code Included: Freeze column

Reply
Thread Tools

Code Included: Freeze column

 
 
Haircuts Are Important
Guest
Posts: n/a
 
      05-31-2013
On May 31, 3:37*pm, Lew <(E-Mail Removed)> 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,
 
Reply With Quote
 
 
 
 
John B. Matthews
Guest
Posts: n/a
 
      06-01-2013
In article
<(E-Mail Removed)>,
Haircuts Are Important <(E-Mail Removed)> 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>
 
Reply With Quote
 
 
 
 
Haircuts Are Important
Guest
Posts: n/a
 
      06-03-2013
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" <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
> *Haircuts Are Important <(E-Mail Removed)> 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(dimen sion);
scrollTable.getTableHeader().setPreferredSize(dime nsion);

fixedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OF F);
scrollTable.setAutoResizeMode(JTable.AUTO_RESIZE_O FF);

fixedTable.setSelectionMode(ListSelectionModel.SIN GLE_SELECTION);
scrollTable.setSelectionMode(ListSelectionModel.SI NGLE_SELECTION);

scrollTable.setFocusable(false);

JScrollPane jScrollPane1 = new JScrollPane(scrollTable);

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

jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORN ER,
fixedTable.getTableHea**der());
getContentPane().add(jScrollPane1,BorderLayout.CEN TER);
}

//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().setSelectionInterva l(
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(fixedSelectedI ndex,fixedSelectedIndex);
} else {

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

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();
}
}
};

 
Reply With Quote
 
Haircuts Are Important
Guest
Posts: n/a
 
      06-03-2013
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,
 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      06-04-2013
In article
<(E-Mail Removed)>,
Haircuts Are Important <(E-Mail Removed)> 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>
 
Reply With Quote
 
Haircuts Are Important
Guest
Posts: n/a
 
      06-04-2013
On Jun 3, 11:03*pm, "John B. Matthews" <(E-Mail Removed)> wrote:
> In article
> <(E-Mail Removed)>,
> *Haircuts Are Important <(E-Mail Removed)> 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,


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-04-2013
> 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
 
Reply With Quote
 
Haircuts Are Important
Guest
Posts: n/a
 
      06-04-2013
On Jun 4, 3:17*pm, Lew <(E-Mail Removed)> 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,
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      06-04-2013
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
 
Reply With Quote
 
Joerg Meier
Guest
Posts: n/a
 
      06-04-2013
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
freeze column headings Seth Williams ASP .Net 2 10-17-2008 04:57 PM
freeze string fails to freeze ??? jwaala.amplify@gmail.com Ruby 5 03-15-2007 11:29 PM
Sending an E-mail (Code Included with post) Michael Perl 2 09-15-2004 02:08 PM
Freeze column(s) in a Datagrid =?Utf-8?B?QmVu?= ASP .Net 1 06-05-2004 06:35 PM
Re: help with insert/updating date variables in table code included ASP .Net 1 12-14-2003 11:28 PM



Advertisments