Java SWT: Label width not changing after changing it's text - java

I read a lot about this problem here in Stack Overflow and applied all the proposed solutions (getShell pack, layout, getparent layout etc...) and none of them worked.
I have a label with a text value. Also I have a button and when click on that button I'm changing the content of the label with a longer text. The problem is that the width of the label doesn't change and only a part of the new longer text is visible.
My text code:
Composite composite_2 = new Composite(this, SWT.NONE);
composite_2.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true, 1, 1));
composite_2.setLayout(new GridLayout(2, false));
Label lblNumbers = new Label(composite_2, SWT.NONE);
lblNumbers.setText("NĂºmeros:");
Label lblNumbersValue = new Label(composite_2, SWT.NONE);
lblNumbersValue.setText("...");
.
.
.
btnElegirNmeros.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
lblNumbersValue.setText("asddadadasdasdasd");
lblNumbersValue.getParent().layout();
lblNumbersValue.pack();
getShell().pack();
getShell().layout();
}
});

Since SWT 4.6, the most reliable method to trigger a layout update is Control.requestLayout().
It should solve these kind of problems.
So in your case try lblNumbersValue.requestLayout();.

Related

How to create a SWT text field with inactive text as a suffix?

I am using Java's SWT toolkit to create a GUI with text field inputs. These input fields require numerical input and have units assigned to them. I'm trying to create a fancy way to integrate units within the field as a fixed suffix to the text, such that the user can only edit the numerical part. I'd also like for the suffix to be greyed out so the user knows it is disabled - something like the following:
While searching, I saw some solutions with a mask formatter from Swing that might do the trick, but I'm sort of hoping there might be something default with SWT. Any suggestions on how to make this work?
The field is part of a a matrix, so I can't simply add the units to a header label. I suppose I could create another column after the text field that could provide units as a label, but I'm going for something more intuitive and aesthetic.
Any suggestions?
One option would be to group Text and Label widgets in the same composite, and set the text on the Label to the desired suffix:
The area to the left of the suffix is the single-line text field, which can be edited, and the suffix is a disabled Label.
public class TextWithSuffixExample {
public class TextWithSuffix {
public TextWithSuffix(final Composite parent) {
// The border gives the appearance of a single component
final Composite baseComposite = new Composite(parent, SWT.BORDER);
baseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
final GridLayout baseCompositeGridLayout = new GridLayout(2, false);
baseCompositeGridLayout.marginHeight = 0;
baseCompositeGridLayout.marginWidth = 0;
baseComposite.setLayout(baseCompositeGridLayout);
// You can set the background color and force it on
// the children (the Text and Label objects) to add
// to the illusion of a single component
baseComposite.setBackground(new Color(parent.getDisplay(), new RGB(255, 255, 255)));
baseComposite.setBackgroundMode(SWT.INHERIT_FORCE);
final Text text = new Text(baseComposite, SWT.SINGLE | SWT.RIGHT);
text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final Label label = new Label(baseComposite, SWT.NONE);
label.setEnabled(false);
label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, true));
label.setText("kg/m^3");
}
}
final Display display;
final Shell shell;
public TextWithSuffixExample() {
display = new Display();
shell = new Shell(display);
shell.setLayout(new GridLayout());
shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
new TextWithSuffix(shell);
}
public void run() {
shell.setSize(200, 100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void main(final String[] args) {
new TextWithSuffixExample().run();
}
}

Redraw Eclipse RCP Editor after adding a widget

In my Eclipse plugin editor there is a button called "Add Graph" which adds a composite with a graph on another already created composite with "Grid Layout", but the created composite is not appearing until resizing the Editor manually or switch to another editor and go back
i have also used
parentComposite.pack();
parentComposite.layout();
but the added composite is shown up with a small size, and it`s adjusted also after resizing the Editor manually or switch to another editor and go back.
So my Question is how to call the same handler which runs while resizing the Editor manually or switch to another editor and go back. to adjust my view after adding the new composite.
Adding the Example:
public class MyEditor extends EditorPart {
private Composite compGraphesArea;
#Override
public void createPartControl(Composite parent) {
Composite container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout(2, false));
Button btnAddGraph = new Button(container, SWT.NONE);
btnAddGraph.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
createGraph(compGraphesArea);
}
});
btnAddGraph.setText("Add Graph");
compGraphesArea = new Composite(container, SWT.NONE);
compGraphesArea.setLayout(new GridLayout(1, true));
compGraphesArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
createGraph(compGraphesArea);
}
private void createGraph(Composite compGraphArea) {
JFreeChart chart = chartHandler.getChart();
compGraphPlot = new Composite(compGraphArea, SWT.BORDER | SWT.EMBEDDED);
compGraphPlot.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
Frame chartFrame = SWT_AWT.new_Frame(compGraphPlot);
ChartPanel label = new ChartPanel(chart);
chartFrame.add(label);
chartFrame.pack();
chartFrame.setVisible(true);
XYPlot plot = chart.getXYPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setFixedAutoRange(1000000 + slider.getMaximum() / 2);
}
And this what i got after pressing "Add Graph":
and this what i am expecting and what i got after making any change on the Editor manually:

How can I show Scroll on my UI, with JAVA, SWT?

I'm trying to put a ScrolledComposite on my UI with SWT.
but now it only shows blank area or something wrong.
The area to show ScrolledComposite is blank without the code,
"optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT)); "
This is the pic of screenshot.
The optionTab should have the buttons with Scroll....
and with the code, it shows scroll, but too long height and narrow width.
Somehow, the upper is gone, but come back when I extend the size of window with scroll disappearing.
I set the color of background red to make it easier to see for you.
Can anyone help me make "optionCompositeAtLeft" filled in ScrolledComposite?
I want the left area of Option tab to be filled with a red background from "optionCompositeAtLef".
Below is my code.
private BomCloneDialog dialog = null;
private TabFolder tabFolder = null;
private TabItem tabOption = null;
public CreateOptionTab(TabFolder tabFolder, TabItem tabOption) {
this.tabFolder = tabFolder;
this.tabOption = tabOption;
}
public void createOptionTabUI() {
GridData gdWithFillBoth = new GridData(GridData.FILL_BOTH);
Composite optionComposite = new Composite(tabFolder, SWT.NONE);
optionComposite.setLayout(new GridLayout(2, true));
tabOption.setControl(optionComposite);
ScrolledComposite scrolledCompositeForOption = new ScrolledComposite(optionComposite,SWT.V_SCROLL | SWT.BORDER);
scrolledCompositeForOption.setLayoutData(gdWithFillBoth);
Composite optionCompositeAtLeft = new Composite(scrolledCompositeForOption, SWT.BORDER);
scrolledCompositeForOption.setContent(optionCompositeAtLeft);
optionCompositeAtLeft.setLayout(new GridLayout(1, true));
optionCompositeAtLeft.setLayoutData(gdWithFillBoth);
optionCompositeAtLeft.setBackground(new Color(Display.getCurrent(), 255,0,0));
Button b [] = new Button[30];
for(int a=0; a<30; a++){
b[a] = new Button(optionCompositeAtLeft, SWT.PUSH);
b[a].setText("button"+a);
}
**optionCompositeAtLeft.setSize(optionCompositeAtLeft.computeSize(SWT.DEFAULT, SWT.DEFAULT));**
Composite optionButtonComposite = new Composite(optionComposite, SWT.BORDER);
optionButtonComposite.setLayout(new GridLayout(1, true));
optionButtonComposite.setLayoutData(gdWithFillBoth);
Button optionSaveButton = new Button(optionButtonComposite, SWT.NONE);
optionSaveButton.setLayoutData(gdWithFillBoth);
optionSaveButton.setText("Save");
Button optionAddButton = new Button(optionButtonComposite, SWT.NONE);
optionAddButton.setLayoutData(gdWithFillBoth);
optionAddButton.setText("Add");
}
You need to configure ScrolledComposite to resize the content:
scrolledCompositeForOption.setExpandHorizontal(true);
scrolledCompositeForOption.setExpandVertical(true);

ScrolledComposite children not populating content

I have a ScrolledComposite widget that I'm initializing inside a selection listener (in turn attached to another composite). This ScrolledComposite contains in turn another Composite, which in turn holds a Button and a Label. While the internal composite appears, none of its children widgets do. I've used ScrolledComposite plenty of times before, and everything looks right to my eye. Can any of you see anything wrong? Note, the ScrolledComposite is a class variable. Also note that this problem is occurring regardless of if I ever dispose of the composite and its contents in the else condition.
final Button showConsole = new Button(topLeft, SWT.CHECK);
showConsole.setText("Show Debug Console");
showConsole.setFont(new Font(domains.getDisplay(), "Segoe UI", 9, SWT.ITALIC));
showConsole.setSelection(false);
showConsole.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
//The total widget group is only supposed to appear when the button is selected
if (showConsole.getSelection()) {
scrolledConsoleComp = new ScrolledComposite(leftComposite,
SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
Composite consoleComposite = new ScrolledComposite(scrolledConsoleComp, SWT.NONE | SWT.BORDER);
consoleComposite.setLayout(new GridLayout());
consoleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
consoleComposite.setVisible(true);
scrolledConsoleComp.setContent(consoleComposite);
scrolledConsoleComp.setExpandHorizontal(true);
scrolledConsoleComp.setExpandVertical(true);
scrolledConsoleComp.setLayout(new GridLayout());
scrolledConsoleComp.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
Button clear = new Button(consoleComposite, SWT.PUSH);
clear.setText("Clear Console");
final Label consoleText = new Label(consoleComposite, SWT.WRAP);
consoleText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
consoleText.setText("Messages: \n" + consoleData);
clear.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
consoleData = "";
consoleText.setText("Messages: \n" + consoleData);
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
scrolledConsoleComp.setMinSize(leftComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
leftComposite.layout(true);
} else {
scrolledConsoleComp.setVisible(false);
scrolledConsoleComp.dispose();
leftComposite.layout(true);
}
}
I appreciate any insight. Let me know if anything in this question is unclear. Thank you!
You're creating your Button and your Label inside a ScrolledComposite. They won't be shown because setContent() is not called.
Usually a ScrolledComposite contains a Composite that holds all the other Widgets.
Your consoleComposite should become a Composite.
No need for a call to setVisible()

Increase the size of my form , when user maximizes the window, in eclipse

In eclipse i have a layout with table , buttons text fields. I have created them in the:
public class NewForm extends FormPage{
#Override
protected void createFormContent(final IManagedForm managedForm) {
FormToolkit ftk= managedForm.getToolkit();
ScrolledForm scrldfrm = managedForm.getForm();
scrldfrm.getBody().setLayout(null);
scrldfrm.setText("Hello there!");
Section section = managedForm.getToolkit().createSection(
managedForm.getForm().getBody(),
Section.TWISTIE | Section.TITLE_BAR);
section.setBounds(522, 10, 374, 21);
managedForm.getToolkit().paintBordersFor(section);
section.setText("Selected API");
section.setExpanded(true);
textName = new Text(managedForm.getForm().getBody(), SWT.BORDER);
textName.setBounds(610, 67, 275, 21);
managedForm.getToolkit().adapt(textName, true, true);
textName.setEnabled(false);
//similarly table is added.
}
}
Everything works fine until the user maximizes the window. After maximizing the window, the form contents remain at same position, well I have used absolute values in the form. How to make it increase size when window re-sizes? If i should not give absolute values in form, the how to avoid absolute values and give values relative to window size?
Use a Layout rather than setBounds calls. GridLayout is one possibility or FormLayout.
Something like:
ScrolledForm scrldfrm = managedForm.getForm();
scrldfrm.getBody().setLayout(new GridLayout());
scrldfrm.setText("Hello there!");
Section section = managedForm.getToolkit().createSection(
managedForm.getForm().getBody(),
Section.TWISTIE | Section.TITLE_BAR);
section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
managedForm.getToolkit().paintBordersFor(section);
section.setText("Selected API");
section.setExpanded(true);
textName = new Text(managedForm.getForm().getBody(), SWT.BORDER);
textName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false);
managedForm.getToolkit().adapt(textName, true, true);
textName.setEnabled(false);

Categories