I Have a GridLayout filled with Composites in a random order. Now I'm sorting the Composites that are filling the GridLayout in a List/Collection and want to order them like the sort result from my List/Collection. I tried to do it by allocating them again to their parent so they are in the right order, but for some reason nothing happened. Then I tried to cache them in a Composite you don't see and then bring them back to the parent with the same method as in the first attempt. No change at all. Anyone has a pointer? I'm ordering by date, just in case /so want to know.
Thats how my grid looks like, now I want to order them like in my arrayList();
The methods you are looking for are Control#moveAbove(Control control) and Control#moveBelow(Control control) to reorder the items:
private static List<Label> labels = new ArrayList<Label>();
private static List<Color> colors = new ArrayList<Color>();
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("Stackoverflow");
shell.setLayout(new RowLayout(SWT.VERTICAL));
colors.add(display.getSystemColor(SWT.COLOR_BLUE));
colors.add(display.getSystemColor(SWT.COLOR_CYAN));
colors.add(display.getSystemColor(SWT.COLOR_GREEN));
colors.add(display.getSystemColor(SWT.COLOR_YELLOW));
colors.add(display.getSystemColor(SWT.COLOR_RED));
for (int i = 0; i < 5; i++)
{
Label label = new Label(shell, SWT.BORDER);
label.setText("Button " + i);
label.setBackground(colors.get(i));
labels.add(label);
}
Button sortButton = new Button(shell, SWT.TOGGLE);
sortButton.setText("Sort");
sortButton.addListener(SWT.Selection, new Listener()
{
#Override
public void handleEvent(Event e)
{
Button source = (Button) e.widget;
final boolean asc = source.getSelection();
Label oldFirst = labels.get(0);
Collections.sort(labels, new Comparator<Label>()
{
#Override
public int compare(Label o1, Label o2)
{
int result = o1.getText().compareTo(o2.getText());
if (asc)
result = -result;
return result;
}
});
Label label = labels.get(0);
label.moveAbove(oldFirst);
for (int i = 1; i < labels.size(); i++)
{
labels.get(i).moveBelow(labels.get(i - 1));
}
shell.layout();
}
});
shell.pack();
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
After starting:
After pressing the button:
I found the solution. You have to call child.moveAbove(otherChild) or .moveBelow() When you are done with the reordering just call on the parent Composite parent.layout()
Related
When trying to add a lot of labels on to a Composite that is contained in a ScrolledComposite after a certain number (1638) for me, it just seems to give up and stop drawing the components after it. Is this a hard limit on a number of things that can be displayed or something I'm doing wrong.
This also happens if I just add one label with 2000 lines of text in.
public class LoadsOfLabelsTestDialog extends Dialog
{
List<Label> displaylabels = new ArrayList<>();
Composite content, list;
ScrolledComposite scroll;
public LoadsOfLabelsTestDialog(Shell parentShell)
{
super(parentShell);
}
#Override
protected void configureShell(Shell shell)
{
super.configureShell(shell);
shell.setSize(new Point(700, 500));
shell.setText("FML"); //$NON-NLS-1$
}
#Override
public Control createDialogArea(final Composite comp)
{
content = (Composite) super.createDialogArea(comp);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button set1 = new Button(content, SWT.PUSH);
set1.setText("Display List 1");
set1.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
List<String> rows = new ArrayList<>();
for (int i = 0; i < 2000; i++) {
rows.add(i +" row");
}
updateList(rows);
}
});
scroll = new ScrolledComposite(content, SWT.V_SCROLL);
list = new Composite(scroll, SWT.NONE);
list.setLayout(new GridLayout(1, true));
scroll.setContent(list);
scroll.setExpandHorizontal(true);
scroll.setExpandVertical(true);
scroll.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
new Label(content, SWT.HORIZONTAL | SWT.SEPARATOR);
setScrollSize();
return content;
}
private void setScrollSize() {
scroll.setMinSize(list.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
private void updateList(List<String> rows) {
if (this.displaylabels == null) {
this.displaylabels = new ArrayList<>();
}
for (Label l : displaylabels) {
l.dispose();
}
this.displaylabels.clear();
for (String item : rows) {
addListLabel(item);
}
content.layout(true, true);
setScrollSize();
}
private void addListLabel(String whoText) {
Label a = new Label(list, SWT.NONE);
a.setText(whoText);
this.displaylabels.add(a);
}
public static void main(String[] args)
{
Display d = new Display();
Shell s = new Shell();
LoadsOfLabelsTestDialog fml = new LoadsOfLabelsTestDialog(s);
fml.open();
}
}
You hit a hard limit, probably the maximum size of a control. While this limit may differ slightly on other platforms, you can't size a control arbitrarily.
As #greg-449 suggested, prefer using a Table. If the content per table row is more than just an image and text, you can add a paint listener to draw the row contents yourself.
I just started to learn Java. I have already looked Swing and at the moment I'm trying to do something with SWT.
But I have the next problem. Key Listener that I added for Text field is working, but inside this listener I can't change for example my label.
I have seen a few demos they worked, but I don't see any differences.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class FirstClass {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("First Application");
GridLayout layout = new GridLayout(2, false);
shell.setLayout(layout);
Text word = new Text(shell,SWT.SINGLE | SWT.BORDER | SWT.FILL);
GridData wordGrDt = new GridData();
wordGrDt.heightHint = 130;
wordGrDt.minimumHeight = 130;
wordGrDt.horizontalAlignment = SWT.FILL;
wordGrDt.verticalAlignment = SWT.FILL;
wordGrDt.horizontalSpan = 2;
word.setLayoutData(wordGrDt);
GridData statusGrDt = new GridData();
statusGrDt.grabExcessHorizontalSpace = true;
statusGrDt.horizontalSpan = 1;
statusGrDt.horizontalAlignment = SWT.LEFT_TO_RIGHT;
Label status = new Label(shell, SWT.PUSH);
status.setEnabled(true);
status.setText("");
status.setLayoutData(statusGrDt);
GridData checkGrDt = new GridData();
checkGrDt.widthHint = 150;
checkGrDt.horizontalSpan = 1;
checkGrDt.horizontalAlignment = SWT.RIGHT_TO_LEFT;
Button check = new Button(shell, SWT.PUSH);
check.setText("Check");
check.setLayoutData(checkGrDt);
word.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
if (e.keyCode == SWT.CR) {
System.out.println("worked!!!");
status.setText("ababahalamaha");
}
}
});
shell.setMinimumSize(400, 300);
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Add a layout() call to the parent of the Label:
word.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
if (e.keyCode == SWT.CR) {
System.out.println("worked!!!");
status.setText("ababahalamaha");
status.getParent().layout();
}
}
});
The label originally has a width of 0, as it doesn't contain any text. When you add content, the parent has to know to re-layout its children.
As a note:
Please check which style you use with which widget. A Label does not know what to do with the style SWT.PUSH for example.
I wish to use org.eclipse.swt.widgets.List just to present some data. User should not be allowed to select any item.
I could just:
List list = new List(this, SWT.V_SCROLL);
list.setEnabled(false);
But then I will loose scrolling feature. How can I just make list items unselectable?
Another alternative is to use a Table instead of List and disable selection painting like this:
table.addListener(SWT.EraseItem, new Listener() {
#Override
public void handleEvent(Event event) {
event.detail &= ~SWT.SELECTED;
event.detail &= ~SWT.HOT;
}
});
You could try to clear selection each time user selects an item. The selection will be visible for a short time interval, though.
list.addListener(SWT.Selection, new Listener() {
#Override
public void handleEvent(Event event) {
list.setSelection(new String[0]);
}
});
If you don't like my other answer with clearing selection, you could try to keep the list disabled, but inside a ScrolledComposite. It will look disabled, but scrolling will work. Here is a snippet:
public static void main(String[] args) {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
final ScrolledComposite scrolledComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
final List list = new List(scrolledComposite, SWT.NONE);
list.setEnabled(false);
scrolledComposite.setContent(list);
scrolledComposite.addListener(SWT.Resize, new Listener() {
#Override
public void handleEvent(Event event) {
final Point size = list.computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
scrolledComposite.setMinSize(size);
}
});
for (int i = 0; i < 1000; i++) {
list.add(Integer.toString(i));
}
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
Arrow keys and page up/down down do not work, so you will have to register key listeners and implement scrolling with keyboard.
I'm trying to do look alike chess-board in SWT, JAVA.
I tried to do array of buttons, but the color of a button can't be changed (after a long research I have done!).
So I did an array of labels that I can change their color, but now I cannot handle them in one listener, and I don't think that 64 copy-paste listeners are the right thing to do.
However, I found the setActionCommand is not for labels at all.
Do you have any suggestions what can I do to fix that out?
Thanks.
You can use the same Listener for multiple Labels:
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout(8, true);
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
shell.setLayout(layout);
shell.setText("Chess");
/* Define listener once */
Listener listener = new Listener()
{
#Override
public void handleEvent(Event event)
{
/* event.widget is the source of the event */
if(event.widget instanceof Label)
{
System.out.println(event.widget.getData());
}
}
};
for(int i = 0; i < 64; i++)
{
Label label = new Label(shell, SWT.CENTER);
label.setText(i + "");
label.setData(i);
/* Use listener here */
label.addListener(SWT.MouseUp, listener);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Color background = ((i + (i/8))%2 == 0) ? display.getSystemColor(SWT.COLOR_BLACK) : display.getSystemColor(SWT.COLOR_WHITE);
Color foreground = ((i + (i/8))%2 == 0) ? display.getSystemColor(SWT.COLOR_WHITE) : display.getSystemColor(SWT.COLOR_BLACK);
label.setBackground(background);
label.setForeground(foreground);
}
shell.pack();
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
It will print out the Labels data on click.
Looks like this:
Group group = new Group(parent, SWT.NONE);
StyledText comment = new StyledText(group, SWT.BORDER_DASH);
This creates a group with a text area inside.
How can I later delete the text (remove it from the screen so that I can replace it with something else)?
Use Widget.dispose.
public class DisposeDemo {
private static void addControls(final Shell shell) {
shell.setLayout(new GridLayout());
Button button = new Button(shell, SWT.PUSH);
button.setText("Click to remove all controls from shell");
button.addSelectionListener(new SelectionListener() {
#Override public void widgetDefaultSelected(SelectionEvent event) {}
#Override public void widgetSelected(SelectionEvent event) {
for (Control kid : shell.getChildren()) {
kid.dispose();
}
}
});
for (int i = 0; i < 5; i++) {
Label label = new Label(shell, SWT.NONE);
label.setText("Hello, World!");
}
shell.pack();
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
addControls(shell);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Another option is to use a StackLayout to switch between underlying controls. This prevents you from running into a "widget is disposed" error.
You have to either call comment.changeParent(newParent) or comment.setVisible(false) to remove/hide it from the Group. I am unsure if comment.changeParent(null) would work but I would give that a try.
We do it this way because SWT uses the Composite Pattern.
group.getChildren()[0].dispose() will remove the first child. You need to find a way to identify the precise child you want to delete. It could be comparing the id. You can do that by using the setData / getData on that control:
For example:
StyledText comment = new StyledText(group, SWT.BORDER_DASH);
comment.setData("ID","commentEditBox");
and then:
for (Control ctrl : group.getChildren()) {
if (control.getData("ID").equals("commentEditBox")) {
ctrl.dispose();
break;
}
}