Now i'm coding with SWT/JFace.
I wanna show text message and add button on Sash (org.eclipse.swt.widgets.Sash),how to do?
Moderators: Konstantin.Scheglov, gnebling, Alexander.Mitin, jwren, Eric Clayberg
Konstantin.Scheglov wrote:You don't want to use Sash, you should use SashForm.
Just first widget on it, then:
1. drop second in Tree
2. or select SashForm and drop second widget near to the border of SashForm. A little tricky, but SashForm fill itself with children widgets, so you need special way for dropping new children.
jackyustb wrote:i hope to show some tip messages on sash in a sashform which contains two children ,one is Tableview on its top, another is Text on its bottom. the sash in the middle separate them.
When i select one item in the Tableview,i wanna show the item's main content in the Text widget and tip message on the sash.
how to do?
Eric Clayberg wrote:jackyustb wrote:i hope to show some tip messages on sash in a sashform which contains two children ,one is Tableview on its top, another is Text on its bottom. the sash in the middle separate them.
When i select one item in the Tableview,i wanna show the item's main content in the Text widget and tip message on the sash.
how to do?
You would need to get the children of the SashForm, find the Sash widget and then set its tooltip using setToolTipText().
General questions about using SWT should be posted to the Eclipse SWT newsgroup.
jackyustb wrote:Children of the SashForm contains the Sash?
Eric Clayberg wrote:jackyustb wrote:Children of the SashForm contains the Sash?
Yes.
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates SashForm
*/
public class SashFormTest {
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SashForm Test");
createContents(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the main window's contents
*
* @param parent the parent window
*/
private void createContents(Composite parent) {
// Fill the parent window with the buttons and sash
parent.setLayout(new FillLayout());
// Create the SashForm and the buttons
SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);
new Button(sashForm, SWT.PUSH).setText("Left");
new Button(sashForm, SWT.PUSH).setText("Right");
// This is the part, where the children of SashForm are printed out:
Control[] controls = sashForm.getChildren();
for (Control control : controls)
{
System.out.println(control.getClass().getName());
}
}
/**
* The application entry point
*
* @param args the command line arguments
*/
public static void main(String[] args) {
new SashFormTest().run();
}
}
Makaron wrote:Sorry, but the children of SashForm don't contain the Sash
Makaron wrote:consider the following example
Makaron wrote:Or maybe I'm missing something
Eric Clayberg wrote:First of all, let me reiterate that general questions about SWT should be posted to the Eclipse SWT newsgroup.
Makaron wrote:Or maybe I'm missing somethingEric Clayberg wrote:Yes. As is always the case with a question like this, you should start by looking at the actual implementation of the SashForm class itself. There you will see quite clearly that a SashForm includes one or more Sash widgets and that they are all held in the sashes field.
Makaron wrote:I looked at the SashForm implementation and there's indeed private field sashes, but I don't know how to get Sash instances from it; the method SashForm.getChildren() obviously doesn't return any Sash objects.
import java.lang.reflect.Field;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Sash;
public class SashFormHelper {
public static Sash[] getSashes(SashForm sf) {
final Field fields[] = sf.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; ++i) {
if ("sashes".equals(fields[i].getName())) {
try {
fields[i].setAccessible(true);
return (Sash[]) fields[i].get(sf);
} catch (IllegalAccessException ex) {
}
}
}
return null;
}
}
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
/**
* This class demonstrates SashForm
*/
public class SashFormTest {
public SashForm sashForm;
/**
* Runs the application
*/
public void run() {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("SashForm Test");
createContents(shell);
shell.open();
// This is the part, where the children of SashForm are printed out:
Control[] controls = sashForm.getChildren();
for (Control control : controls) {
System.out.println(control.getClass().getName());
}
// Note that Sashes don't exist until the window is open
Sash[] sashes = SashFormHelper.getSashes(sashForm);
for (Sash sash : sashes) {
sash.setToolTipText("I am a Sash");
System.out.println(sash.getClass().getName());
}
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the main window's contents
*
* @param parent
* the parent window
*/
private void createContents(Composite parent) {
// Fill the parent window with the buttons and sash
parent.setLayout(new FillLayout());
// Create the SashForm and the buttons
sashForm = new SashForm(parent, SWT.HORIZONTAL);
new Button(sashForm, SWT.PUSH).setText("Left");
new Button(sashForm, SWT.PUSH).setText("Right");
}
/**
* The application entry point
*
* @param args
* the command line arguments
*/
public static void main(String[] args) {
new SashFormTest().run();
}
}
Eric Clayberg wrote:Makaron wrote:I looked at the SashForm implementation and there's indeed private field sashes, but I don't know how to get Sash instances from it; the method SashForm.getChildren() obviously doesn't return any Sash objects.
Very easy using reflection...
Makaron wrote:Thank you very much, this solution really works, you saved my day.
Makaron wrote:I wasn't aware of the getClass().getDeclaredFields() method.
Users browsing this forum: No registered users and 1 guest