I want to hide the content of html page until its fully loaded. The idea is I want to get the content of html page do some processing and set the content back again. For doing so I am able to retrieve the text and do some processing on it. But the problem is , till the time processing is not complete, I want to hide the original html page and instead wanna show some dummy page.
I want to hover a shell on browser window and remove it when the loading is complete. I tried to hide the browser widget but that is not working as per the expectations.
Here is the snippet for the same.
package browserapp;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.extended.LocationEvent;
import org.eclipse.swt.browser.extended.LocationListener;
import org.eclipse.swt.browser.extended.ProgressEvent;
import org.eclipse.swt.browser.extended.ProgressListener;
import org.eclipse.swt.browser.extended.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class MyBrowser_1 {
/**
* Runs the application
*/
Display display = new Display();
protected boolean done;
public void run() {
final Shell shell = new Shell(display);
shell.setText("Simple Browser");
createContents(shell);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
/**
* Creates the main window's contents
*
* #param shell the main window
*/
private void createContents(final Shell shell) {
shell.setLayout(new FormLayout());
// Create the composite to hold the buttons and text field
Composite controls = new Composite(shell, SWT.NONE);
FormData data = new FormData();
data.top = new FormAttachment(0, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
controls.setLayoutData(data);
// Create the web browser
final Browser browser = new Browser(shell, SWT.NONE);
browser.addLocationListener(new LocationListener() {
#Override
public void changing(LocationEvent event) {
// TODO Auto-generated method stub
//browser.setVisible(false);
Image image = new Image(display,
"C:\\Documents and Settings\\My
Documents\\Pictures\\loadingAnimation.gif");
///shell.setBackgroundImage(image);
}
#Override
public void changed(LocationEvent event) {
// TODO Auto-generated method stub
}
});
browser.addProgressListener(new ProgressListener()
{
public void completed(ProgressEvent event)
{
}
public void changed(ProgressEvent event)
{
int progressWorked=0;
if (event.total == 0)
return;
done = (event.current == event.total);
int percentProgress = event.current * 100 / event.total;
System.out.println("Loading...");
if (done)
{
progressWorked = 0;
System.out.println("Loading completed...");
//browser.setVisible(true);
//maskPage(browser, maskedMap);
} else if (progressWorked == 0)
{
progressWorked = percentProgress;
} else
{
progressWorked = event.current;
}
}
});
data = new FormData();
data.top = new FormAttachment(controls);
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
browser.setLayoutData(data);
// Create the controls and wire them to the browser
controls.setLayout(new GridLayout(7, false));
// Create the back button
Button button = new Button(controls, SWT.PUSH);
button.setText("Back");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.back();
}
});
// Create the forward button
button = new Button(controls, SWT.PUSH);
button.setText("Forward");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.forward();
}
});
// Create the refresh button
button = new Button(controls, SWT.PUSH);
button.setText("Refresh");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.refresh();
}
});
// Create the stop button
button = new Button(controls, SWT.PUSH);
button.setText("Stop");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.stop();
}
});
// Create the address entry field and set focus to it
final Text url = new Text(controls, SWT.BORDER);
url.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
url.setText("https://netbanking.hdfcbank.com/netbanking/");
url.setFocus();
// Create the go button
button = new Button(controls, SWT.PUSH);
button.setText("Go");
button.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
browser.setUrl(url.getText());
}
});
GridData gridData2 = new GridData(SWT.FILL, SWT.FILL, true, false);
Label status = new Label(controls, SWT.BORDER);
status.setLayoutData(gridData2);
// Allow users to hit enter to go to the typed URL
shell.setDefaultButton(button);
}
/**
* The application entry point
*
* #param args the command line arguments
*/
public static void main(String[] args) {
MyBrowser_1 browser=new MyBrowser_1();
browser.run();
}
}
Related
I have a Text inside a Group, which is inside a Composite and that resides inside a ScrolledComposite. All Elements are inside an EditorPart.
ScrolledComposite mySc
|- Composite myComposite
|- Group myGroup
|- Text myText
I can scroll (using the mouse wheel) over all Elements in the EditorPart, but when the cursor is over the Text area, the scrolling stops.
I want to scroll inside the Text only when it has keyboard focus.
Instantiation of the Text myText:
myText = new Text(myGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
Without SWT.V_SCROLL it works, but then I don't have scroll bars and the possibility to scroll in the Text.
I thought I could maybe use forceFocus() on the parent in case the Text has no Focus Control:
myText.addListener(SWT.MouseWheel, new Listener() {
#Override
public void handleEvent(Event event) {
if (!commandText.isFocusControl()) {
System.out.println("no focus");
Control wheelControl = myText.getParent();
Point cursorPos = wheelControl.toControl(event.display.getCursorLocation());
event.x = cursorPos.x;
event.y = cursorPos.y;
event.widget = wheelControl;
wheelControl.forceFocus();
wheelControl.notifyListeners(SWT.MouseWheel, event);
} else {
System.out.println("Focus control");
}
}
});
But it doesn't work. No change at all. It only prints "Focus control" and "no focus" correctly.
EDIT:
Here is a minimal working example:
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
public class MyEditor extends EditorPart {
private Text myText;
private boolean dirty = false;
public MyEditor() {
super();
}
#Override
public void init(IEditorSite site, IEditorInput input) {
setSite(site);
setInput(input);
}
#Override
public void doSave(IProgressMonitor monitor) {
return;
}
#Override
public void doSaveAs() {
return;
}
#Override
public boolean isDirty() {
return dirty;
}
#Override
public boolean isSaveAsAllowed() {
return false;
}
#Override
public void createPartControl(Composite parent) {
parent.setLayout(new FillLayout());
ScrolledComposite mySc = new ScrolledComposite(parent, SWT.V_SCROLL);
Composite myComposite = new Composite(mySc, SWT.BORDER);
myComposite.setLayout(new GridLayout(1, false));
// Set the child as the scrolled content of the ScrolledComposite
mySc.setContent(myComposite);
// Expand both horizontally and vertically
mySc.setExpandHorizontal(true);
mySc.setExpandVertical(true);
Group myGroup = new Group(myComposite, SWT.NONE);
myGroup.setText("Hello or something");
myGroup.setLayout(new GridLayout(1, false));
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
gd.verticalIndent = 10;
myGroup.setLayoutData(gd);
Label aLabel = new Label(myGroup, SWT.NONE);
aLabel.setText("You can write here: ");
myText = new Text(myGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
myText.setText("Some Default Text");
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.heightHint = 300;
gd.horizontalIndent = 10;
myText.setLayoutData(gd);
myText.addListener(SWT.MouseWheel, new Listener() {
#Override
public void handleEvent(Event event) {
if (!myText.isFocusControl() ) {
System.out.println("no focus");
Control wheelControl = myText.getParent();
Point cursorPos = wheelControl.toControl(event.display.getCursorLocation());
event.x = cursorPos.x;
event.y = cursorPos.y;
event.widget = wheelControl;
wheelControl.forceFocus();
wheelControl.notifyListeners(SWT.MouseWheel, event);
myText.setCapture(false);
} else {
System.out.println("Focus control");
myText.setCapture(true);
}
}
});
mySc.setMinSize(myComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
}
#Override
public void propertyChange(PropertyChangeEvent evt) {
// TODO Auto-generated method stub
}
#Override
public void setFocus() {
// TODO Auto-generated method stub
}
}
The solution I found was to disable the vertical scrollbar. This also disables scrolling by mouse wheel. Also use SWT's addMouseWheelListener() and mouseScrolled() methods instead of addListener(). Than just scroll the ScrolledComposite by using its getOrigin() method.
myText.addMouseWheelListener(new MouseWheelListener() {
#Override
public void mouseScrolled(MouseEvent e) {
if (!myText.isFocusControl() ) {
myText.getVerticalBar().setEnabled(false);
if (e.count == 3) {
mySc.setOrigin(sc.getOrigin().x, mySc.getOrigin().y - 30);
} else if (e.count == -3) {
mySc.setOrigin(sc.getOrigin().x, mySc.getOrigin().y + 30);
}
} else {
myText.getVerticalBar().setEnabled(true);
}
}
});
count always returns 3 or -3, depending on the scroll direction. The value of 30 for scrolling up/down is good for me, might be more or less for other purposes. I didn't check the behavior on a Windows machine yet.
I just started to learn Java. I have already looked Swing and at the moment I'm trying to do something with SWT.
But I have the next problem. Key Listener that I added for Text field is working, but inside this listener I can't change for example my label.
I have seen a few demos they worked, but I don't see any differences.
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class FirstClass {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setText("First Application");
GridLayout layout = new GridLayout(2, false);
shell.setLayout(layout);
Text word = new Text(shell,SWT.SINGLE | SWT.BORDER | SWT.FILL);
GridData wordGrDt = new GridData();
wordGrDt.heightHint = 130;
wordGrDt.minimumHeight = 130;
wordGrDt.horizontalAlignment = SWT.FILL;
wordGrDt.verticalAlignment = SWT.FILL;
wordGrDt.horizontalSpan = 2;
word.setLayoutData(wordGrDt);
GridData statusGrDt = new GridData();
statusGrDt.grabExcessHorizontalSpace = true;
statusGrDt.horizontalSpan = 1;
statusGrDt.horizontalAlignment = SWT.LEFT_TO_RIGHT;
Label status = new Label(shell, SWT.PUSH);
status.setEnabled(true);
status.setText("");
status.setLayoutData(statusGrDt);
GridData checkGrDt = new GridData();
checkGrDt.widthHint = 150;
checkGrDt.horizontalSpan = 1;
checkGrDt.horizontalAlignment = SWT.RIGHT_TO_LEFT;
Button check = new Button(shell, SWT.PUSH);
check.setText("Check");
check.setLayoutData(checkGrDt);
word.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
if (e.keyCode == SWT.CR) {
System.out.println("worked!!!");
status.setText("ababahalamaha");
}
}
});
shell.setMinimumSize(400, 300);
shell.open();
shell.pack();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}
Add a layout() call to the parent of the Label:
word.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
if (e.keyCode == SWT.CR) {
System.out.println("worked!!!");
status.setText("ababahalamaha");
status.getParent().layout();
}
}
});
The label originally has a width of 0, as it doesn't contain any text. When you add content, the parent has to know to re-layout its children.
As a note:
Please check which style you use with which widget. A Label does not know what to do with the style SWT.PUSH for example.
I'm just starting with Java basic and having this issue with SWT FileDialog. Eclipse always returns this error: The constructor FileDialog(Shell, int) is undefined
Below is the full code, could you please help to advise?
public class FormObject2 {
protected Shell shell;
private Text txtComboBoxItem;
private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
private Text taOne;
/**
* Launch the application.
* #param args
*/
public static void main(String[] args) {
try {
FormObject2 window = new FormObject2();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setSize(481, 364);
shell.setText("SWT Application");
Menu menu = new Menu(shell, SWT.BAR);
shell.setMenuBar(menu);
MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
mntmFile.setText("File");
Menu menu_1 = new Menu(mntmFile);
mntmFile.setMenu(menu_1);
MenuItem mntmOpen = new MenuItem(menu_1, SWT.NONE);
mntmOpen.setAccelerator(1);
mntmOpen.setText("Open");
mntmOpen.setAccelerator(SWT.MOD1+ 'O');
MenuItem mntmSave = new MenuItem(menu_1, SWT.NONE);
mntmSave.setAccelerator(1);
mntmSave.setText("Save");
MenuItem mntmEdit = new MenuItem(menu, SWT.CASCADE);
mntmEdit.setText("Edit");
Menu menu_2 = new Menu(mntmEdit);
mntmEdit.setMenu(menu_2);
MenuItem mntmExit = new MenuItem(menu_2, SWT.NONE);
mntmExit.setText("Exit");
mntmOpen.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
FileDialog dialog = new FileDialog(shell, SWT.MULTI);
dialog.setFilterPath("C:\\Logs");
dialog.setFilterExtensions(new String[]{"*.txt", "*.pdf", "*.*"});
dialog.setFilterNames(new String[]{ "Rich Text Format", "HTML Document", "Any"});
dialog.open();
}
});
}
Your are probably importing the wrong FileDialog class - you need to be importing org.eclipse.swt.widgets.FileDialog not java.awt.FileDialog
In my project i have a shell, in the shell there are 3 buttons, i want that a click on each button will open a shell ,but i want that if a shell is already open due to a click on a button then that shell will be closed and a new shell will be opened.
(I dont want 2 shell from clicking buttons to be open at the same time)
But i have no idea how to do this.
In this class the opening of the shells should be.
public class ClickLabel implements MouseListener
{
Shell shell;
int p;
public ClickLabel(int p)
{
shell = new Shell();
this.p = p;
}
#Override
public void mouseDoubleClick(MouseEvent e) {}
#Override
public void mouseDown(MouseEvent e) {}
#Override
public void mouseUp(MouseEvent e) {
shell.open();
}
}
Can anyone help me?
Here is simple example with buttons and one active Shell, examine that:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class Example{
public static void main(String[] args) {
new Example();
}
private Shell openedShell;
public Example() {
final Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
SelectionAdapter adapter = new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
if(openedShell != null){
openedShell.dispose();
}
openedShell = new Shell(display);
openedShell.setSize(200,200);
openedShell.setText(((Button)e.getSource()).getText());
openedShell.open();
}
};
for(int i =1;i<4;i++){
Button b = new Button(shell, SWT.PUSH);
b.setText("shell "+i);
b.addSelectionListener(adapter);
b.pack();
}
shell.pack();
shell.open ();
while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}
display.dispose ();
}
}
I have a class like,
Class A extends B {
String test = "";
String first = "";
public void testMethod() {
new Thread() {
public void run() {
testThreadMethod();
}
}.start();
}
public void testThreadMethod() {
System.out.println(first + " " + test);
}
}
The above class compiles fine. But in run time, the error is thrown in system.out.println() saying "invalid thread access".
Is there any wrong in the code. Accessing instance variables in multithread not allowed? Is there any way to access the instance variable inside the thread?
Thanks in advance.
EDITED NEW: To Reproduce the problem
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;
public class SWTView extends ViewPart{
public static Display display;
public static Shell shell;
static Text sampleText;
static String testField1 = "";
static String firstField2 = "";
public static void main(String[] args) {
display = new Display();
shell = new Shell(display);
// Create a new Gridlayout with 2 columns
// where the 2 column do not have the
// same size
GridLayout layout = new GridLayout(2, false);
// set the layout of the shell
shell.setLayout(layout);
// Create a label and a button
sampleText = new Text(shell, SWT.NONE);
Label label = new Label(shell, SWT.NONE);
label.setText("A label");
Button button = new Button(shell, SWT.PUSH);
button.setText("Press Me");
// Create a new label that will spam two columns
label = new Label(shell, SWT.BORDER);
label.setText("This is a label");
// Create new layout data
GridData data = new GridData(GridData.FILL,
GridData.BEGINNING, true, false, 2, 1);
label.setLayoutData(data);
// Create a new label which is used as a separator
label = new Label(shell, SWT.SEPARATOR | SWT.HORIZONTAL);
// Create new layout data
data = new GridData(GridData.FILL, GridData.BEGINNING, true,
false, 2, 1);
data.horizontalSpan=2;
label.setLayoutData(data);
// Create a right aligned button
Button b = new Button(shell, SWT.PUSH);
b.setText("New Button");
b.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
new Thread() {
public void run() {
printInstanceVariables();
}
}.start();
//showProgressBar() ---> // This is been implemented in another file which will shoe progress bar
}
});
data = new GridData(GridData.END, GridData.BEGINNING, false,
false, 2, 1);
b.setLayoutData(data);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
public static void printInstanceVariables() {
System.out.println("Text entered is :: " +sampleText.getText());
System.out.println("test Field 1 is :: " + testField1);
System.out.println("first Field 2 is :: " + firstField2);
}
#Override
public void createPartControl(Composite arg0) {
// TODO Auto-generated method stub
}
#Override
public void setFocus() {
shell.setFocus();
}
}
The above code will throw invalid thread acesses ecpection # printInstanceVariables() first system.out.println()
Answer: Got it.. It is because of accessing the component Text inside the thread in printInstanceVariables(). When i pass this component as paramter, everything works fine. Thanks for all you answers.
This program compiles and executes as expected (it prints a space and a new line). Your problem is somewhere else:
public class Test {
public static void main(String[] args) throws Exception {
A a = new A();
a.testMethod();
}
static class A {
String test = "";
String first = "";
public void testMethod() {
new Thread() {
public void run() {
testThreadMethod();
}
}.start();
}
public void testThreadMethod() {
System.out.println(first + " " + test);
}
}
}
I tried your code without extending class B and its working fine. What does your class B contain? Is there really any need to extend class B?
anonymous inner class is only allowed access FINAL member variable.
this is JVM specification.
i doubt why there is no warning when your code be compiled.
in fact i could get nothing out put when run these code. is there something wrong?
i rewrite the codes, test it and find
it can't access a member variable when the thread in anonymous inner class.
please see the output from eclipse.