How to change the size of the dialog? - java

I created a dialog class:
myDailog extends Dialog
public myDailog (Shell parentShell, String tatgetEntity) {
super(parentShell)
}
#Override
protected Control createDialogArea(final Composite parent) {
final Composite body = (Composite) super.createDialogArea(parent);
...//some logic to create tableViewwer
}
The problem that I can't change the size of the dialog (stretch the windows).
Do I need to use different dialog?

Override the isResizable method of org.eclipse.jface.dialogs.Dialog:
#Override
protected boolean isResizable()
{
return true;
}

You may need to use this public method to enable the dialog to be resizable:
public void setResizable(boolean resizable)
ps I'm assuming you are using this java.awt.Dialog class

Related

Switching contentpane in real time

I try to make java small project with intro and after full video or keypressed skip to menu. I made enums for state of "game". How i can switch JPanels and stop everything that old one is doing, because intro is also playing music in Thread.
contentPane.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
System.out.println("Clicked");
State = GameState.MainMenu;
}
});
if (State == GameState.Intro) {
contentPane.removeAll();
contentPane.add(intro);
contentPane.revalidate();
contentPane.repaint();
intro.music.interrupt();
System.out.println(State);
} else if (State == GameState.MainMenu) {
contentPane.removeAll();
contentPane.add(menu);
contentPane.revalidate();
contentPane.repaint();
System.out.println(State);
}
}
The first thing I would recommend is, making use of a CardLayout - See How to use CardLayout for more details.
It will make switching between the views much simpler.
Next, I would recommend devising some kind of "navigation" system which is independent of the views themselves. The idea is, any one view really should know or care which view is next (or previous) and should only be making "simple" requests to the navigation system, like "show next view". It's up to the navigation system to decide exactly what that means (and how to drive it).
This allows you more control over defining and managing the overall navigation process.
I would then couple that with a optional "life cycle" concept, which would allow the navigation system to notify those implementations when the navigation state was changing.
"How" you actually do this, will depend on a lot on your overall design and intentions, but it might look something like...
LifeCycle
public interface LifeCycle {
public void willShow();
public void didShow();
public void willHide();
public void didHide();
}
NavigationController
public interface NavigationController {
public void next();
}
public interface View {
public String getName();
public JComponent getView();
}
Default implementations...
public class DefaultView implements View {
private String name;
private JComponent view;
public DefaultView(String name, JComponent view) {
this.name = name;
this.view = view;
}
#Override
public String getName() {
return name;
}
#Override
public JComponent getView() {
return view;
}
}
public class DefaultNavigationController implements NavigationController {
private List<View> views;
private Container parent;
private CardLayout layout;
private View currentView;
public DefaultNavigationController(Container parent, CardLayout layout) {
this.parent = parent;
this.layout = layout;
}
protected Container getParent() {
return parent;
}
protected CardLayout getLayout() {
return layout;
}
public void add(String name, JComponent comp) {
getParent().add(comp, name);
views.add(new DefaultView(name, comp));
}
protected List<View> getViews() {
return views;
}
#Override
public void next() {
List<View> views = getViews();
if (views.isEmpty()) {
return;
}
int index = (currentView == null ? -1 : views.indexOf(currentView)) + 1;
if (index >= views.size()) {
// This is the last view
return;
}
View previousView = currentView;
View nextView = views.get(index);
willHide(previousView);
willShow(nextView);
getLayout().show(getParent(), nextView.getName());
didHide(previousView);
didShow(nextView);
currentView = nextView;
}
protected void willHide(View view) {
if (view != null && view.getView() instanceof LifeCycle) {
LifeCycle cycle = (LifeCycle)view.getView();
cycle.willHide();
}
}
protected void willShow(View view) {
if (view != null && view.getView() instanceof LifeCycle) {
LifeCycle cycle = (LifeCycle)view.getView();
cycle.willShow();
}
}
protected void didHide(View view) {
if (view != null && view.getView() instanceof LifeCycle) {
LifeCycle cycle = (LifeCycle)view.getView();
cycle.didHide();
}
}
protected void didShow(View view) {
if (view != null && view.getView() instanceof LifeCycle) {
LifeCycle cycle = (LifeCycle)view.getView();
cycle.didShow();
}
}
}
As you can see, I like working to interfaces, this hides the implementation details from other parts of the system and provides me with a better point of customisation. For example, components don't care "how" the NavigationController is implemented, only that it follows a specific and definable work flow.
Okay, but how might this work? Let's start with a basic implementation of a component that support LifeCycle, it might look something like...
public class IntroPane extends JPanel implements LifeCycle {
private NavigationController navigationController;
protected NavigationController getNavigationController() {
return navigationController;
}
protected void next() {
getNavigationController().next();
}
#Override
public void willShow() {
// Prepare any resources
// Lazy load resources
// Do other preparation work which doesn't need to be done in the
// constructor or which needs to be recreated because of actions
// in will/didHide
}
#Override
public void didShow() {
// Start animation loops and other "UI" related stuff
}
#Override
public void willHide() {
// Pause the animation loop and other "UI" related stuff
}
#Override
public void didHide() {
// Dispose of system intensive resources which can be recreated
// in willShow
}
}
And you might set it up using something like...
CardLayout cardLayout = new CardLayout();
JPanel contentPane = new JPanel(cardLayout);
DefaultNavigationController navigationController = new DefaultNavigationController(contentPane, cardLayout);
navigationController.add("intro", new IntroPane());
navigationController.add("menu", new MenuPane());
navigationController.add("game", new GamePane());
"But wait" you say, "this is a linear navigation, I need something more dynamic!"
Far point. In that case I would create, yes, another interface!! This would extend from the (linear) NavigationController and provide a means to "show" arbitrary named views.
Why do it this way? Because not all the views need the power to decide which view should be shown, some just want to show the next (or previous) view, for example, MenuPane might be the only view which actually needs a non-linear navigation controller, all the others just need to be able to move back or forward
But how do I stop a Thread
That's a complicated question, which isn't always easily answered. The "basic" answer is, you need to define a controlling mechanism which can work together with the "interruptable" support of the Thread and exit the thread context.
Maybe How to Kill a Java Thread would be a starting point

Jface Dialog, How to retrieve correctly what button pressed the user?

I'm having troubles with a custom Dialog in Eclipse.
in the first place, I created a Class that extend Dialog.
public class ModificarGrupoBCDialog extends Dialog {
private static final int CANCELAR = 999;
private static final int MODIFICAR = 1;
...
somewhere I create the buttons...
protected void createButtonsForButtonBar(Composite parent) {
this.createButton(parent, MODIFICAR, "Modificar", true);
this.getButton(MODIFICAR).setEnabled(puedeAltaGrupoBC());
this.bt_ok = this.getButton(MODIFICAR);
this.createButton(parent, CANCELAR, "Cancelar", false);
Display display = window.getShell().getDisplay();
Image image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/modificar.png"));
this.getButton(MODIFICAR).setImage(image);
image = new Image(display, ModificarGrupoBCDialog.class.getResourceAsStream("/icons/cancelar.png"));
this.getButton(CANCELAR).setImage(image);
}
and when the user clicks...
protected void buttonPressed(int buttonId) {
switch (buttonId) {
case MODIFICAR:
// Some Code, for Change Button
break;
case CANCELAR:
setReturnCode(CANCELAR);
close();
break;
}
Finally, this is how I open and get the returnCode, in the caller object.
...
ModificarGrupoBCDialog modificarGrupoBC = new ModificarGrupoBCDialog(window.getShell(), window, gr_bc);
if (modificarGrupoBC.getReturnCode() == Window.OK) {
//... Some code on OK
} else {
//another code when cancel pressed.
}
;
as you can see, after trying a while, I have to write setReturnCode() in CANCELAR switch block, is that OK ?
I spect that Dialog class automatically asign the correct return code.
May be someone could point me to a good sample.
I'm reading Vogela's blog, and may be the solution is to override okPressed() method ?
Best Regards.
The standard dialog sets the return code in two places:
protected void okPressed() {
setReturnCode(OK);
close();
}
protected void cancelPressed() {
setReturnCode(CANCEL);
close();
}
so your code doing:
setReturnCode(xxxx);
close();
should be fine as long as the button id you are using does not match the Cancel or OK button ids.
You could also use the approach used by MessageDialog which simply does this:
protected void buttonPressed(int buttonId) {
setReturnCode(buttonId);
close();
}

How to update content in a vaadin tab on selection change?

I have a Vaadin tab menu, each tab contains of course different content. Now, if the content is an own class with dynamically generated content, how can I force the content to be updated if a tab is selected?
My content class implements View, thus it as a enter(ViewChangeEvent event) that would be normally triggered if I use the Navigator to change the view.
However, then I use a tab to change the content, there is no ViewChangeEvent fired.
How can I though trigger the enter() method of the specific view content of the tab?
#VaadinComponent
#UIScope
public class MyView extends CssLayout implements View {
private Label label;
public MyView() {
label = new Label("empty");
}
#Override
public void enter(ViewChangeEvent event) {
label.setValue("entered");
}
}
#VaadinComponent
#UIScope
public class MyMenu extends CssLayout {
private TabSheet tabs;
public MyMenu() {
tabs = new TabSheet();
addComponent(tabs);
}
#Autowired
private MyView myview;
#PostConstruct
public void init() {
tabs.addComponent(myview);
//some more tabs
}
}
You can use a Shortcut Listener like that
component.addShortcutListener(new ShortcutListener("caption", KeyCode.ENTER, null) {
private static final long serialVersionUID = 1L;
#Override
public void handleAction(Object sender, Object target) {
label.setValue("entered");
}
});

Is it possible to change Finish button text to Done in Wizard?

I have created a custom wizard with some pages in eclipse plugin. The pages are created by extending the WizardPage. The wizard has Next back Finish and cancel button, and everything works fine.
Now, I want to change the name/text of Finish button to Done. Is it possible to do this in eclipse? Or will I need to provide all the buttons by myself, even this would be fine.
This should do:
#Override
protected void createButtonsForButtonBar(Composite parent) {
super.createButtonsForButtonBar(parent);
Button finish = getButton(IDialogConstants.FINISH_ID)
finish.setText("Done");
setButtonLayoutData(finish);
}
Here is a related question.
So this is how I was using the wizard:
MyCustomWizard wizard = new MyCustomWizard ("title");
WizardDialog wizardDialog = new WizardDialog(Display.getDefault().getActiveShell(), wizard);
wizardDialog.open();
Now I created a new class, MyCustomDialog extending the WizardDialog:
public class MyCustomDialog extends WizardDialog {
public MyCustomDialog(Shell parentShell, IWizard newWizard) {
super(parentShell, newWizard);
}
#Override
public void createButtonsForButtonBar(Composite parent){
super.createButtonsForButtonBar(parent);
Button finishButton = getButton(IDialogConstants.FINISH_ID);
finishButton.setText(windowTitle);
}
}
And now I changed the code where I create wizard and wizard dialog as:
MyCustomWizard wizard = new MyCustomWizard ("title");
MyCustomDialog wizardDialog = new MyCustomDialog(Display.getDefault().getActiveShell(), wizard);
wizardDialog.open();
Hope it is useful to someone! :)
Thanks for the help #greg-449 and #Baz
There is one more way to change the label of any button. You can override createButton of WizardDialog class as :
#Override
protected Button createButton(Composite parent, int id, String label,
boolean defaultButton) {
if (id == IDialogConstants.FINISH_ID) {
return super.createButton(parent, id,
"Done", defaultButton);
}
return super.createButton(parent, id, label, defaultButton);
}

Java SWT: widgetSelected vs widgetDefaultSelected

For example I want to execute something when user clicks on a button. Which do I use? The documentation didn't appear to make it very clear
UPDATE
A quick test shows that Widget Selected is triggered but not Default Selected.
In TasksView.main()
TasksView view = new TasksView(shell, SWT.None);
TasksController controller = new TasksController(view);
In TasksController
public class TasksController extends ControllerAbstract {
protected TasksView view;
public TasksController(TasksView view) {
this.view = view;
view.addTaskListener(new AddTaskListener());
}
protected class AddTaskListener implements SelectionListener {
#Override
public void widgetDefaultSelected(SelectionEvent arg0) {
System.out.println("Default Selected");
}
#Override
public void widgetSelected(SelectionEvent arg0) {
System.out.println("Widget Selected");
}
}
}
btw, Did I do MVC correctly?
Use widgetSelected. In fact, all the better is to simply extend SelectionAdapter and only override the widgetSelected method and completely ignore widgetDefaultSelected.
SelectionListener.widgetDefaultSelected(e) has a toolkit dependent behavior. I usually just invoke SelectionListener.widgetSelected(...). (Note that this is not the default in SelectionAdapter.widgetDefaultSelected(e) - you will have to do this yourself.

Categories

Resources