Scrollbar is not showing in ScrolledForm - java

I am using ScrolledForm and using its property setAlwaysShowScrollBars(true) but still I am not getting scroll bar always.
Please count the reason: I want scroll bar to show always. I have used its reFlow(true) also by thinking problem in layout but it still not work.
formToolkit = new FormToolkit(parent.getDisplay());
form = formToolkit.createScrolledForm(parent);
form.setText("scrolled from");
form.setAlwaysShowScrollBars(true);
form.setFont(fontTahoma8Normal);
GridLayout layout = new GridLayout();
form.getBody().setLayout(layout);
GridData data = new GridData(GridData.FILL_HORIZONTAL
| GridData.VERTICAL_ALIGN_BEGINNING
| GridData.HORIZONTAL_ALIGN_BEGINNING);
form.getBody().setLayoutData(data);
return form;

Related

Some strings on Chinese locale are truncated in SWT Label in Eclipse RAP on the first dialog opening

After reloading this effect disappears and labels are in their suitable sizes. This repeats only if the application is reloaded. On English locale, this dialog shows normally.
Example of code containing problem label:
private void createDailyGroup(Composite composite)
{
m_typePanels[DAILY] = new Composite(composite, SWT.NONE);
Composite panel = m_typePanels[DAILY];
panel.setLayout(GridLayoutFactory.fillDefaults().numColumns(3).create());
panel.setLayoutData(GridDataFactory.fillDefaults().create());
addRunTime(panel, DAILY);
}
private void addRunTime(Composite panel, int scheduleType)
{
Label runTimeLabel = new Label(panel, SWT.NONE);
runTimeLabel.setText(QmfResources.getString(IDS_RUNTIME_LABEL));
runTimeLabel.setLayoutData(GridDataFactory.fillDefaults().
align(SWT.BEGINNING, SWT.CENTER).grab(false, false).create());
m_runTimes.put(Integer.valueOf(scheduleType), new DateTime(panel, SWT.TIME | SWT.BORDER | SWT.SHORT));
DateTime runTime = m_runTimes.get(Integer.valueOf(scheduleType));
runTime.addSelectionListener(m_parametersChangeListener);
runTime.setLayoutData(GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).create());
}
Try below code, here I am considering runTimeLabel is label where you want to add Chinese locale string
runTimeLabel.getParent().requestLayout();
runTimeLabel.getParent().redraw();
runTimeLabel.getParent().getParent().update();

Create empty table with specific height and redraw on button click in SWT

I want to create an empty SWT table with 2-3 empty rows. Also I want to fill this table with data upon button clicking.
From what I found on the web, it is not possible to create a table with empty rows. If I want empty rows, I would need to add some dummy data which I don't want to (a specific hight of 2-3 rows is good was well). Is there any other way?
Preferably I would have a table with header + 3 rows height which keeps that height and adds scrollbars as necessarily. How can I achieve this?
My layout for the table is set up like this:
Group tableGroup = new Group(parent, SWT.SHADOW_OUT);
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginWidth = 5;
gridLayout.marginHeight = 5;
tableGroup.setLayout(gridLayout);
tableGroup.setText("Test");
Composite tableComp = new Composite(tableGroup, SWT.NONE);
TableViewerBuilder tableViewerBuilder = new TableViewerBuilder(tableComp, SWT.BORDER | SWT.NO_SCROLL | SWT.V_SCROLL);
tableViewer = tableViewerBuilder.getTableViewer();
GridData gridData = new GridData();
gridData.heightHint = tableViewer.getTable().getHeaderHeight()
+ (5 * tableViewer.getTable().getItemHeight());
tableGroup.setLayoutData(gridData);
Note: TableViewerBuilder just creates a new TableViewer and sets TableColumnLayout to table.getParent()
The size and location of widgets in SWT is controlled by layout managers. Therefore, you need to instruct the layout manager of the table to give it the desired height.
If the parent of the table uses a GridLayout, you can use hints to influence the size computation of the layout manager.
parent.setLayout( new GridLayout( 1, false ) );
...
Table table = new Table( parent, SWT.NONE );
GridData gridData = new GridData();
gridData.heightHint = table.getHeaderHeight() + ( 3 * table.getItemHeight() );
table.setLayoutData( gridData );
The above example tells the grid layout to reserve a height of 3 times the item height, plus room for the header row, for the table.
Note that the getItemHeight() may change after you add items, in which case you would need to adjust the height hint and re-layout.
The accepted answer did help me much to achieve the result. Yet here I provide the final solution which worked for me:
Group tableGroup = new Group(parent, SWT.SHADOW_OUT);
FillLayout fillLayout = new FillLayout(SWT.VERTICAL);
fillLayout.marginWidth = 5;
fillLayout.marginHeight = 5;
tableGroup.setLayout(fillLayout);
data = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
tableGroup.setText("Test");
Composite tableComp = new Composite(tableGroup, SWT.NONE);
TableViewerBuilder tableViewerBuilder = new TableViewerBuilder(tableComp,
SWT.BORDER | SWT.NO_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION);
tableViewer = tableViewerBuilder.getTableViewer();
tableViewer.setContentProvider(OpenFigiResponseContentProvider.getInstance());
data.heightHint = tableViewer.getTable().getHeaderHeight()
+ (5 * tableViewer.getTable().getItemHeight());
tableGroup.setLayoutData(data);
tableViewer.getTable().setLayoutData(data);
I get a table which completely fill the Group

What is the proper way to add custom Controls to the Eclipse toolbar (using WorkbenchWindowControlContribution)?

I'm developing a set of Eclipse plugins, one of which is responsible for adding a toolbar to the Eclipse workspace.
While adding new commands (and the corresponding buttons) can be done in plugin.xml, I also need a text box and a label, which requires the addition of a Control to the plugin.xml, plus an implementation in Java that extends org.eclipse.ui.menus.WorkbenchWindowControlContribution. In practice, this comes down to overriding createControl(Composite parent) in the subclass.
This part is clear to me. The problem is that I'm not sure what type of Control object I should return.
I have tried the following:
Create a ToolBarManager, add an SWT Label and an STW Text to it (both wrapped in separate ControlContribution objects), and return the toolbar obtained by ToolBarManager.createControl(parent):
#Override
protected Control createControl(Composite parent)
{
ToolBarManager manager = new ToolBarManager(SWT.FLAT | SWT.HORIZONTAL);
LabelContributionItem labelItem = new LabelContributionItem("myLabelId");
manager.add(labelItem);
TextContributionItem textItem = new TextContributionItem("myTextId");
manager.add(textItem);
ToolBar toolbar = manager.createControl(parent);
return toolbar;
}
However, the label is not positioned correctly:
Use a GridLayout as the control to return (code adapted from this answer):
#Override
protected Control createControl(Composite parent)
{
Composite composite = new Composite(parent, SWT.SINGLE);
GridLayout compositeLayout = new GridLayout(2, false);
compositeLayout.marginTop = -1;
compositeLayout.marginBottom = 0;
compositeLayout.marginLeft = 5;
compositeLayout.marginWidth = 0;
composite.setLayout(compositeLayout);
Label myLabel = new Label(composite, SWT.BORDER | SWT.SINGLE);
myLabel.setText("myLabel");
Text myText = new Text(composite, SWT.BORDER | SWT.SINGLE);
myText.setText("myText");
return composite;
}
The result is an incorrectly sized and aligned text box, plus a border around the label (rightmost text box added for comparison):
I also tried some other combinations and layouts, but cannot get this to work properly. Furthermore, I'd like to add a ControlDecoration to the text box, like this:
For the ControlDecoration's mouseover text to work properly, there needs to be margin space to the left of the text box (source):
Clients using ControlDecoration should typically ensure that enough margin space is reserved for a decoration
Adding this space has also proved troublesome, except when using the GridLayout's marginLeft parameter (but GridLayout gave the alignment problems described above).
I had the same problem with labels. Use CLabel if you want to add text to the toolbar.
My advice would be to first create a standalone Composite that contains the desired controls and decorations independantly of the workbench contribution.
There you can test the layout in a simple Shell until it looks like how it should.
As far as I understood your question, this is how the controls should be layed out:
Composite composite = new Composite( shell, SWT.NONE );
composite.setLayout( new GridLayout( 2, false ) );
composite.setBackground( composite.getDisplay().getSystemColor( SWT.COLOR_GREEN ) );
Label label = new Label( composite, SWT.NONE );
label.setText( "MyLabel" );
Text text = new Text( composite, SWT.BORDER );
text.setText( "my text" );
ControlDecoration decoration = new ControlDecoration( text, SWT.TOP | SWT.LEFT );
FieldDecorationRegistry registry = FieldDecorationRegistry.getDefault();
FieldDecoration fieldDecoration = registry.getFieldDecoration( DEC_CONTENT_PROPOSAL );
decoration.setImage( fieldDecoration.getImage() );
label.setLayoutData( new GridData( SWT.BEGINNING, SWT.CENTER, false, false ) );
GridData gridData = new GridData( SWT.BEGINNING, SWT.CENTER, false, false );
gridData.horizontalIndent = 10;
text.setLayoutData( gridData );
Then you can attempt to contribute the controls to the workbench. The extension should be specified as described in this post: Contributed control to the status bar not visible
createControl() should return the above mentioned composite and look like this
protected Control createControl( Composite parent ) {
Composite composite = new Composite( shell, SWT.NONE );
// ... create label, text, decoration as above
return composite;
}
You need to create a tool item of type SWT.SEPARATOR and then attach the label to that. The following code demonstrates this:
public static ToolItem createToolBarLabel(ToolBar toolBar, String text, int width) {
ToolItem labelItem = new ToolItem(toolBar, SWT.SEPARATOR);
CLabel label = new CLabel(toolBar, SWT.NONE);
label.setText(text);
labelItem.setWidth(width);
labelItem.setControl(label);
return labelItem;
}

Java learn using SWT gridlayout and griddata

I understand and can use FormLayout, FormData, FormAttachment but I can't understand how GridLayout, GridData is working. I want to learn using GridLayout and GridData because it's more like a table, it has a structure and doesn't depend on other widgets.
I was working as a web developer (front-end, back-end) and I got lost in Java "Grid" structure. How am I supposed to align, move widgets within a cell (horizontal/vertical Aling, hor./vert. Indent)? Like in HTML/CSS: margin, padding, etc. Ex: move a block from left by 100px. (margin-left: 100px), but in Java?
When I was working as a web developer, I created a page (here in Java it's view), I know how to organize parents and blocks. Can I compare a Composite to a div, like a block element like in HTML/CSS ?
I need to create the following app:
Am I need to use 4 composites?
The following article should shed some light on GridLayout for you:
Understanding Layouts in SWT
To achieve something like the form you have there, you would need something like this:
shell.setLayout(new GridLayout(3, false));
Label title = new Label(shell, SWT.NONE);
title.setText("My first text editor");
GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
data.horizontalSpan = 3;
title.setLayoutData(data);
Label select = new Label(shell, SWT.NONE);
select.setText("Select a file:");
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
select.setLayoutData(data);
Text text = new Text(shell, SWT.BORDER);
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
text.setLayoutData(data);
Button button = new Button(shell, SWT.PUSH);
button.setText("Browse...");
data = new GridData(SWT.FILL, SWT.BEGINNING, true, false);
button.setLayoutData(data);
List result = new List(shell, SWT.BORDER);
data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.horizontalSpan = 3;
result.setLayoutData(data);
The GridData us used to define the behavior of the component within the layout. You can define vertical/horizontal alignment, margins and so on. horizontalSpan is used to tell the layout how many columns the widget will cover.

Java Group layout, vertical layout issues

I am trying to make a two column, 3 row layout. Something along the lines of:
----------------------
| Username |Textbox| |
| Email |Textbox| |
----------------------
Yet even when I'm quite sure the groups managed correctly, it still ends up on a single row like so:
I have the vertical groups separated just fine
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernameLabel)
.addComponent(emailLabel))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernames)
.addComponent(email))
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(usernameLabel)
.addComponent(usernames))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(emailLabel)
.addComponent(email))
);
Any ideas?
You need to set the layout for the container - see mark [1] in the third line below.
To me it looks like you missed that and the container uses FlowLayout.
JFrame frame = new JFrame("GroupLayout Test");
GroupLayout gl_contentPanel = new GroupLayout(frame.getContentPane());
frame.setLayout(gl_contentPanel); // [1]
JLabel usernameLabel = new JLabel("User name");
JLabel emailLabel = new JLabel("Email");
JTextField usernames = new JTextField("usernames");
JTextField email = new JTextField("email");
// your snippet
gl_contentPanel.setHorizontalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernameLabel)
.addComponent(emailLabel))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(usernames)
.addComponent(email))
);
gl_contentPanel.setVerticalGroup(
gl_contentPanel.createSequentialGroup()
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(usernameLabel)
.addComponent(usernames))
.addGroup(gl_contentPanel.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(emailLabel)
.addComponent(email))
);
// end of your snippet
frame.pack();
frame.setVisible(true);
For reference, there's a working example of a two column, three row layout here, illustrated below, that may help guide you.

Categories

Resources