Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Is Layout required for Event handling?
Is Layout required for Event handling? [message #455671] |
Thu, 19 May 2005 14:49 |
Eclipse User |
|
|
|
Originally posted by: eclipse.patcom.demon.co.uk
Hello
This is probably a very basic question but I have looked through the
articles I can find and have not found the answer.
Does a composite have to have a layout for the event listeners in its
children to work?
I am trying to display two tables side by side and synchronise the
vertical scrolling of the two tables. I added 2 ScrolledComposites to a
Shell and added a table to each of the Scrolled Composites. I found an
example where buttons are added to the ScrollableComposites and listeners
added to the scrollbars which set the origin the the opposite
ScollableComposite.
This was working but I had problems with laying out the controls, at some
point the two tables stopped scrolling in tandem.
I went back to the two button example and found that if I commented out
the setLayout for the shell, the listeners stopped working.
Why is this ? Is there somewhere I can read up on this?
Thanks for your help
Mike
|
|
| | |
Re: Is Layout required for Event handling? [message #455700 is a reply to message #455698] |
Thu, 19 May 2005 17:22 |
Eclipse User |
|
|
|
Originally posted by: eclipse.patcom.demon.co.uk
Thanks, here is the code
package com.wcg.framework.workflow.ui.designer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
public class MatrixView {
public MatrixView() {
}
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
// RowLayout rowLayout = new RowLayout();
// rowLayout.pack = true;
// rowLayout.marginRight = 0;
// shell.setLayout(rowLayout);
shell.setText("WCG Catalyst Designer - Matrix View");
final ScrolledComposite sc1 = new ScrolledComposite (shell,
SWT.H_SCROLL | SWT.V_SCROLL);
sc1.setLayout(new FillLayout());
sc1.setBounds(0,0,260,740);
final ScrolledComposite sc2 = new ScrolledComposite (shell,
SWT.H_SCROLL | SWT.V_SCROLL);
sc2.setLayout(new FillLayout());
sc2.setBounds(261,0,740,740);
Table table1 = new Table(sc1,SWT.SINGLE);
table1.setSize (260, 700);
table1.setHeaderVisible(false);
table1.setLinesVisible(true);
TableColumn col1 = new TableColumn(table1,SWT.LEFT);
col1.setWidth(200);
TableColumn col2 = new TableColumn(table1,SWT.LEFT);
col2.setWidth(60);
for (int i = 0; i < 60; i++) {
TableItem item1 = new TableItem(table1,0);
item1.setText(new String[]{"Line " + i,"b"});
}
Table table2 = new Table(sc2,SWT.SINGLE);
table2.setSize (740, 700);
table2.setHeaderVisible(false);
table2.setLinesVisible(true);
for (int i = 0; i < 10; i++) {
TableColumn col21 = new TableColumn(table2,SWT.LEFT);
col21.setWidth(80);
}
for (int i = 0; i < 60; i++) {
TableItem item1 = new TableItem(table2,0);
item1.setText(new String[]{"Line "+i,"c"});
}
sc1.setContent(table1);
sc2.setContent(table2);
final ScrollBar vBar1 = sc1.getVerticalBar ();
final ScrollBar vBar2 = sc2.getVerticalBar ();
final ScrollBar hBar1 = sc1.getHorizontalBar ();
final ScrollBar hBar2 = sc2.getHorizontalBar ();
SelectionListener listener1 = new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
System.out.println("listener1 " + hBar1.getSelection () + " " +
vBar1.getSelection ());
sc2.setOrigin (
hBar1.getSelection (),
vBar1.getSelection ());
}
};
SelectionListener listener2 = new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
System.out.println("listener2 " + hBar2.getSelection () + " " +
vBar2.getSelection ());
sc1.setOrigin (
hBar2.getSelection (),
vBar2.getSelection ());
}
};
vBar1.addSelectionListener (listener1);
hBar1.addSelectionListener (listener1);
vBar2.addSelectionListener (listener2);
hBar2.addSelectionListener (listener2);
System.out.println("Show");
shell.setSize (1000, 740);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
|
|
|
Re: Is Layout required for Event handling? [message #455708 is a reply to message #455700] |
Thu, 19 May 2005 20:23 |
Veronika Irvine Messages: 1272 Registered: July 2009 |
Senior Member |
|
|
In your example, one table had column widths of 200 + 60 and the other table
had column widths of 8 * 10. Since the tables have different widths, they
are not going to scroll horizontall together as you have written it.
You have two choices, make the two things the same width or make the wider
one scroll faster than the other one. Here is a version of your code that
works for me.
public static void main (String [] args) {
Display display = new Display ();
Shell shell = new Shell (display);
shell.setLayout(new FillLayout());
shell.setText("WCG Catalyst Designer - Matrix View");
final ScrolledComposite sc1 = new ScrolledComposite (shell, SWT.H_SCROLL
| SWT.V_SCROLL);
final ScrolledComposite sc2 = new ScrolledComposite (shell, SWT.H_SCROLL
| SWT.V_SCROLL);
Table table1 = new Table(sc1,SWT.SINGLE);
table1.setHeaderVisible(false);
table1.setLinesVisible(true);
TableColumn col1 = new TableColumn(table1,SWT.LEFT);
col1.setWidth(200);
TableColumn col2 = new TableColumn(table1,SWT.LEFT);
col2.setWidth(60);
for (int i = 0; i < 60; i++) {
TableItem item1 = new TableItem(table1,0);
item1.setText(new String[]{"Line " + i,"b"});
}
Table table2 = new Table(sc2,SWT.SINGLE);
table2.setHeaderVisible(false);
table2.setLinesVisible(true);
for (int i = 0; i < 10; i++) {
TableColumn col21 = new TableColumn(table2,SWT.LEFT);
col21.setWidth(80);
}
for (int i = 0; i < 60; i++) {
TableItem item1 = new TableItem(table2,0);
item1.setText(new String[]{"Line "+i,"c"});
}
sc1.setContent(table1);
table1.pack();
sc2.setContent(table2);
table2.pack();
final ScrollBar vBar1 = sc1.getVerticalBar ();
final ScrollBar vBar2 = sc2.getVerticalBar ();
final ScrollBar hBar1 = sc1.getHorizontalBar ();
final ScrollBar hBar2 = sc2.getHorizontalBar ();
SelectionListener listener1 = new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
int x = hBar1.getSelection() * (hBar2.getMaximum() -
hBar2.getThumb()) / Math.max(1, hBar1.getMaximum() - hBar1.getThumb());
int y = vBar1.getSelection() * (vBar2.getMaximum() -
vBar2.getThumb()) / Math.max(1, vBar1.getMaximum() - vBar1.getThumb());
sc2.setOrigin (x, y);
}
};
SelectionListener listener2 = new SelectionAdapter () {
public void widgetSelected (SelectionEvent e) {
int x = hBar2.getSelection() * (hBar1.getMaximum() -
hBar1.getThumb()) / Math.max(1, hBar2.getMaximum() - hBar2.getThumb());
int y = vBar2.getSelection() * (vBar1.getMaximum() -
vBar1.getThumb()) / Math.max(1, vBar2.getMaximum() - vBar2.getThumb());
sc1.setOrigin (x, y);
}
};
vBar1.addSelectionListener (listener1);
hBar1.addSelectionListener (listener1);
vBar2.addSelectionListener (listener2);
hBar2.addSelectionListener (listener2);
System.out.println("Show");
shell.setSize (500, 500);
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
|
|
|
Goto Forum:
Current Time: Thu Apr 25 08:53:18 GMT 2024
Powered by FUDForum. Page generated in 0.03614 seconds
|