How to use Libgdx Scene2d Dialog? - java

I created a dialog and added a button actor.
windowStyle = new Window.WindowStyle(boldFont, Color.WHITE, drawable);
dialog = new Dialog("Error", windowStyle);
dialog.getContentTable().add(resetButton);
but the problem is that when I do dialog.show(stage) only the background of the dialog is at the center, and the string "Error" is at the top left corner of the dialog background while the resetButton is also not aligned.

You can do several things here. The Dialog class has at last three tables in it. The "Error" string is added to the titleTable. You could create an empty titled Dialog:
dialog = new Dialog("", windowStyle);
and then add and center your title manually:
dialog.getTitleTable().add(new Label("Error", new Label.LabelStyle(boldFont, Color.WHITE)).center().expand();
Same for the resetButton, but you could use the getContentTable() or getButtonTable() methods.
If you want to resize or reposition your dialog window, I found the only way to do so is to create your own Dialog class, MyDialog extends Dialog and override the getPrefHeight(), getPrefWidth() and setPosition(x, y) methods.

Related

How to close only JDialog and have JFrame still usable

I've a JFrame and a JPanel is added in. When I click o a button in the panel, a JDialog (named choiceDialog) appears. When I click on a particular button on the dialog I just want it to close.
I would want the dialog closed and the frame usable. Is it possible?
I tried to hide the dialog with setVisible(false) but it hid both the dialog and frame. Then I tried to do choiceDialog.dispose() but I lost both the elements again. At that point I found a way to set the Frame again visible but not usable.
Can anyone help me please? I don't really know what to do.
Here's the relevant code:
if (dimField.isEnabled()){
String dimFieldText = dimField.getText();
if (dimFieldText.equals("") || !isNumeric(dimFieldText)){ //if there's an error when filling the options in the JDialog
errorLabel = new JLabel(noDim, SwingConstants.CENTER);
/*other stuff
...
*/
}else{ //if it's all ok: I want the JDialog close but the JFrame to be usable
JFrame topFrame = (JFrame) SwingUtilities.getWindowAncestor(this); //to catch the JFrame istance
choiceDialog.dispose();
topFrame.setVisible(true); //to make the JFrame visible again
//choiceDialog.setVisible(false);
}
You are setting topFrame to be the window ancestor of the button, which is the dialog itself. You need to get the window ancestor of the dialog instead. That assumes that when you created the dialog you specified the main frame as its parent rather than using the no-argument JDialog constructor.

Swing context menu on JavaFX Chart

I am working on an application primarily in swing, though the chart dialog uses JavaFX. (Chart in a GridPane in a JFXPanel to be exact.)
First, Chart does not implement a setContextMenu method to use a JFX ContextMenu. I have seen a workaround by attaching a handler to the right click of the chart, but the menu is not removed from the GUI when it loses focus, rather it persists until the user either 1) triggers it to open again or 2) clicks a MenuItem.
chart.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
if (t.getButton() == MouseButton.SECONDARY) {
contextMenu.show(chart, t.getScreenX(), t.getScreenY());
}
}
});
That said, I would prefer to use a Swing JPopupMenu instead. How can I bind this to my Chart and maintain a natural behavior?
Thank you!
I determined it was not possible to use a JPopupMenu.
The ContextMenu is added to a JLabel:
Label contextMenuLabel = new Label();
grid.add(contextMenuLabel, 0, 0); // Add to the same grid after adding the chart
contextMenuLabel.setPrefSize(width, height);
contextMenuLabel.setMinSize(width, height);
contextMenuLabel.setMaxSize(width, height);
ContextMenu contextMenu = new ContextMenu();
contextMenuLabel.setContextMenu(contextMenu);

adding text box on top of gwt java

I am currently working on a school project where we are creating a GWT web application which uses a GeoChart widget to display information about the servers we have crawled. Simply put, I would wish to create a text box on top of our GeoChart widget which shows an interactive world map that takes up the whole screen right now to input information. I have searched quite extensively but I have been unable to come up with an answer.
Here is the code as follows:
#Override
public void onModuleLoad() {
dataReader = (DataReaderAsync) GWT.create(DataReader.class);
RootLayoutPanel.get().add(getSimpleLayoutPanel());
// Create the API Loader
ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
chartLoader.loadApi(new Runnable() {
#Override
public void run() {
getSimpleLayoutPanel().setWidget(getGeoChart());
drawGeoChart();
}
});
}
As GeoChart is a widget, it is wrapped under(i am not sure if this is the right word) a SimpleLayoutPanel right now which will display it into a full screen. As stated above, I would wish to include text above the geoChart. From my understanding, I would need to create another widget containing my text and add both the GeoChart widget and the text box widget into it. What would be the best way to go about doing this?
I believe DialogBox could solve your problem. People usually program the DialogBox in a way that it only pops up into display when certain event is triggered and disappears after user finishes some operation. In your particular case, you can simply make the DialogBox shows up from the beginning and never disappears. And the best part of it: you don't need to add the DialogBox widget to the geoChart widget. Calling dialogBox.center() or dialogBox.show() will do the magic for you.
Here is the sample code.
#Override
public void onModuleLoad() {
dataReader = (DataReaderAsync) GWT.create(DataReader.class);
RootLayoutPanel.get().add(getSimpleLayoutPanel());
// Create the API Loader
ChartLoader chartLoader = new ChartLoader(ChartPackage.CORECHART);
chartLoader.loadApi(new Runnable() {
#Override
public void run() {
getSimpleLayoutPanel().setWidget(getGeoChart());
drawGeoChart();
}
});
// NOTE: the first argument 'false' makes sure that this dialog box
// will not disappear when user clicks outside of it
// NOTE: the second argument 'false' makes sure that mouse and keyboard
// events outside of the dialog box will NOT be ignored
DialogBox dialogBox = new DialogBox(false, false);
DialogBox.setText("Demo");
HorizontalPanel panel = new HorizontalPanel();
panel.setSpacing(5);
InlineLabel labelOfTextBox = new InlineLabel("Label");
TextBox textBox = new TextBox();
panel.add(labelOfTextBox);
panel.add(textBox);
dialogBox.setWidget(panel);
// show up in the center
dialogBox.center();
}
Dear all thanks for answering my question. To rectify this problem, I have made use of the custom widget API within GWT(known as Composite). Here's the code as below:
private static class CombinedWidget extends Composite {
public CombinedWidget() {
// place the check above the text box using a vertical panel.
VerticalPanel panel = new VerticalPanel();
DockLayoutPanel dPanel = new DockLayoutPanel(Unit.EM);
panel.setSpacing(13);
panel.add(nameProject);
nameProject.setStyleName("gwt-Group-Label");
panel.add(className);
panel.add(nameKs);
panel.add(nameEsmond);
panel.add(nameBowen);
panel.add(nameAaron);
dPanel.addWest(panel, 13);
dPanel.add(getGeoChart());
// all composites must call initWidget() in their constructors.
initWidget(dPanel);
setWidth("100%");
}
Actually I sort of changed from the original idea. Instead of putting it on the very top, I attached the labels into a VerticalPanel and then created a CombinedWidget(custom widget) which adds both a VerticalPanel and DockLayoutPanel together. I then added the VerticalPanel(containing all the labels) and the GeoChart into the DockLayoutPanel.
This solved my problem of displaying both the labels and the GeoChart on the same page(as originally i added it into a VerticalPanel but it would not work as the app would not read the GeoChart due to the VerticalPanel being overlayed on top of the GeoChart).
If you guys want a picture of my app to visualise, please say so!

Getting hold of a reference to the object created by JOptionPane static methods

I wonder if it is possible to get hold of a reference to the (JDialog?) object created by one of those static methods of JOptionPane (e.g. showMessageDialog)? I intend to modify the position where the dialog appears on the screen. More specifically, I want the dialog to appear at the top-left corner of the main app window, instead of the centre of the window by default. So having a reference to the object would enable me to use setLocation to achieve the desired effect...
Any suggestion would be appreciated! Thanks!
The static showXXXDialog() methods are just for convenience. If you look at the source code for JOptionPane, you'll find that in actuality, a JOptionPane object is created based on the options you specify and then JOptionPane.createDialog(...) is called. One method to show your message dialog at a different position is:
JOptionPane pane = new JOptionPane("Message", JOptionPane.WARNING_MESSAGE,
JOptionPane.DEFAULT_OPTION);
JDialog dialog = pane.createDialog("TITLE");
dialog.setLocation(0, 0);
dialog.setVisible(true);
// dialog box shown here
dialog.dispose();
Object selection = pane.getValue();
With a combination of parameters to the JOptionPane constructor, and JOptionPane set methods, you can do anything you would have done with the static methods, plus you have access to the JDialog object itself.
EDITED: (to add example of input dialog for OP)
JOptionPane pane = new JOptionPane("Message", JOptionPane.QUESTION_MESSAGE,
JOptionPane.OK_CANCEL_OPTION, null, null, null);
pane.setWantsInput(true);
JDialog dialog = pane.createDialog(null, "Title");
dialog.setLocation(0, 0);
dialog.setVisible(true);
String str = (String) pane.getInputValue();
The JOptionPane will use the given parentComponent (first method parameter) to determine where to center the dialog (for example in javax.swing.JOptionPane.showMessageDialog(Component, Object))
You could try to pass in a fake component which positions the dialog to another location, for example like this:
JFrame frame = new JFrame("Test");
frame.setLocation(100, 100);
frame.setSize(500, 500);
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// 'Invisible' fake component for positioning
JWindow c = new JWindow();
c.setSize(0, 0);
c.setVisible(true);
Point location = frame.getLocation();
location.translate(200, 100);
c.setLocation(location);
JOptionPane.showInputDialog(c,"Foo");

Tips for displaying an image in swt/jface dialog box

I'm looking for tips/tutorials for displaying an image in a SWT/JFace dialog box.
Can someone please point me to the right way?
Take a look at the official tutorial.
If it is clickable:
Image image = new Image(display,
ShellWithButtonShowingEclipseLogo.class.getResourceAsStream(
"yourpicture"));
Button button = new Button(shell,SWT.PUSH);
button.setImage(image);
If it is not clickable then you can use Label instead of Button.
From your question i understood that you are trying to display
an image in the title area of the daialog Box.
You can make use of the "setTitleImage(Image image)" function in
your main class (class which extends the Dialog )
example:
class DemoDialog extends TitleAreaDialog {
public DemoDialog(Shell parentShell) {
super(parentShell);
}
protected Control createDialogArea(Composite parent) {
setTitle("Demo dialog...");
setTitleImage(ImageObject) // Image to be displayed in your Dialog
}
}

Categories