I am trying to have wicket display an information dialog after a save button is clicked which invokes an onsubmit that has no access to AjaxRequestTarget target. Here is code snippet
if (trainingmode() && !recordDecision.equalsIgnoreCase("Primary")) {
if (trainingEvalService.compareDecisions(recordDecision, recordSet.getRecordSetId())) {
System.out.println("Validity matchesMaserati: " + trainingEvalService.getTrainingEval().getActual_validity_decision_comment());
// Dialog associated with save button
dialog = new MessageDialog("dialog", "Notice", "Decision Matches " + trainingEvalService.getTrainingEval().getActual_validity_decision_comment() , DialogButtons.OK_CANCEL, DialogIcon.WARN) {
public void onClose(AjaxRequestTarget target, DialogButton button) {
}
};
dialog.open(target) // breaks here without reference to AjaxTarget
} else {
}
}
How can I get a reference to the current AjaxRequestTarget?
Two ways :
If you are doing the form submit through a link, then you can add a SimpleAttributeModifire to it.
Example :
yourLink.add(new SimpleAttributeModifier("onclick","alert('information')"));
This ll display a javascript info, on which you can show the information.
Note :
If you are not submitting from link, then you can do that by putting the submit code in the onSubmit() of a SubmitLink.
You can also use ModalWidow for this, but for that you ll need Ajax submit.
You can use AjaxSubmitLink for that.
You can find ModalWindow code from here:
http://www.wicket-library.com/wicket-examples-6.0.x/ajax/modal-window;jsessionid=2E08EC28B0C0A1AD3F8399628F048003?0
Related
I have a page that has a button to download information in the format of a CSV file. The button opens a confirm dialog to download the file. I need to store that file to a temporary location (whether that be memory or saving to an actual file and then deleting it after) and then read the data in the CSV to an array.
I've tried the code in these questions (question 1, question 2, question 3, and question 4), and it's not quite what I need - mostly because they weren't downloading a CSV and using the data in it.
I'm not sure that the ConfirmDialog is being opened, but I did add a ConfirmHandler returning true to attempt to download the file. However, I don't know where the file is downloading if it is at all.
Here's what I have happening and where I get stuck:
I log in just fine. I go to a report generator. I generate a report that opens in a new window. The new window opens fine and I catch it with a WebWindowListener. I then search for the "save as CSV" button on the new window. I can find that, and I can click on it, but a System.out.print call shows that the ConfirmHandler isn't firing.
for (DomElement e : newPage.getElementsByTagName("button")) {
int i = 0;
webClient.setConfirmHandler(new ConfirmHandler() {
private static final long serialVersionUID = 1L;
#Override
public boolean handleConfirm(Page arg0, String arg1) {
System.out.println("Test"); //isn't firing
return false;
}
});
if (((HtmlButton) e).getAttribute("onclick").contains("CSV")) {
((HtmlButton) e).click();
}else {
if (i++ == (newPage.getElementsByTagName("button").size() - 1)) throw new AssertionError("CSV button not found");
}
}
The inspiration for this answer came from this answer.
I really wanted to get the CSV information without downloading the file, and so I, inside of the webWindowContentChanged(arg0) method of my webWindowListener, just used arg0.getWebWindow().getEnclosingPage().getWebResponse().getContentAsString() and then used String.split() a couple times to get the information I wanted.
Here's what the code looks like so it's a little clearer:
webClient.addWebWindowListener(new WebWindowListener() {
#Override
public void webWindowContentChanged(WebWindowEvent arg0) {
if (CSVclicked) { //boolean that is set true when I click the download button...
String CSV = arg0.getWebWindow().getEnclosedPage().getWebResponse().getContentAsString();
//do things...
CSVclicked = false; //don't use the same behavior next time the method is called...
}
}
});
I have rename dialog for rename file
String renameTo = JOptionPane.showInputDialog(gui, "New Name", currentFile.getName());
it works this way, but I have a problem.
the problem is that I set the default value with the extension of the file
but I just want the file name to be selected.
sample : my file name = yusuf.png
I want select only yusuf like;
There is a lot going on inside JOptionPane, it's one of the things that makes it so powerful, it also makes it a little inflexible to.
Two immediate problems are apparent...
You can't gain direct access to the JTextField been used to get input from the user
The JOptionPane wants to control which components have focus when the dialog is first shown.
Setting up the JTextField is actually straight forward...
String text = "yusuf.png";
int endIndex = text.lastIndexOf(".");
JTextField field = new JTextField(text, 20);
if (endIndex > 0) {
field.setSelectionStart(0);
field.setSelectionEnd(endIndex);
} else {
field.selectAll();
}
This will basically select all the text from the start of the String up to the last . or all the text if no . can be found.
The difficult part now is taking back focus control from the JOptionPane
// Make a basic JOptionPane instance
JOptionPane pane = new JOptionPane(field,
JOptionPane.PLAIN_MESSAGE,
JOptionPane.OK_CANCEL_OPTION,
null);
// Use it's own dialog creation process, it's simpler this way
JDialog dialog = pane.createDialog("Rename");
// When the window is displayed, we want to "steal"
// focus from what the `JOptionPane` has set
// and apply it to our text field
dialog.addWindowListener(new WindowAdapter() {
#Override
public void windowActivated(WindowEvent e) {
// Set a small "delayed" action
// to occur at some point in the future...
// This way we can circumvent the JOptionPane's
// focus control
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
field.requestFocusInWindow();
}
});
}
});
// Put it on the screen...
dialog.setVisible(true);
dialog.dispose();
// Get the resulting action (what button was activated)
Object value = pane.getValue();
if (value instanceof Integer) {
int result = (int)value;
// OK was actioned, get the new name
if (result == JOptionPane.OK_OPTION) {
String newName = field.getText();
System.out.println("newName = " + newName);
}
}
And, crossing our fingers, we end up with something looking like...
Personally, I'd wrap this up in a nice reusable class/method call which returned the new text or null based on the action of the user, but that's me
Isn't there an easier way?
Of course, I just like showing you the most difficult solution possible ... 😳 (sarcasm) ... it's kind of why I suggested wrapping it up in it's own utility class, so you can re-use it later 😉
I have a popup that show a table with data, I am able to select a row, and by pressing a OK button I can retrieve the idNo of the selected row in the table.
What I want to do is to pass this idNo to the window that is calling the popup and update a outputText that is on this window.
Can some one help me?
Code for the button:
newBean Class for the button:
public String b1_action() {
// Add event code here...
System.out.println("Select One Button has been Clicked");
// Get bindings associated with the current scope, then access the one that we have assigned to our table - e.g. OpenSupportItemsIterator
DCBindingContainer bindings =
(DCBindingContainer)BindingContext.getCurrent().getCurrentBindingsEntry();
DCIteratorBinding dcItteratorBindings =
bindings.findIteratorBinding("NameView1_1Iterator");
// Get an object representing the table and what may be selected within it
ViewObject voTableData = dcItteratorBindings.getViewObject();
// Get selected row
Row rowSelected = voTableData.getCurrentRow();
// Display attriebute of row in console output - would generally be bound to a UI component like a Label and or used to call another proces
System.out.println(rowSelected.getAttribute("IdNo"));
setOutputText("" + rowSelected.getAttribute("IdNo") + "");
closePopup("p1");
return null;
}
I want that my function: setOutputText() which is not implemented yet to be able to update my outputText on the main Window.
Thanks
Best Regards
Put the "IdNo" in view or page flow scope depending on how you want to keep the value.
//view scope
AdfFacesContext.getCurrentInstance().getViewScope().put("IdNo", value);
//or page flow scope
AdfFacesContext.getCurrentInstance().getPageFlowScope.put("IdNo", value);
In the window bean, write a listener for the popup dialog:
public void dialogCloseListener(DialogEvent dialogEvent) {
if (dialogEvent.getOutcome().equals(DialogEvent.Outcome.ok)) {
String idNo = AdfFacesContext.getCurrentInstance().getViewScope().get("IdNo");
//now you have the idNo, do whatever you want
}
}
You can also use the returnListener inside the button or link that is invoking the popup like in this article
before I start, I'm a beginner programmer.
How can I enable a text field when a button is clicked.
I have two frames, one that has the JFields and the other for the exception.
When the exception occurs > setEditable(false)
but what statement should I make to enable the JFields once the user click on okay button -that i've made in the exception-?
I've tried to add static boolean to exception frame, and inside the action performed of this class I initialized that boolean to true.
in the other class, I added an if statment, if that boolean is true, then setEditable(true)
-========-
The point of this program, that when the exception occurs the user cannot enter anything in the fields until he closes the exception window.
I wish you'd help me.
With all love, programmers.
The code of action performed for THE EXCEPTION WINDOW FRAME ( having Okay button. )
public void actionPerformed(ActionEvent e){
{
allow=true; //static boolean
Container TheFrame = OKButton.getParent();
do TheFrame = TheFrame.getParent();
while (!(TheFrame instanceof JFrame));
((JFrame) TheFrame).dispose();
}
The code of action performed for THE MAIN PROGRAM (having three fields, an exception will occur once the user enters non digits )
I added some comments to clarify.
public void actionPerformed(ActionEvent event) {
try{
r =Double.parseDouble(RField.getText());
s=Double.parseDouble(SField.getText());
h=Double.parseDouble(HField.getText());
Cone C = new Cone(r,s,h);//class cone
if (event.getSource() instanceof JButton) {
JButton clickedButton = (JButton) event.getSource();
if (clickedButton == VolumeButton) {
Result.append("VOLUME = "+C.volume()+ "\n");
ifV= true;//this's for clearing the fields for new entries.
}
if (clickedButton == AreaButton) {
Result.append("SURFACE AREA = "+C.surfaceArea()+ "\n");
ifA= true;//this's for clearing the fields for new entries.
}
if(ifA&&ifV){ // clearing the fields for new entries.
SField.setText(CLEAR);
HField.setText(CLEAR);
RField.setText(CLEAR);
ifV=false; ifA= false;}
}
SList.addShape(C);
}
catch(NumberFormatException e){
//Object of type "Exception__" already created
Ex.setVisible(true);//class "Exception__" is the one i've made for Exception window
SField.setText(CLEAR);
HField.setText(CLEAR);
RField.setText(CLEAR);
SField.setEditable(false);
HField.setEditable(false);
RField.setEditable(false);
}/*here, if the user clicked on -that okay in Exception window-
and variable allow initialized to "true" those statements should extend. I guess?
- everything worked correctly except for this ?*/
if(Ex.allow){
SField.setEditable(true);
HField.setEditable(true);
RField.setEditable(true); }
}
THANK YOU ALL IT FINALLY WORKED.
I added
Ex.allow(SField,HField,RField);
to the catch.
and added this method in class Exception__:
public void allow(JTextField js,JTextField jh,JTextField jr){
HField =jh;
SField =js;
RField =jr;
}
finally, to the action performed of class Exception__:
SField.setEditable(true);
HField.setEditable(true);
RField.setEditable(true);
WOHOOOO. It feels so awesome lol. Thanks all. should I delete my question or leave it for others who might face the same problem as mine? :P
Your question needs a lot more detail. But if all you want to to show an 'exception window' and allow the user to do anything else only after she dismisses this window, I think all you need is a MessageDialog:
See JOptionPane
If you need more details to be displayed you can create your own modal JDialog.
See How to Make Dialogs
Make the text field hiden by writing:
jTextfield.setVisible(fasle);
in the constructor of your form code. than use the button event " Action -> Action Performed " and write the code:
jTextfield.setVisible(true);
and thus your text field will be visible only after the button will be clicked.
I have a servlet with a form and two buttons. One is a Submit button, the other is a Delete button. The default action is Updater.do, but when I click the Delete button, I have a function to change the action to Deleter.do. This works fine. The problem I have now is that when I put in a confirm dialog, if the user clicks "OK", then it does go on to Deleter.do, as I wanted. However, when they click "Cancel", instead of just staying on the page, it appears to go to "Updater.do". How would I remedy this? I tried deleting the default action and having both Submit and Delete call changeAction, but that didn't work. Here is the javascript chgAction.
function chgAction(action_name) {
if (action_name=="Delete") {
var answer = confirm("Are you sure you want to delete this Person? This action cannot be undone.")
if (answer) { document.forms[0].action = \"Deleter.do\"; }
}
}
Try adding a return false to the "cancel" part:
function chgAction(action_name) {
if (action_name=="Delete") {
var answer = confirm("Are you sure you want to delete this Person? This action cannot be undone.")
if (answer) {
document.forms[0].action = \"Deleter.do\";
}
else {
return false; //ADDED
}
}
}
If both submit and delete are submit buttons, just give them different names. The name of whichever button is clicked will be sent with the request so the server can sort it out.
When calling the confirm dialog, you must return false from the listener or the form will still submit:
<form onsubmit="return chgAction(...);"
and chgAction must return false as suggested by Nivas.