Eclipse SWT Text Boxes change size - java

I had an Eclipse plug-in app (ten-year-old code, no documentation, etc.) dropped in my lap and while adding new features to it, I noticed that when a panel is resized, the text boxes change size continuously while the separator is being dragged.
As you can see in the second picture, the text boxes are kind of randomly sized. Is there a setting in SWT that will prevent this from happening?
Here's how I'm creating one of the text boxes. The others are basically clones of this:
Font font = parent.getFont();
setLayout(new FillLayout());
SashForm sashForm = new SashForm(this, SWT.VERTICAL);
FormToolkit toolkit = new FormToolkit(getParent().getDisplay());
Section section = toolkit.createSection(sashForm,
Section.DESCRIPTION | ExpandableComposite.TITLE_BAR | ExpandableComposite.EXPANDED);
section.setLayoutData(new GridData(GridData.FILL_BOTH));
section.setLayout(new GridLayout());
section.setText("Section Title");
Composite controlComposite = toolkit.createComposite(section);
GridLayout controlLayout = new GridLayout();
controlLayout.numColumns = 2;
controlLayout.verticalSpacing = 20;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Font bold = ResourceManager.getBoldFont(font);
Label textLabel = toolkit.createLabel(controlComposite, "Title:", SWT.BOLD);
GridData gd = new GridData();
gd.horizontalSpan = 1;
textLabel.setLayoutData(gd);
textLabel.setFont(bold);
textBox = new ExtendedText(controlComposite, SWT.BORDER | SWT.SINGLE, false);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = 1;
gd.verticalSpan = 2;
textBox.setLayoutData(gd);
The ExtendedText class is an extension of StyledText. The important bits of it are this:
GridData gd_bg = new GridData(GridData.FILL_BOTH);
setLayoutData(gd_bg);
final GridLayout gridLayout = new GridLayout();
gridLayout.verticalSpacing = 0;
gridLayout.marginWidth = 0;
gridLayout.marginHeight = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.numColumns = 1;
gridLayout.makeColumnsEqualWidth = true;
sashForm.setWeights(new int[] { 1, 1 });

Okay, after digging in a little deeper, I got it working as expected.
First, the controlComposite and controlLayout objects are now created using
Composite controlComposite = new Composite(section, SWT.NONE)
controlComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
controlComposite.setBackground(section.getBackground());
GridLayout controlLayout = new GridLayout(2, true);
controlLayout.marginHeight = 20;
controlLayout.marginWidth = 0;
controlLayout.verticalSpacing = 10;
controlLayout.horizontalSpacing = 0;
controlComposite.setLayout(controlLayout);
section.setClient(controlComposite);
Once I did that, things started to stabilize. I also ended up tweaking the weights to this:
sashForm.setWeights(new int[] { 2, 3 });
It's not perfect, but it'll do for now.
Thanks to #greg-449 and #Baz for taking a look

Related

Low Resolution, Blurry TrueType font in LibGDX

I'm programming a game in LibGDX, but when I try to display some text on the screen with a TrueType font (.ttf), it comes out somewhat blurry.
Why is this happening?
This is how I'm using the text in my code, implemented from this thread How to draw smooth text in libgdx?:
stage = new Stage(viewport, game.batch);
table = new Table();
table.center();
table.setFillParent(true);
generator = new FreeTypeFontGenerator(Gdx.files.internal("Lato-Bold.ttf"));
parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 30;
parameter.color = Color.WHITE;
parameter.characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
font = generator.generateFont(parameter);
generator.dispose();
Label.LabelStyle labelStyle = new Label.LabelStyle(font, font.getColor());
touchToStartLabel = new Label("Touch To Start", labelStyle);
table.add(touchToStartLabel);
stage.addActor(table);
Any ideas on how I can get a better rendered text? It's been bugging me for a while.
EDIT: I tried to use trilinear filtering by changing the code to this (and I'm using a FitViewport in case that's important):
generator = new FreeTypeFontGenerator(Gdx.files.internal("Lato-Bold.ttf"));
parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 30;
parameter.color = Color.WHITE;
parameter.minFilter = Texture.TextureFilter.MipMapLinearLinear;
parameter.magFilter = Texture.TextureFilter.Linear;
parameter.characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
font = generator.generateFont(parameter);
font.setUseIntegerPositions(false);
All I got was this (at least the squares are sharp):

Eclipse plugin: Show window with scrollable text

In an eclipse plugin, I try to show the user a dialog that just contains a long text. This text should be scrollable.
I tried:
protected Control createDialogArea(Composite parent)
{
Composite container = (Composite) super.createDialogArea(parent);
Text text = new Text(container, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL| SWT.MULTI);
text.setText(" " + command + "\n\r\n\r" + result);
return container;
}
The text is then shown with a disabled scrollbar (although it is larger than the size of the window). How do I enable scrolling?
The issue seems to be, that your layoutdata on the text is not limited. So SWT appears to have no idea when to enable scrolling.
Setting griddata to fill both did not work for me with your code (just tried).
However, this will:
public static void main(String[] args) {
Display display = Display.getDefault();
Shell s = new Shell(display);
s.setSize(300, 300);
s.setLayout(new GridLayout(1, true));
Composite c = new Composite(s, SWT.NONE);
c.setLayout(new GridLayout(1, false));
Text text = new Text(c, SWT.H_SCROLL | SWT.V_SCROLL | SWT.READ_ONLY);
GridData gridData = new GridData(SWT.NONE, SWT.NONE, false, false);
gridData.heightHint = 200;
gridData.widthHint = 200;
text.setLayoutData(gridData);
text.setBackground(s.getDisplay().getSystemColor(SWT.COLOR_WHITE));
text.setSize(250, 250);
Font stdFont = new Font(text.getDisplay(), new FontData("Consolas", 11, SWT.NORMAL));
text.setFont(stdFont);
StringBuffer buffer = new StringBuffer();
for (int row = 0; row < 40; row++) {
for (int column = 0; column < 20; column++) {
buffer.append("Word ");
}
buffer.append("\n");
}
text.setText(buffer.toString());
s.open();
while (!s.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
By restricting the size of your Text properly (with layoutdata, not setting the size), SWT now knows when the text is bigger than the area and enables scrolling.
Mind you, your solution does work, if you type something after creating (i know not possible for your case).
I works if you also set
text.setLayoutData(new GridData(GridData.FILL_BOTH));

Adding help button to SWT GUI

I have a simple swt GUI in my Eclipse application, which looks like the following:
It is implemented very simply:
// creating the label
Label label = new Label(composite, SWT.NONE);
label.setText("Label");
// creating the input field
Text text = new Text(composite, SWT.BORDER);
gridData.horizontalAlignment = SWT.FILL;
gridData.grabExcessHorizontalSpace = true;
text.setLayoutData(gridData);
I would like to add an button between the label and the input element, so that the user can get additional help on what to add inide the field.
It can either be a help button or just a icon which shows information in mouse hover.
How do I implement that? I would appreciate any help!
One of the many ways to do this is to use an information field decoration.
Something like:
Text text = new Text(composite, SWT.BORDER);
FieldDecorationRegistry decRegistry = FieldDecorationRegistry.getDefault();
FieldDecoration infoField = decRegistry.getFieldDecoration(FieldDecorationRegistry.DEC_INFORMATION);
ControlDecoration decoration = new ControlDecoration(text, SWT.TOP | SWT.LEFT);
decoration.setImage(infoField.getImage());
decoration.setDescriptionText("Info decoration text");
GridData gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
// Space for decoration image
gridData.horizontalIndent = decRegistry.getMaximumDecorationWidth();
text.setLayoutData(gridData);

How to set focus on a checkbox when it is grouped with radio buttons inside a composite control

I have created composite control that has check box and two radio buttons.This is something like controlling radio buttons with a check box, i.e. radios will be enabled only after check box selected
I want to set the focus on the check box when the page comes up(using CTRl+TAB for composite traversal), it works fine in the case of none of radio buttons are enabled.
If any of the radio button is selected then focus is going to that radio button but not to the checked box.Is this windows default behavior, focus should be on selected radio button ??
How can i make focus should always be on check box ??
I tried to use setFocus on checkbox, but that didnt help.
All these three buttons are in a group.
Here is the sample code
Composite composite = this;
GridData LData = new GridData();
LData.horizontalSpan = 1;
LData.horizontalAlignment = GridData.FILL;
LData.grabExcessHorizontalSpace = true;
LData.verticalAlignment = GridData.FILL;
LData.grabExcessVerticalSpace = true;
composite.setLayoutData(LData);
composite.setLayout(new GridLayout(1, false));
Label infoLabel = new Label(composite, SWT.NONE);
infoLabel.setText("Test");
rGroup = new Group(composite, SWT.NONE);
GridData rGroupLData = new GridData();
rGroupLData.grabExcessHorizontalSpace = true;
rGroupLData.horizontalAlignment = GridData.FILL;
rGroup.setLayoutData(rGroupLData);
GridLayout rGroupLayout = new GridLayout(5, false);
rGroup.setLayout(rGroupLayout);
checkBox1 = new Button(rGroup, SWT.CHECK);
checkBox1.setText("CheckBox");
GridData rcheckBox1LData = new GridData();
rcheckBox1LData.horizontalSpan = 5;
rcheckBox1LData.verticalAlignment = GridData.CENTER;
rcheckBox1LData.grabExcessHorizontalSpace = true;
rcheckBox1LData.horizontalAlignment = GridData.FILL;
checkBox1.setLayoutData(rcheckBox1LData);
GridData r1LData = new GridData();
r1LData.horizontalAlignment = GridData.CENTER;
r1LData.grabExcessHorizontalSpace = true;
r1LData.verticalAlignment = GridData.CENTER;
r1Numeric = new Button(rGroup, SWT.RADIO);
r1Numeric.setText("Radio1");
r1Numeric.setSelection(true);
r1Numeric.setLayoutData(r1LData);
r1Numeric.setEnabled(false);
GridData selR1LData = new GridData();
selR1LData.horizontalAlignment = GridData.CENTER;
selR1LData.grabExcessHorizontalSpace = true;
selR1LData.verticalAlignment = GridData.CENTER;
selR1Value = new Button(rGroup, SWT.NONE);
selR1Value
.setText("Select R1");
selR1Value.setLayoutData(selR1LData);
selR1Value.setEnabled(false);
GridData r2LData = new GridData();
r2LData.horizontalAlignment = GridData.CENTER;
r2LData.grabExcessHorizontalSpace = true;
r2LData.verticalAlignment = GridData.CENTER;
r2Numeric = new Button(rGroup, SWT.RADIO);
r2Numeric.setText("Radio2");
r2Numeric.setLayoutData(r2LData);
r2Numeric.setSelection(false);
r2Numeric.setEnabled(false);
GridData selR2LData = new GridData();
selR2LData.horizontalAlignment = GridData.CENTER;
selR2LData.grabExcessHorizontalSpace = true;
selR2LData.verticalAlignment = GridData.CENTER;
selR2Value = new Button(rGroup, SWT.NONE);
selR2Value
.setText("Select R2");
selR2Value.setLayoutData(selR2LData);
selR2Value.setEnabled(false);
Given the code provided is not exactly what you have on your machine, I could not reproduce your problem, but here I could traverse through all components.
Anyway, you can set the component's tab order. Based on your example, try adding this code after creating everything:
rGroup.setTabList( new Control[] { checkBox1, r1Numeric, selR1Value, r2Numeric, selR2Value } );
Besides that, you could also implement, if it exists, the page's setFocus method, making it set focus on the checkbox.

Grid Layout does not display correctly

I'm trying to build simple calculator gui with display and 9 buttons
public void init()
{
setSize(60,80);
inf = new InfoButton(this);
zero = new CalcButton(this,"0");
one = new CalcButton(this,"1");
add = new CalcButton(this,"+");
sub = new CalcButton(this,"-");
div = new CalcButton(this,"/");
mlt = new CalcButton(this,"*");
modu = new CalcButton(this,"%");
blank = new JButton("");
wys = new Wyswietlacz(); // its JTextPane
wys.setSize(60,20);
przyciski = new JPanel();
przyciski.setSize(60,60);
przyciski.setLayout(new GridLayout(3,3));
przyciski.add(zero);
przyciski.add(one);
przyciski.add(add);
przyciski.add(sub);
przyciski.add(mlt);
przyciski.add(div);
przyciski.add(modu);
przyciski.add(inf);
przyciski.add(blank);
calosc = new JPanel();
calosc.setLayout(new BoxLayout(calosc,BoxLayout.Y_AXIS));
calosc.add(wys);
calosc.add(przyciski);
calosc.setSize(60,80);
add(calosc);
}
and in main i make frame with size (60,80) but when i make it visible all i can see is display and one row of buttons. What am i doing wrong?
Call setPreferredSize(..) instead of setSize() on wys and przyciski. Then use JFrame's pack() instead of specifying a size for it.

Categories

Resources