How to set multiple images on a SWT TreeItem? - java

I working on an SWT Tree where each TreeItem needs to have multiple images on it. Right now I am trying to do
treeItem.setImage(index, Image)
and trying to set multiple Images on a single TreeItem. But it doesn't seem to work. What is this method about? Any help appreciated.

I don't really see your problem. The following works perfectly for me:
public static void main(String[] args)
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Tree tree = new Tree(shell, SWT.NONE);
tree.setHeaderVisible(true);
TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
column1.setText("Column 1");
column1.setWidth(50);
TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
column2.setText("Column 2");
column2.setWidth(50);
TreeColumn column3 = new TreeColumn(tree, SWT.LEFT);
column3.setText("Column 3");
column3.setWidth(50);
Image image = YOUR_IMAGE_HERE;
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setImage(0, image);
item.setImage(1, image);
item.setImage(2, image);
shell.pack();
shell.open();
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}

Related

Wrap the text inside a Button

I am having an issue trying to do what seemed to be an easy task.
Coding an UI in java/swt, I'm trying to get the text of a button display on two lines (wrap the string passed to the button), but I can't manage to do so with the carriage return in the string, nor with the SWT.WRAP style of the button.
Here is a sample of my code :
Button myButton = new Button(compoCentre, SWT.WRAP);
myButton.setBounds(40, 200, 240, 40);
myButton.setText("A long text, but not so long, just enough);
However, this results in the text displaying on one single line, hiding the part not fitting the size of the button.
Any ideas / workaround ?
Thank you for your time.
Check out below code :
public class Sample {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new GridLayout(1, false));
Composite comp = new Composite(shell, SWT.NONE);
comp.setLayout(new GridLayout(1, false));
comp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button testButton = new Button(comp, SWT.PUSH | SWT.WRAP);
testButton.setText("A long text, but not so long, just enough");
final GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true);
layoutData.widthHint = 100;
testButton.setLayoutData(layoutData);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Output on Windows 10:

How do I right align the check box in my Java SWT application

How do I right align the check box in my Java SWT application ? Here is my code snippet:
Button checkFullECR = new Button(scrolledForm.getBody(), SWT.CHECK);
checkFullECR.setAlignment(SWT.RIGHT);
checkFullECR.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
checkFullECR.setText("I need the complete ECR/ECN Process:");
It looks like the following:
[ ] I need the complete ECR/ECN Process:
I want it to look like:
I need the complete ECR/ECN Process: [ ]
You can't do this using just a Button control, you will need to use a Label for the text and a checkbox Button with no text arranged in two columns.
Something like:
Composite body = new Composite(parent, SWT.NONE);
body.setLayout(new GridLayout(2, false));
Label label = new Label(body, SWT.LEFT);
label.setText("I need the complete ECR/ECN Process:");
Button checkFullECR = new Button(body, SWT.CHECK);
... more label / button pairs ....
For once I kind of have to disagree with Greg.
You can use the setOrientation(int) method of the Button to force right-to-left layout:
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.CHECK);
button.setText("TEST");
button = new Button(shell, SWT.CHECK);
button.setOrientation(SWT.RIGHT_TO_LEFT);
button.setText("TEST");
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this:
(Tested on Linux Mint and Windows. Does not work on the Mac.)

SWT: Resize Table (height) using a MouseListener

I'm new to SWT and want to make an application with two vertical composites (or somethink like that).
The the composite at the buttom caintains a simple Table. Now I need a variable height for the complete table - the user should determine the height by using the Mouse - just like it's possible to resize the eclipse views. The composite on the top should adjust the space.
Is somethinkg like that possible with SWT? If yes, I would be thankful for every suggestion.
You can use a SashForm for this. It's user resizable.
Here is an example.
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
Text text1 = new Text(sashForm, SWT.CENTER);
text1.setText("Text in pane #1");
Text text2 = new Text(sashForm, SWT.CENTER);
text2.setText("Text in pane #2");
final SashForm sashForm2 = new SashForm(sashForm, SWT.VERTICAL);
final Label labelA = new Label(sashForm2, SWT.BORDER | SWT.CENTER);
labelA.setText("Label in pane A");
final Label labelB = new Label(sashForm2, SWT.BORDER | SWT.CENTER);
labelB.setText("Label in pane B");
sashForm.setWeights(new int[] { 1, 2, 3 });
new Label(shell, SWT.NONE).setText("Label");
shell.pack();
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}

Input form using FormLayout in swt

i am new to swt and im trying to create a layout in a window. Each texfield will have a label to the left of the textfield- however in some cases there may be two textfields per label, and maybe at a later date there will be radio buttons added. Is a formlayout the best way of doing this? It seems to me unnecessarily over-complicated. I dont have windowbuilder or a visual designer utility and am finding the FormAttachment method difficult to handle. Any advice appreciated. I have attached a screenshot of the basic gui design Im trying to create.
Unfortunately im not allowed upload images for the moment, as i a new user. Essentially the structure i am aiming for is like so:
LABEL TEXTBOX
LABEL TEXTBOX TEXTBOX
LABEL TEXTBOX
LABEL TEXTBOX TEXTBOX
LABEL CALENDAR CONTROL
OK | NOK
Here is a simple example that should explain how to span a widget across multiple columns of a GridLayout:
public class StackOverflow2
{
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(3, false));
Label firstLabel = new Label(shell, SWT.NONE);
firstLabel.setText("Label");
Text firstText = new Text(shell, SWT.BORDER);
addSpanData(firstText, 2);
Label secondLabel = new Label(shell, SWT.NONE);
secondLabel.setText("Label");
Text secondText = new Text(shell, SWT.BORDER);
addSpanData(secondText, 1);
Text thirdText = new Text(shell, SWT.BORDER);
addSpanData(thirdText, 1);
Label thirdLabel = new Label(shell, SWT.NONE);
thirdLabel.setText("Label");
Text fourthText = new Text(shell, SWT.BORDER);
addSpanData(fourthText, 2);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static void addSpanData(Control comp, int span)
{
GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
data.horizontalSpan = span;
comp.setLayoutData(data);
}
}
This is how it looks:

why are widgets placed on the TabItem invisible when using ScrolledComposite SWT?

Here is my code which actually does all the GUI work for Domain(right side contained in the picture attached).In this function What i am doing is I am creating a composite "test" on the "newTabFolder".Then i am creating ScrolledComposite "sc" on it and then Creating a composite "compositeInTab" on it and after placing all the widgets on "compositeInTab" I am creating a TabItem for placing the composite "test" on it.
public DomainUI(Composite composite, TabFolder newTabFolder, boolean comingFromSelf)
{
Composite test = new Composite(newTabFolder,SWT.NONE);
test.setLayout(new FillLayout());
ScrolledComposite sc = new ScrolledComposite(test, SWT.V_SCROLL|SWT.H_SCROLL);
final Composite compositeInTab = new Composite(sc, SWT.NONE);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setMinHeight(compositeInTab.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
sc.setMinWidth(compositeInTab.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
sc.setContent(compositeInTab);
compositeInTab.setLayout(null);
sc.setAlwaysShowScrollBars(true);
/*HERE I AM CREATING LABELS AND TEXT FIELDS AND SETTING THEIR BOUNDS*/
systemCodeLabel = new Label(compositeInTab, 0);
systemCodeText = new Text(compositeInTab, SWT.BORDER);
systemCodeLabel.setText("System Code");
systemCodeLabel.setBounds(350, 60, 100, 15);
systemCodeText.setBounds(480, 60, 150, 17);
// CREATION OF LABELS AND TEXTFIELDS ENDED
// CREATION OF TABLE STARTS
myTable = new CreateTable(compositeInTab, 1);
myTable.setBounds(50, 230, 0, 0);
myTable.table.setSize(myTable.table.computeSize(570, 250));
//here i filled data in table
for(int i=0; i<myTable.table.getColumnCount(); i++)
{
myTable.table.getColumn(i).pack();
myTable.table.getColumn(i).setWidth(myTable.table.getColumn(i).getWidth()+10);
}
TabItem tabItem1 = new TabItem(newTabFolder, SWT.NONE);
tabItem1.setText("Domain");
tabItem1.setControl(test);
newTabFolder.setBounds(0, 0, 480, 300);
}
It seems to be a matter of layouts. I fiddled around a little bit and managed to get this working:
public static void main(String[] args)
{
Display display = Display.getDefault();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
ScrolledComposite scrollComp = new ScrolledComposite(shell, SWT.V_SCROLL | SWT.H_SCROLL);
Composite innerComp = new Composite(scrollComp, SWT.NONE);
innerComp.setLayout(new GridLayout(4, true));
for(int i = 0; i < 32; i++)
new Button(innerComp, SWT.PUSH).setText("Button");
scrollComp.setMinHeight(innerComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).y);
scrollComp.setMinWidth(innerComp.computeSize(SWT.DEFAULT, SWT.DEFAULT).x);
scrollComp.setContent(innerComp);
scrollComp.setExpandHorizontal(true);
scrollComp.setExpandVertical(true);
scrollComp.setAlwaysShowScrollBars(true);
shell.pack();
shell.open();
shell.setSize(200, 200);
while(!shell.isDisposed())
{
if(!display.readAndDispatch())
display.sleep();
}
}
It will show the scrollbars. However, if I change the layout of the shell to a GridLayout, it will not work. Maybe your "combination" of layouts seems to be the problem...

Categories