Code Included: Freeze column

  • Thread starter Haircuts Are Important
  • Start date
L

Lew

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

I'm sure that is true, but what does it have to do with this thread?
 
J

John B. Matthews

Haircuts Are Important said:
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();
}
});
}
}
 
J

Joerg Meier

I'm sure that is true, but what does it have to do with this thread?

On this thread, you have addressed two different people and asked about
their posts, Haircuts and John Matthews. From the context, I'm now guessing
the first one was due to not having a post of Haircuts left to reply to ?

Either way, it does seem to be on 'your' end (Google) - the posts are still
visible on my news server, and nothing about them seems special or unusual.

Liebe Gruesse,
Joerg
 
L

Lew

Joerg said:
On this thread, you have addressed two different people and asked about
their posts, Haircuts and John Matthews. From the context, I'm now guessing

Looking at the attributions, which your posts preserved, you can see that
both questions were addressed to the same person.
the first one was due to not having a post of Haircuts left to reply to ?

It was due to every post of Haircuts' being marked "This message has been deleted."

Something to which John Matthews also alluded in response to your post.
Either way, it does seem to be on 'your' end (Google) - the posts are still
visible on my news server, and nothing about them seems special or unusual.

Thank you. That is helpful to know.

Still, for even GG to report that the messages have been deleted, someone
took an affirmative action to delete them. So I took the reasonable action
of asking the poster what happened, under the hypothesis that they initiated
the deletion. The answer I got was tangential at best, and completely
devoid of useful content, so I repeated the question - to the same person.

So I ask Haircut this: Did you in some fashion "delete" your posts?
 
H

Haircuts Are Important

 Haircuts Are Important said:
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.
 
J

John B. Matthews

Haircuts Are Important said:
 Haircuts Are Important said:
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.
 
L

Lew

John said:
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.
 
C

clusardi2k

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

Lew

John said:
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 rowand 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.
 
J

John B. Matthews

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().
 
H

Haircuts Are Important

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

John B. Matthews

Haircuts Are Important said:
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));
}
});
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top