Arranging Labels - java

How can I arrange Labels on a canvas from its top left down and so on?
and if you can show me how to do that in a loop.
I tried to use GridData, but I don't think I understand it that much.
GridData gd = new GridData(SWT.LEFT);
Label lbl = new Label(canvas, SWT.LEFT);
lbl.setText("A: ");
lbl.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
lbl.setLayoutData(gd);
Label lbl2 = new Label(c,SWT.TOP);
lbl.setText("B: ");
lbl.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
lbl2.setLayoutData(gd);

If you just want one column, use RowLayout with SWT.VERTICAL:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new RowLayout(SWT.VERTICAL));
shell.setText("StackOverflow");
for(int i = 0; i < 10; i++)
{
new Label(shell, SWT.NONE).setText("Label " + i);
}
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
Looks like this:

SWT.LEFT and SWT.TOP
first of all, shouldn't that be the same or?
second
with array
just loop trough the array and for each value create a label
size/position is just (in %) 100/TOTAL * KEY
(if KEY starts at 0 and goes to the highest)

Related

Why applied Font is removed upon font.dispose() in swt?

I have created an SWT Table and added columns to it. I want to apply bold font to header row alone. So my code looks like below
Table table= new Table(top, tableStyle);
Font font = new Font(null, StringUtils.EMPTY, 9, SWT.BOLD);
for (int i = 0; i < titles.length; i++)
{
new TableColumn(table, SWT.NONE);
header.setFont(i, font);
header.setText(i, titles[i]);
}
font.dispose();
In the above code, I have disposed as it is a good practice to do. But this is removing the font style from the header. If I remove the last line, font remains applied.
Is there any mistake over here? Or is this the expected behavior?
You need to wait and only dispose() the Font when you no longer need it. You could tie the disposal to the dispose event of the table so you don't have to manually dispose it:
public static void main(String[] args)
{
final Display d = new Display();
Shell s = new Shell(d);
s.setLayout(new FillLayout());
Table table = new Table(s, SWT.NONE);
Font font = new Font(null, "", 12, SWT.BOLD);
for (int i = 0; i < 3; i++)
{
TableItem item = new TableItem(table, SWT.NONE);
item.setFont(font);
item.setText("" + i);
}
table.addListener(SWT.Dispose, e -> font.dispose());
s.pack();
s.open();
while (!s.isDisposed())
{
if (!d.readAndDispatch())
d.sleep();
}
d.dispose();
}

Create GridLayout With Rows of Equal Height

I want to have a composite with some labels, and all the labels should have the same height, but they should be vertically centered.
So far I have the following:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Composite composite = new Composite(shell, SWT.VERTICAL);
composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(true).spacing(0, 0).create());
for (int i = 0; i < 10; i++) {
final Label label = new Label(composite, SWT.WRAP);
label.setAlignment(SWT.CENTER);
label.setText(i < 5 ? "small " + i : "a very big text for the row that is named " + i);
label.setToolTipText(label.getText());
label.setLayoutData(GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).create());
}
shell.setSize(100, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
As you can see, the labels are vertically centered in their rows, but the ones which wrap take more space than the other ones.
If I add hint(SWT.DEFAULT, 60) to the GridData, I can force the labels to have the same height, but then they won't be vertically centered anymore.
I could probably wrap all the labels in composites, set the height hint on the composite and center the labels, but let's see of there is another option first.
How do I create vertically centered rows of equal height?
Since you're trying to uniformly arrange widgets which may be different sizes, I think a good option would be to place each Label into a Composite. That way each Composite can have the same size, and the Label will be centered within.
By adding a ControlListener to the parent Composite, you can check the size of each child Composite, then set the height hint of each child to be the height of the largest child:
public static void main(final String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Composite composite = new Composite(shell, SWT.VERTICAL);
composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(true).spacing(0, 0).create());
final List<Composite> children = new ArrayList<Composite>();
for (int i = 0; i < 10; i++) {
final Composite c = new Composite(composite, SWT.BORDER);
c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
c.setLayout(new GridLayout());
final Label label = new Label(c, SWT.WRAP);
label.setAlignment(SWT.CENTER);
label.setText(i < 5 ? "small " + i : "a very big text for the row that is named " + i);
label.setToolTipText(label.getText());
label.setLayoutData(GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).create());
children.add(c);
}
composite.addControlListener(new ControlAdapter() {
#Override
public void controlResized(final ControlEvent e) {
int maxHeight = 0;
for (final Composite c : children) {
if (c.getClientArea().height > maxHeight) {
maxHeight = c.getClientArea().height;
}
}
for (final Composite c : children) {
((GridData) c.getLayoutData()).heightHint = maxHeight;
}
}
});
shell.setSize(100, 600);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
Result:
And after resizing:

Show Table in a View - Eclipse Plugin

I'm trying to develop a plugin for Eclipse. I follow tutorials online and I have done a plugin with sample perspective and a sample views.
Now the view show this:
public Object[] getElements(Object parent)
{
return new String[] { "One", "Two", "Three" };
}
Instead of string I need to insert a table. I follow another tutorial to create a TreeTable, but I have no idea how to put this table Tree into my plugin's view.
This is the code of TreeTable:
public class TreeTableCreation
{
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Tree tree = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
tree.setHeaderVisible(true);
TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
column1.setText("Column 1");
column1.setWidth(200);
TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
column2.setText("Column 2");
column2.setWidth(200);
TreeColumn column3 = new TreeColumn(tree, SWT.RIGHT);
column3.setText("Column 3");
column3.setWidth(200);
for (int i = 0; i < 4; i++) {
TreeItem item = new TreeItem(tree, SWT.NONE);
item.setText(new String[] { "item " + i, "abc", "defghi" });
for (int j = 0; j < 4; j++) {
TreeItem subItem = new TreeItem(item, SWT.NONE);
subItem.setText(new String[] { "subitem " + j, "jklmnop", "qrs" });
for (int k = 0; k < 4; k++) {
TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
subsubItem.setText(new String[] { "subsubitem " + k, "tuv", "wxyz" });
}
}
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Your view should have a createPartControl(Composite parent) method inherited from ViewPart. Put your tree table creation code there and use the parent parameter as the tree's parent (instead of shell in your code).

Getting FillLayout to behave correctly

I am trying to create a view with two trees that take up the entire display. As of now, the two trees are only taking up half (left half) of the canvas. I cannot figure out why, as I have tried many different parameters sent to each of the setup methods, such as setLayout, SashForm, etc. Here is my code and I attached an image to show what a simplified view of what I am getting now is.
super(parent);
setDisplayName("Viewer");
setLayout(new FillLayout());
((FillLayout) getLayout()).marginHeight = ((FillLayout) getLayout()).marginWidth = 20;
fullForm = new SashForm(this, SWT.VERTICAL);
fullForm.setLayout(new FillLayout());
adForm = new SashForm(fullForm, SWT.VERTICAL);
adForm.setLayout(new FillLayout());
countLabel = GUIToolkit.newLabel(this, SWT.CENTER, "", new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
countLabel.setFont(boldFont);
ad1Tree = createTree(fullForm, "name1", "col1", "col2");
ad2Tree = createTree(fullForm, "name2", "col1", "col2");
Create Tree
private static Tree createTree(final Composite parent, final String text, final String... columns) {
final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, null);
group.setFont(FontManager.NORMAL_BOLD);
group.setLayout(new FillLayout());
final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
tree.setHeaderVisible(true);
GUIToolkit.createColumns(tree, columns);
GUIToolkit.addColumnSort(tree, DATA);
GUIToolkit.removeDoubleClickExpand(tree);
I think your problem is that you don't assign a GridData to the Group containing the Tree and the Tree itself.
Try updating your code like this:
private static Tree createTree(final Composite parent, final String text, final String... columns) {
final Composite group = GUIToolkit.newGroup(parent, SWT.NONE, text, new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
group.setFont(FontManager.NORMAL_BOLD);
group.setLayout(new FillLayout());
final Tree tree = new Tree(group, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); // <-- THIS
tree.setHeaderVisible(true);
GUIToolkit.createColumns(tree, columns);
GUIToolkit.addColumnSort(tree, DATA);
GUIToolkit.removeDoubleClickExpand(tree);
}
Not sure what exactly GUIToolkit is, but I'm assuming it will just apply the LayoutData you provide.
UPDATE:
Ok, this is how I would do it:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(1, false));
Group top = new Group(shell, SWT.NONE);
top.setLayout(new GridLayout(1, false));
top.setText("Top");
top.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Group bottom = new Group(shell, SWT.NONE);
bottom.setLayout(new GridLayout(1, false));
bottom.setText("Bottom");
bottom.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Tree topTree = new Tree(top, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
createColumns(topTree);
Tree bottomTree = new Tree(bottom, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
createColumns(bottomTree);
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}
private static void createColumns(Tree tree)
{
tree.setHeaderVisible(true);
for(int i = 0; i < 3; i++)
new TreeColumn(tree, SWT.NONE).setText("Col " + i);
for(int i = 0; i < 10; i++)
{
TreeItem item = new TreeItem(tree, SWT.NONE);
for(int j = 0; j < tree.getColumnCount(); j++)
{
item.setText(j, "Item " + i + " " + j);
}
}
for(int i = 0; i < tree.getColumnCount(); i++)
tree.getColumn(i).pack();
}

SWT text output and new lines

I have a program that I'm using as an experiment to get used to GUI. It basically takes a quadratic in f(x)=ax^2+bx+c form and finds the real zeros, y-intercept, and axis of symmetry.. It works well as far as the calculations and all. My issue is that I created a non editable text box (using window builder SWT app) and no matter what I do, it always prints everything on one single line! \n doesnt work, \r\n doesnt work... Please help.
Button btnNewButton = new Button(shlParacalc, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
String aval = alphabox.getText();
double a = Double.parseDouble(aval);
String bval = betabox.getText();
double b = Double.parseDouble(bval);
String cval = gammabox.getText();
double c = Double.parseDouble(cval);
CalcLib mathObj = new CalcLib();
double yint = mathObj.yIntercept(a, b, c);
double axis = mathObj.Axis(a, b);
double zero[] = mathObj.Zero(a, b, c);
outputbox.append("y-intercept = " + yint); // these four lines need
outputbox.append("axis of symmetry = " + axis); //to be printed
outputbox.append("1st zero = " + zero[0]); //on individual lines
outputbox.append("2nd zero = " + zero[1]);
You're probably using the wrong style bits. This code works:
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final Text text = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
Button button = new Button(shell, SWT.NONE);
button.setText("Add text");
button.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event e)
{
text.append("y-intercept = \n");
text.append("axis of symmetry = \n");
text.append("1st zero = \n");
text.append("2nd zero = \n");
}
});
shell.pack();
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}

Categories