3

how can i add two JTable's side by side in a JScrollPane using setViewPortView() option and set the size of the tables with respect to the size assigned to JscrollPane

mKorbel
  • 109,525
  • 20
  • 134
  • 319
rahul anand
  • 97
  • 1
  • 7

3 Answers3

9

As shown here and here, let each table occupy its own scroll pane. Let one pane show a scrollbar always, while the other does so never. Then let both scrollbars share a common BoundedRangeModel.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • 1
    +1 possible (I like it) with [AdjustmentListener](http://stackoverflow.com/a/12707995/714968), but nobody asked here for synchronized srolling :-) – mKorbel May 14 '14 at 18:18
4

JScrollPane can contain only one JComponent, but you can wrap your tables to JPanel and add it to JScrollPane like next:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


public class TestFrame extends JFrame{

    public TestFrame(){
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setVisible(true);
    }

    private void init() {
      JTable t1 = new JTable(10,5);
      JTable t2 = new JTable(15,5);
      JPanel p = new JPanel(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      c.anchor = GridBagConstraints.NORTHWEST;
      c.insets =  new Insets(0,5,0,5);
      c.gridy=0;
      c.gridx=0;
      p.add(t1.getTableHeader(),c);
      c.gridx=1;
      p.add(t2.getTableHeader(),c);
      c.gridx=0;
      c.gridy=1;
      p.add(t1,c);
      c.gridx=1;
      p.add(t2,c);
      add(new JScrollPane(p));
    }


    public static void main(String... strings) {
        new TestFrame();
    }

}

enter image description here

alex2410
  • 10,904
  • 3
  • 25
  • 41
2

how can i add two JTable's side by side in a JScrollPane using setViewPortView() option and set the size of the tables with respect to the size assigned to JscrollPane

  • JScrollPane is designated to nest only one JComponent
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • True, but two scrollbars can share a common model, for [example](http://stackoverflow.com/a/23658586/230513). – trashgod May 14 '14 at 15:11
  • I'm think that you have to consume() events from parent JScrollPane and to redirect this event from MouseInfo, Point from Mouse Scroll or equivalent from SwingUtilities to the JScrollPane under the Cursor, otherwise parent to consume every mouse events, one of the reasons for why is my very short answer to empty question – mKorbel May 14 '14 at 18:10