I'm trying to display a label in a 2-columned GridLayout. I can't seem to make it work to display both text and the horizontal line underneath:
public void foo(){
popupShell = new Shell(Display.getDefault(), SWT.NO_TRIM | SWT.ON_TOP | SWT.MODELESS);
//popupShell.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
popupShell.setLayout(createNoMarginLayout(2, false));
Label history = new Label(popupShell, SWT.SEPARATOR | SWT.SHADOW_OUT | SWT.HORIZONTAL);
history.setText("History");
history.setVisible(true);
Label fill = new Label(popupShell, SWT.NONE);
fill.setSize(0,30);
}
public static GridLayout createNoMarginLayout(int numColumns, boolean makeColumnsEqualWidth) {
GridLayout layout = new GridLayout(numColumns, makeColumnsEqualWidth);
layout.verticalSpacing = 0;
layout.horizontalSpacing = 0;
layout.marginTop = 0;
layout.marginBottom = 0;
layout.marginLeft = 0;
layout.marginRight = 0;
layout.marginWidth = 0;
layout.marginHeight = 0;
return layout;
}
What I'm getting is just the line with no text.
What am I doing wrong?
A Label with the SWT.SEPARATOR style does not display any text value. You must use a separate control to display the text.
From the Label source, showing that setText is completely ignored for SWT.SEPARATOR:
public void setText (String string) {
checkWidget();
if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
if ((style & SWT.SEPARATOR) != 0) return;
...
Related
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();
}
I have a custom tooltip class which extends ColumnViewerToolTipSupport. The problem is the tooltip boundary expands to fit the dynamic content. I need to implement a fixed sized toolTip which do not expand based on content size.
How can this be done ??
private static class MyToolTip extends ColumnViewerToolTipSupport {
public static final void enableFor(ColumnViewer viewer, int style) {
new MyToolTip(viewer, style, false);
}
private MyToolTip(ColumnViewer viewer, int style, boolean manualActivation) {
super(viewer, style, manualActivation);
setHideOnMouseDown(false);
}
#Override
protected Composite createViewerToolTipContentArea(Event event, ViewerCell cell, Composite parent) {
String text = getText(event);
if (text == null || text.isEmpty()) {
return super.createViewerToolTipContentArea(event, cell, parent);
}
ScrolledComposite scrolledComposite = new ScrolledComposite(parent,SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
final Composite comp = new Composite(scrolledComposite, SWT.NONE);
comp.getShell().setBackgroundMode(SWT.INHERIT_FORCE);
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
gridLayout.marginHeight = 0;
gridLayout.marginWidth = 0;
comp.setLayout(gridLayout);
comp.setLayoutData(new GridData(GridData.FILL_BOTH));
scrolledComposite.setContent(comp);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);
scrolledComposite.setMinSize(comp.computeSize(150, 100));
StyledText styledText = new StyledText(comp, SWT.BORDER);
styledText.setText(text);
StyleRange style = new StyleRange();
style.start = 0;
style.length = text.indexOf(":");
style.fontStyle = SWT.BOLD;
styledText.setStyleRange(style);
String lineSep = System.getProperty("line.separator");
Matcher matcher = Pattern.compile("(" + lineSep + ")" + ".*?:").matcher(text);
while (matcher.find()) {
style = new StyleRange();
style.start = matcher.start();
style.length = matcher.end() - matcher.start();
style.fontStyle = SWT.BOLD;
}
return scrolledComposite;
}
}
ScrolledComposite scrollFormItemComposite = new ScrolledComposite(c, SWT.V_SCROLL | SWT.BORDER);
GridData formItemCompositeGridData = new GridData(SWT.FILL, SWT.FILL, true, true);
formItemCompositeGridData.horizontalSpan = 4;
scrollFormItemComposite.setLayoutData(formItemCompositeGridData);
GridLayout formItemLayout = new GridLayout(1, true);
formItemLayout.marginHeight = 0;
formItemLayout.marginWidth = 0;
formItemLayout.verticalSpacing = 0;
formItemLayout.horizontalSpacing = 0;
scrollFormItemComposite.setLayout(formItemLayout);
Composite formItemComposite = new Composite(scrollFormItemComposite, SWT.RESIZE);
formItemComposite.setLayout(new FillLayout(SWT.VERTICAL|SWT.HORIZONTAL));
FormProvider formProvider = new FormProvider();
formProvider.createForms(formItemComposite);
scrollFormItemComposite.setContent(formItemComposite);
scrollFormItemComposite.setExpandHorizontal(true);
scrollFormItemComposite.setExpandVertical(true);
scrollFormItemComposite.setMinSize(formItemComposite.computeSize(300,SWT.DEFAULT));
The previous lines of code give me the following output. I want the contents to shrink as the form becomes smaller. I dont want the horizontal scroll bar to appear. How do I prevent the text box from being hidden?
I have a table with 4 columns. I try to integrate a ToolTip for all cells of the third column.
With my Code the ToolTip only appears for the cells of the first column.
This is my Code:
protected void checkAction() throws Exception {
System.out.println("Start Test");
//Erstellen einer neuen Shell
final Shell shell = new Shell();
shell.setSize(280, 300);
shell.setText("Testtabelle");
//Erstellen einer neuen Tabelle
final Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
//Einlesen der Überschriften und Vergabe der Namen
String[] titles = {"Element", "Stage", "Type", "Generate-User", "Change-User" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(titles[i]);
}
// Inhalte hinzufügen
final int count = 4;
for (int i = 0; i < count; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "Test "+i);
item.setText(1, ""+(i+1));
item.setText(2, "Testtype");
item.setText(3, "562910");
item.setText(4, "423424");
}
// Disable native tooltip
table.setToolTipText ("");
// Implement a "fake" tooltip
final Listener labelListener = new Listener () {
public void handleEvent (Event event) {
Label label = (Label)event.widget;
Shell shell = label.getShell ();
switch (event.type) {
case SWT.MouseDown:
Event e = new Event ();
e.item = (TableItem) label.getData ("_TABLEITEM");
// Assuming table is single select, set the selection as if
// the mouse down event went through to the table
table.setSelection (new TableItem [] {(TableItem) e.item});
table.notifyListeners (SWT.Selection, e);
shell.dispose ();
table.setFocus();
break;
case SWT.MouseExit:
shell.dispose ();
break;
}
}
};
Listener tableListener = new Listener () {
Shell tip = null;
Label label = null;
public void handleEvent (Event event) {
switch (event.type) {
case SWT.Dispose:
case SWT.KeyDown:
case SWT.MouseMove: {
if (tip == null) break;
tip.dispose ();
tip = null;
label = null;
break;
}
case SWT.MouseHover: {
TableItem item = table.getItem (new Point (event.x, event.y));
if (item != null) {
if (tip != null && !tip.isDisposed ()) tip.dispose ();
tip = new Shell (shell, SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL);
FillLayout layout = new FillLayout ();
layout.marginWidth = 2;
tip.setLayout (layout);
label = new Label (tip, SWT.NONE);
label.setData ("_TABLEITEM", item);
if (item.getText().equals("Test 3")){
label.setText ("Jonas Intfeld");
}
else{
label.setText (item.getText ());
}
label.addListener (SWT.MouseExit, labelListener);
label.addListener (SWT.MouseDown, labelListener);
Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT);
Rectangle rect = item.getBounds (0);
Point pt = table.toDisplay (rect.x, rect.y);
tip.setBounds (pt.x, pt.y, size.x, size.y);
tip.setVisible (true);
}
}
}
}
};
table.addListener (SWT.Dispose, tableListener);
table.addListener (SWT.KeyDown, tableListener);
table.addListener (SWT.MouseMove, tableListener);
table.addListener (SWT.MouseHover, tableListener);
// Tabelle und Shell Packen
for (int i = 0; i < titles.length; i++) {
table.getColumn(i).pack();
}
table.setSize(table.computeSize(SWT.DEFAULT, 200));
shell.pack();
// Shell öffnen
try { shell.open();
} catch (SWTException e) {
System.out.println("Test: "+e);
shell.close();
}
}
Is there any way to activate the Tooltip for the 3. Column ?
At the moment when I move over the cells in the 3. column the Tooltip appears for the 1. column.
Maybe the best option is to search the column by title?
Your problem is this line:
Rectangle rect = item.getBounds(0);
You are asking for the bounds of the first column. Just change the index to the column where you want your tooltip to appear and you'll be fine:
Rectangle rect = item.getBounds(2);
If you need to get the column index from the mouse position, use this code:
Point point = new Point(event.x, event.y);
int column = 0;
for (int i = 0; i < table.getColumnCount(); i++)
{
if (item.getBounds(i).contains(point))
{
column = i;
break;
}
}
Rectangle rect = item.getBounds(column);
Can someone explain me something? How can I create a yellow hint/notification with Java swt?
I want that popup a small notice when I move over a cell in a table. Something like this:
This is my Java-Code:
protected void checkAction() throws Exception {
//Erstellen einer neuen Shell
Shell shell = new Shell();
shell.setSize(280, 300);
shell.setText("Testtabelle");
//Erstellen einer neuen Tabelle
final Table table = new Table(shell, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
table.setLinesVisible(true);
table.setHeaderVisible(true);
//Einlesen der Überschriften und Vergabe der Namen
String[] titles = {"Element", "Stage", "Type", "Generate-User", "Change-User" };
for (int i = 0; i < titles.length; i++) {
TableColumn column = new TableColumn(table, SWT.NONE);
column.setText(titles[i]);
}
// Inhalte hinzufügen
final int count = 4;
for (int i = 0; i < count; i++) {
TableItem item = new TableItem(table, SWT.NONE);
item.setText(0, "Test "+i);
item.setText(1, ""+(i+1));
item.setText(2, "Testtype");
item.setText(3, "562910");
item.setText(4, "423424");
}
// Tabelle und Shell Packen
for (int i = 0; i < titles.length; i++) {
table.getColumn(i).pack();
}
table.setSize(table.computeSize(SWT.DEFAULT, 200));
shell.pack();
//MouseListener
table.addListener(SWT.MouseHover, new Listener() {
public void handleEvent(Event event) {
Rectangle clientArea = table.getClientArea();
Point pt = new Point(event.x, event.y);
int index = table.getTopIndex();
// System.out.println("TopIndex: "+index);
while (index < table.getItemCount()) {
boolean visible = false;
TableItem item = table.getItem(index);
for (int i = 0; i < (table.getItemCount()+1); i++) {
Rectangle rect = item.getBounds(i);
if (rect.contains(pt)) {
String selected = table.getItem(index).getText(i);
if (selected.equals("562910")){
String real = "Jonas";
System.out.println(real);
}
else{
System.out.println("Ausgewählt: "+selected);
}
}
if (!visible && rect.intersects(clientArea)) {
visible = true;
}
}
if (!visible)
return;
index++;
}
}
});
// Shell öffnen
shell.open();
}
Using just SWT use the code in SWT Snippet 125.
If you can use JFace then ColumnViewerToolTipSupport is rather easier.
You should start using jface TableViewer. To create table column, you will use TableViewerColumn.
For each column, add CellLabelProvider which has getToolTipXXX() methods.
TableViewerColumn.setLabelProvider(CellLabelProvider)
here is good article to start with
http://www.vogella.com/tutorials/EclipseJFaceTable/article.html