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


Groups > comp.lang.java.programmer > #24297

Re: Code Included: Freeze column

From "John B. Matthews" <nospam@nospam.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Code Included: Freeze column
Date 2013-06-04 23:04 -0400
Organization The Wasteland
Message-ID <nospam-1D56F4.23042504062013@news.aioe.org> (permalink)
References (4 earlier) <3e3557ff-431c-4a9f-bf7c-777eeaeb65f7@w15g2000vbn.googlegroups.com> <nospam-7A73BC.23591631052013@news.aioe.org> <653739d6-b70e-4e57-9af2-2dfaa66fec79@k4g2000yqb.googlegroups.com> <nospam-6D2463.23033403062013@news.aioe.org> <dac8d849-cae0-48df-855b-07a3c0115d1a@d10g2000yqb.googlegroups.com>

Show all headers | View raw


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>

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web