Thread: [Java-gnome-developer] Grabbing default in a subclass of Dialog
Brought to you by:
afcowie
From: Richard S. <ri...@r-...> - 2010-08-10 09:15:18
|
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm trying to create dialogs with default buttons but I can't get them to work. I've subclassed org.gnome.gtk.Dialog and used addButton() in the constructor like this: Button applyButton = new Button(Stock.APPLY); addButton(Stock.CLOSE, ResponseType.CLOSE); addButton(applyButton, ResponseType.APPLY); applyButton.setCanDefault(true); applyButton.grabDefault(); But when I show the dialog and press return, the default button isn't activated? I tried exposing the button from the dialog class with a getter method and calling dialog.getDefaultButton().grabDefault() after I'd shown the dialog but that doesn't seem to work either. Am I missing something? - -- Regards, Richard -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAkxhGJ8ACgkQdPdD0eWqTe7VywCgtKe+f3nvF5V9aXajaJg2QVHE i9oAoMaioq+D3kwhem95m0iAofVcXtKo =Pp9U -----END PGP SIGNATURE----- |
From: Andrew C. <an...@op...> - 2010-08-11 00:31:37
|
On Tue, 2010-08-10 at 10:15 +0100, Richard Senior wrote: > applyButton.setCanDefault(true); > applyButton.grabDefault(); > > But when I show the dialog and press return, the default button isn't > activated > Am I missing something? No and yes. I just tried it with some very similar code that I have, and discovered that likewise calling Widget's grabDefault() doesn't do it. Damn. I poked around in the GTK docs, and it turns out there is a method gtk_dialog_set_default_response(). Which works. You have to call setCanDefault() on your custom Button first, though. I've added coverage for setDefaultReponse() to Dialog, and merged it to 'mainline'. It'll be in java-gnome 4.0.17 AfC Sydney |
From: Richard S. <ri...@r-...> - 2010-08-11 13:02:21
|
On 11/08/10 01:31, Andrew Cowie wrote: > I've added coverage for setDefaultReponse() to Dialog, and merged it > to 'mainline'. It'll be in java-gnome 4.0.17 Thanks Andrew, I've grabbed the mainline source and compiled it, but I still can't get it to work. I'm definitely using the 4.0.17-dev version. Sorry for the long and untidy code snippet but here's a test case that doesn't work for me: package test; import org.freedesktop.bindings.Version; import org.gnome.gdk.Event; import org.gnome.gtk.*; public class TestDefaultButton { public TestDefaultButton() { // Create a main window Window window = new Window(); window.setTitle("Default Button (Java-Gnome v" + Version.getVersion() + ")"); window.setDefaultSize(600, 400); // Add a button to the main window that launches a dialog Button showDialogButton = new Button("Show Dialog"); HBox hbox = new HBox(true, 0); hbox.packStart(showDialogButton, false, false, 0); VBox vbox = new VBox(true, 0); vbox.packStart(hbox, false, false, 0); window.add(vbox); // Create a dialog final Dialog dialog = new Dialog("Dialog", window, false); final Entry entry = new Entry(); // Add a label and text entry to the dialog HBox hbox1 = new HBox(false, 5); hbox1.packStart(new Label("Enter text:"), false, false, 10); hbox1.packStart(entry, true, true, 5); VBox vbox1 = new VBox(true, 0); vbox1.packStart(hbox1, true, true, 10); dialog.add(vbox1); // Add close and apply buttons, making apply the default final Button defaultButton = new Button(Stock.APPLY); defaultButton.setCanDefault(true); dialog.addButton(Stock.CLOSE, ResponseType.CLOSE); dialog.addButton(defaultButton, ResponseType.APPLY); dialog.setDefaultResponse(ResponseType.APPLY); // Connect the handler that shows the dialog showDialogButton.connect(new Button.Clicked() { public void onClicked(Button source) { entry.setText(""); entry.grabFocus(); defaultButton.grabDefault(); dialog.showAll(); } }); // Connect a window delete handler to the main window window.connect(new Window.DeleteEvent() { public boolean onDeleteEvent(Widget source, Event event) { Gtk.mainQuit(); return false; } }); // Connect the dialog response to print the content of the entry on // apply, // or to close the dialog on close dialog.connect(new Dialog.Response() { public void onResponse(Dialog source, ResponseType response) { if (response == ResponseType.APPLY) { System.out.println(entry.getText()); } else { source.hide(); } } }); // Show the main window window.showAll(); } public static void main(String... args) { Gtk.init(args); new TestDefaultButton(); Gtk.main(); } } -- Regards, Richard |
From: Kenneth P. <ken...@gm...> - 2010-08-12 16:09:05
Attachments:
signature.asc
|
On Wed, 11 Aug 2010 14:02:12 +0100 Richard Senior <ri...@r-...> wrote: > [...] > > Sorry for the long and untidy code snippet but here's a test case that > doesn't work for me: > [...] It would seem that the Entry is interfering in this test case, as if you remove the Entry hitting enter activates the default button. |
From: Richard S. <ri...@r-...> - 2010-08-13 11:59:53
|
On 12/08/10 17:08, Kenneth Prugh wrote: > It would seem that the Entry is interfering in this test case, as if > you remove the Entry hitting enter activates the default button. You are quite right. I removed the Entry field and played around with it a bit more but still didn't have much success with the dialog.setDefaultButton() method: Havign set Apply as the default button, the dialog shows initially with the Apply button focussed and pressing return invokes the apply processing. However, having pressed Close, the next time the dialog is opened, the Close button has become the default. I tried moving the setDefaultButton() call around - into the handler that shows the dialog for example, so that setDefaultButton() is called every time the dialog is shown, but still the same. I ened up with the flow I wanted (for a goto line dialog in a text editor), by not using setDefaultButton() or setCanDefault() at all. I just added an Entry.Activate() handler on the Entry that emits clicked on the default button when I press return: // Add close and apply buttons final Button defaultButton = new Button(Stock.APPLY); dialog.addButton(Stock.CLOSE, ResponseType.CLOSE); dialog.addButton(defaultButton, ResponseType.APPLY); // Connect the handler that shows the dialog showDialogButton.connect(new Button.Clicked() { public void onClicked(Button source) { entry.setText(""); entry.grabFocus(); dialog.showAll(); } }); // Connect a handler to the entry for the default button entry.connect(new Entry.Activate() { @Override public void onActivate(Entry source) { defaultButton.emitClicked(); } }); It's not ideal because, as a user, you get no feedback about which is the default button. I notice that gedit neatly sidesteps the issue by using a different type of control for goto line. ;) -- Regards, Richard Senior IRC #java-gnome: san-ho-zay |