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
Asked
Active
Viewed 2,339 times
3
-
Maybe by putting both JTables into a JPanel and using MigLayout as layout manager. – NoDataDumpNoContribution May 14 '14 at 13:25
-
Have you tried anything? – Halfwarr May 14 '14 at 13:25
-
1you should have two scrollpanes. one for each table. Otherwise it doesnt make sense what you are doing. – Oliver Watkins May 14 '14 at 15:13
3 Answers
9
-
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();
}
}

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 oneJComponent

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