JFace Dialog disposes widgets when still in use - java

I have a class that extends jface.dialogs.Dialog. In that dialog is a save button. When the user pressed that button I need to read the values from some swt.widgets.Text fields, but the text fields are disposed already.
What am I doing wrong?
public class MyNewDialog extends Dialog {
private Text txt;
public MyNewDialog(Shell parentShell) {
super(parentShell);
}
#Override
protected Control createDialogArea(Composite parent) {
Composite container = (Composite) super.createDialogArea(parent);
container.setLayout(new GridLayout(2, false));
txt = new Text(container, SWT.BORDER);
txt.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));
return container
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
Button saveButton = createButton(parent, IDialogConstants.OK_ID, "Save", true);
saveButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent p_e) {
String string = txt.getText() //widget is disposed exception
}
}
}

Since you're using IDialogConstants.OK_ID for your button, you can use the okPressed() method. No need to add a specific listener.
#Override
protected void okPressed()
{
value = txt.getText();
super.okPressed();
}
Then create a getter method method to return the value variable:
public String getValue()
{
return value;
}

Related

How to tell a FieldEditorPreferencePage to invalidate/redraw programatically?

I defined a custom FieldEditorPreferencePage. In createFieldEditors method I construct a simple interface with a custom FieldEditor that holds button that loads a file. When a user loads the file the interface region (where the button stands) need to change and show other specific gui. How to invalidate the FieldEditorPreferencePage? I used the layout() method of the parent composite but nothing happens. What am I doing wrong here?
public class MyPage extends FieldEditorPreferencePage {
...
private Composite fieldEditorParent;
private boolean fileLoaded = false;
#Override
protected void createFieldEditors() {
fieldEditorParent = this.getFieldEditorParent();
MyFieldEditor compositeFieldEditor = new MyFieldEditor(fieldEditorParent) {
if(fileLoaded) {
//draw file content
} else {
Button chooseButton = new Button(buttonsComposite, SWT.NONE);
chooseButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
//choose file
fileLoaded = true;
//invalidate page <-- THIS DOES NOT WORK!
fieldEditorParent.layout(true, true);
}
}
}
}
}

SWT Widgets are not displayed in the Wizard when the Dialog is opened

I created a wizard with one page and has two widgets: a list and a button
but when calling the wizard using dialog.open() the wizard opens but the widgets of the page are not displayed. I don't know what is wrong!
Here is the code of the page
public class SelectCriterionPage extends WizardPage {
private Composite container;
ArrayList<String> listItems=new ArrayList<String>();
List variables,selected;
protected SelectCriterionPage() {
super("CriterionSelection","SelectCriterionPage",null);
setTitle("Selection of criterion variables");
}
#Override
public void createControl(Composite parent) {
container = new Composite(parent, SWT.NONE);
variables=new List(container, SWT.MULTI | SWT.BORDER | SWT.V_SCROLL);
//fill the list with variables
for(String item:listItems)
variables.add(item);
variables.addSelectionListener(new SelectionListener() {
#Override
public void widgetSelected(SelectionEvent e) {
}
#Override
public void widgetDefaultSelected(SelectionEvent e) {
// TODO Auto-generated method stub
}
});
Button btn=new Button(container, SWT.PUSH);
btn.setText("<");
GridData gr=new GridData(GridData.FILL,SWT.CENTER);
btn.setLayoutData(gr);
setControl(container);
}
}
i called my wizard
WizardDialog dialog = new WizardDialog(null, new SelectSlicingCriterionWizard());
dialog.open();
here is my wizard:
public class SelectSlicingCriterionWizard extends Wizard{
IWorkbenchPage workbench;
IStructuredSelection selection;
ArrayList<String> listItems;
public SelectSlicingCriterionWizard() {
super();
setNeedsProgressMonitor(true);
}
#Override
public boolean performFinish() {
System.out.println("Finish clicked");
return true;
}
#Override
public boolean performCancel(){
return true;
}
#Override
public void addPages() {
SelectCriterionPage criterionpage=new SelectCriterionPage();
addPage(criterionpage);
}
#Override
public String getWindowTitle() {
return "Select Criterion Variables";
}
public void init(IWorkbench workbench, IStructuredSelection selection)
{
this.workbench=workbench.getActiveWorkbenchWindow().getActivePage();
this.selection=selection;
}
}
You have not set a Layout for your wizard page Composite. Since you seem to be trying to use Grids this should be:
container = new Composite(parent, SWT.NONE);
container.setLayout(new GridLayout());
Your GridData for the button is using the wrong constructor (the two parameter constructor sets the height and width). Use something like:
GridData gr = new GridData(SWT.BEGINNING, SWT.TOP, false, false);
btn.setLayoutData(gr);
You need to set a Layout for your Composite.
For examples on using GridLayout please see - https://www.eclipse.org/swt/snippets/#gridlayout
For an example on using GridLayout in WizardPage, please see - http://git.eclipse.org/c/platform/eclipse.platform.ui.git/tree/examples/org.eclipse.jface.snippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippets/wizard/Snippet047WizardWithLongRunningOperation.java

Retrieve inputs from custom DialogBox class

I have a Navigator class and a custom DialogBox class which is descended from GridPane.
public DialogBox(final JDialog jdialog) {
Label lblKeyName = new Label("Enter New Key");
Label lblKeyType = new Label("Select Key Type");
TextField txtKeyName = new TextField();
ComboBox cboKeyType = new ComboBox();
txtKeyName.getText();
Button btnOk = new Button("OK");
Button btnCancel = new Button("Cancel");
btnOk.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
//TODO: Somehow return the values in the ComboBox and TextField
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
jdialog.setVisible(false);
}
});
txtKeyName.prefWidth(300);
cboKeyType.prefWidth(300);
this.add(lblKeyName, 0, 0);
this.add(lblKeyType, 0, 1);
this.add(txtKeyName, 1, 0);
this.add(cboKeyType, 1, 1);
this.add(btnOk, 0, 2);
this.add(btnCancel, 1, 2);
}
This is the constructor for my DialogBox.
JFXPanel fxPanel = new JFXPanel();
testBox = new DialogBox(jdialog);
fxPanel.setScene(new Scene(testBox));
jdialog.add(fxPanel);
jdialog.setVisible(true);
How can I retrieve the values in the TextField and ComboBox? I can slightly recall a long ago class where the professor mentioned a technique involving the calling class (Navigator in this case) implementing an Interface and then passing itself to the DialogBox class to retrieve values. Unfortunately I have not found anything and cannot remember how it is done.
Assuming that the dialog is modal, basically, once btnOk or btnCancel button is pressed you need to change some kind of state flag which you can interrogate to determine how the dialog was closed...
// This will also handle the use case where the user presses the "x" button...
private boolean wasCancelled = true;
//...
public boolean wasCancelled() {
return wasCancelled;
}
In you action listeners, you need to set the state appropriately.
btnOk.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
wasCancelled = false;
jdialog.setVisible(false);
}
});
btnCancel.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
wasCancelled = true;
jdialog.setVisible(false);
}
});
Now, once the dialog returns, you need to check this flag...
jdialog.add(fxPanel);
jdialog.setVisible(true);
if (!jdialog.wasCancelled()) {
//...
}
You then need to supply "getter" methods to allow a caller to extract the values from the dialog...
public String getKey() {
return txtKeyName.getText();
}
public String getType() {
return cboKeyType.getSelectionModel().getValue();
}
This will mean you will need to create these two fields as instance variables

About dialog for html page in SWT application

I have an about dialog in Swing that uses a JEditorPane to display an html document (setContentType("text/html");).
I want to creat an about dialog in an Eclipse SWT application and display this same html document which will be displayed formatted (hyperlinks, br etc) same as it did in Swing.
How could I do this? What is the appropriate widget? StyledText?
I started with a Browser in a Handler:
#Execute
public void execute(final Shell currentShell){
//load html file in textReader
Browser browser = new Browser(currentShell, SWT.NONE);
browser.setText(textReader.toString());
}
But nothing is displayed. What is the proper way to resolve this?
Update: Tested #Baz change:
#Execute
public void execute(final Shell currentShell){
currentShell.setLayout(new FillLayout());
//load html file in textReader
Browser browser = new Browser(currentShell, SWT.NONE);
browser.setText(textReader.toString());
currentShell.pack();
}
It completelly ruins the application! The html is loaded covering the existing elements
You can always use the good old JFace Dialog with an embedded Browser widget:
public class BrowserDialog extends Dialog {
private String browserString;
protected BrowserDialog(Shell parentShell, String browserString) {
super(parentShell);
this.browserString = browserString;
}
#Override
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea(parent);
GridLayout layout = new GridLayout(1, false);
composite.setLayout(layout);
GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
data.widthHint = 400;
data.heightHint = 400;
composite.setLayoutData(data);
Browser browser = new Browser(composite, SWT.NONE);
browser.setText(browserString);
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
return composite;
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
}
#Override
protected void configureShell(Shell newShell) {
super.configureShell(newShell);
newShell.setText("About");
}
#Override
public void okPressed() {
close();
}
public static void main(String[] args)
{
final Display display = new Display();
Shell shell = new Shell(display);
Color gray = display.getSystemColor(SWT.COLOR_WIDGET_BACKGROUND);
String hex = String.format("#%02x%02x%02x", gray.getRed(), gray.getGreen(), gray.getBlue());
new BrowserDialog(shell, "<body bgcolor='" + hex + "'><h2>TEXT</h2></body>").open();
}
}
Looks like this:
If you want the dialog buttons, just replace the createButtonsForButtonBar(Composite parent) method with this:
#Override
protected void createButtonsForButtonBar(Composite parent) {
createButton(parent, Dialog.OK, "OK", true);
createButton(parent, Dialog.CANCEL, "Cancel", false);
}
Then it will look like this:

Eclipse RCP Dialog: Large Text box won't scroll - instead creates a huge dialog window!

I've been banging away at this for a while now and I can't seem to get anywhere. I've tried all of the examples I can find online and nothing seems to work! I haven't been able to find much on this problem which leads me to think I'm missing something basic. . .
In my Eclipse RCP program I want to display a dialog that will show a list of errors that occurred while loading a data file. I have overridden TitleAreaDialog and simply want to display a scrollable Text containing the list of errors and an OK button.
The problem is that the Text vertical scroll bars don't become active - the Text just grows taller to fit the text. This makes the dialog window height increases until it either fits the Text box or until it reaches the height of the screen - and then it just cuts off the bottom of the Text box.
How do I prevent the Dialog/Text box from growing too large? What am I missing?
Thanks for your help!!
-Christine
...
Here is a simple program showing my Dialog:
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class ScrollableDialogRunner {
public static void main(String[] args) {
System.out.println("starting");
Display display = new Display ();
Shell shell = new Shell (display);
String errors = "one\ntwo\nthree\nfour\nfive\n";
for(int i = 0; i < 5; i++){
errors += errors;
}
ScrollableDialog dialog = new ScrollableDialog(shell, "Errors occurred during load", "The following errors occurred while loaded file 'x.data'", errors);
dialog.open();
}
}
class ScrollableDialog extends TitleAreaDialog {
private String title;
private String text;
private String scrollableText;
public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
super(parentShell);
this.title = title;
this.text = text;
this.scrollableText = scrollableText;
}
#Override
protected Control createDialogArea(Composite parent) {
GridLayout layout = new GridLayout();
layout.numColumns = 1;
parent.setLayout(layout);
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
Text scrollable = new Text(parent, SWT.BORDER | SWT.V_SCROLL);
scrollable.setLayoutData(gridData);
scrollable.setText(scrollableText);
return parent;
}
#Override
public void create() {
super.create();
setTitle(title);
setMessage(text, IMessageProvider.ERROR);
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
Button okButton = createButton(parent, OK, "OK", true);
okButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
#Override
protected boolean isResizable() {
return false;
}
}
Assign a size to the dialog; otherwise, the dialog will layout the children asking them for their "preferred" size (which is infinite for the text widget) and will resize itself accordingly.
[EDIT] This version works. See my comments for details.
class ScrollableDialog extends TitleAreaDialog {
private String title;
private String text;
private String scrollableText;
public ScrollableDialog(Shell parentShell, String title, String text, String scrollableText) {
super(parentShell);
this.title = title;
this.text = text;
this.scrollableText = scrollableText;
}
#Override
protected Control createDialogArea(Composite parent) {
Composite composite = (Composite) super.createDialogArea (parent); // Let the dialog create the parent composite
GridData gridData = new GridData();
gridData.grabExcessHorizontalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessVerticalSpace = true; // Layout vertically, too!
gridData.verticalAlignment = GridData.FILL;
Text scrollable = new Text(composite, SWT.BORDER | SWT.V_SCROLL);
scrollable.setLayoutData(gridData);
scrollable.setText(scrollableText);
return composite;
}
#Override
public void create() {
super.create();
// This is not necessary; the dialog will become bigger as the text grows but at the same time,
// the user will be able to see all (or at least more) of the error message at once
//getShell ().setSize (300, 300);
setTitle(title);
setMessage(text, IMessageProvider.ERROR);
}
#Override
protected void createButtonsForButtonBar(Composite parent) {
Button okButton = createButton(parent, OK, "OK", true);
okButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
close();
}
});
}
#Override
protected boolean isResizable() {
return true; // Allow the user to change the dialog size!
}
}

Categories

Resources