so I searched online and tried things and common functions to focus on window but whenever I click on Exit button, it won't return to main JFrame.
When I remove the this.setEnabled(true), it does it but what I really wanted to do is to disable the main JFrame when jButton9 is clicked and show the JInternalFrame. Then close the JInternalFrame when Exit button is clicked.
I tried the instructions on this website http://www.coderanch.com/t/334157/GUI/java/JInternalFrame-Focus
Here's what I've done so far. I'd appreciate any help.
private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {
AddTo_Assigned_Subjects_InternalFrame.setVisible(true);
this.setEnabled(false);
AddTo_Assigned_Subjects_InternalFrame.requestFocusInWindow();
}
private void ExitActionPerformed(java.awt.event.ActionEvent evt) {
this.requestFocusInWindow();
AddTo_Assigned_Subjects_InternalFrame.setVisible(false);
AddTo_Assigned_Subjects_InternalFrame.dispose();
}
"this" is my MainFrame
"Add_To_Assigned_Subjects_InternalFrame" holds the exit button.
Thanks.
Form Javadoc on requestFocusWindow()
This method cannot be used to set the focus owner to no Component at all. Use KeyboardFocusManager.clearGlobalFocusOwner() instead.
The focus behavior of this method can be implemented uniformly across platforms, and thus developers are strongly encouraged to use this method over requestFocus when possible. Code which relies on requestFocus may exhibit different focus behavior on different platforms.
Related
I have written an application in Java that has a JFrame with options. I now want a certain action to be executed when the user has confirmed the dialog with "Ok". I was recommended to add a return value to JFrame.
Unfortunately, I don't have any experience and need some help. Here are the details.
I want to extend JFrame so that I can have an Enum "DialogResult" when closing a JFrame like in the .Net Framework. Well, the Enum is no problem. My problem is to replicate the ShowDialog method from WinForms of the .Net Framework working in Java for the class JFrame.
Below is an example code in C#:
// DlgOptions : Form
DlgOptions dlg = new DlgOptions();
if(dlg.ShowDialog() == DialogResult.Ok)
{
// do something only when "Ok" was clicked
}
Here's a link from MSDN with the behavior I want to replicate:
https://msdn.microsoft.com/de-de/library/c7ykbedk(v=vs.110).aspx
How can I best implement this? Thanks in advance.
EDIT:
I changed my DlgOptions class so it doesn't extend JFrame but JDialog. I also added an enumeration DialogResult and added a public property of this type in my DlgOptions. But I still have a problem. When I use this code:
// executed when user clicked a JMenuItem in a JMenuBar
DlgOptions dlg = new DlgOptions();
dlg.setModal(true);
dlg.setVisible(true);
if(dlg.DialogResult == DialogResult.OK)
{
// do something
}
the program continues running before the user closed the modal dialog. What can I do?
EDIT 2:
My JDialog contains two JButtons; one to confirm changes that were made and one to abort changing the preferences for the program. I have several JCheckBoxes the user can check or uncheck.
So a JOptionPane would not be what I want / need (as far as I know). That's why I need a modal JDialog. But my Java code above doesn't work as I want it to. I read on a German website of a Java book that a JDialog set to modal with
setModal(true);
would cause the program to wait until the dialog is closed. The problem is, that the code continues too early.
I've written a basic calculator type program using WindowsBuilder in Eclise neon and Java 1.8. It's pretty much complete, with everything working how I want it to. Except keyboard entry.
As a finishing touch I'd like to detect keyTyped events and map them to button presses so users can use the keyboard for entry instead of clicking buttons with the mouse.
I've added 'implements KeyListener' to the program class...
public class CashRegister implements KeyListener {
private JTextField keyb;
I've tried to set a listener to a invisible JTextField called keyb....
private void initialize() {
keyb = new JTextField(20);
keyb.addKeyListener(this);
keyb.setFocusable(true);
keyb.requestFocusInWindow();
And I've added methods to handle the captured keypress...
public void keyTyped (KeyEvent e) {
String out = "";
out = out + e.getKeyChar();
pence1text.setText(out);
}
public void keyPressed (KeyEvent e) {
}
public void keyReleased (KeyEvent e) {
}
So, at this stage all I'm expecting, prove it is working, is the keycharacter I press to appear in the textfield called 'pence1text'. However, it doesn't work, when I press the a key nothing is displayed.
I think it could be a focus problem. Googling around and checking stackoverlow lead me to add the following lines...
keyb.setFocusable(true);
keyb.requestFocusInWindow();
as above, but still no luck.
Does anyone have any ideas what I am doing wrong, or what I can try next?
Thanks
Thanks to user Andrew Thompson for pointing me back to the docs and a re-read.
The problem is that the JTextField is not visable and thus can't be given focus. If I add the listener to a textfield that is visable then the program works correctly. However if the user uses the mouse to click a button it loses focus and breaks the implementation...so I need to rethink the code and keep looking at focuse settings.
As a finishing touch I'd like to detect keyTyped events and map them to button presses so users can use the keyboard for entry instead of clicking buttons with the mouse.
Don't use a KeyListener.
Instead you should be using Key Bindings. Read the section from the Swing tutorial on How to Use Key Bindings for basic information.
Also check out: how to put actionlistenerand actioncommand to multiple jbuttons for a working example that shows how you can apply the key bindings to a calculator.
I noticed a behavior that I can't explain. In my GUI, on a button click I display a custom Jdialog that has panel and bunch of textfield. I populate these textfields.
Here is the scenario I am seeing using pseduo code.
public void actionPerformed(ActionEvent e) {
CustomDialog viewDialog = new CustomDialog (Jframe, true);
viewDialog.setVisible(true);
viewDialog.populateInfo();
}
When the code above runs then all textfields are empty. However if I move the setVisible to after the populateInfo method then all the textFields are populated. Basically the JTextField.setText inside the populate info does not seem to have an affect if the setVisible happens before
Why is this!
Likely your CustomDialog class is a modal JDialog (also as suggested by the true 2nd constructor parameter). If so, then program flow in the calling code is blocked by the setVisible(true) call, and so your populateInfo() method will only be called after the dialog is no longer visible. The solution is as you already know -- call the method before displaying the dialog.
This is not a bug but a feature. :)
Seriously, since now you know for a fact when program code flow will be halted and when it will resume, and so you can safely query the dialog for its state after the setVisible(true) has been called, and feel confident that in the very least the dialog has been presented to the user, and the user has had time to interact with it and dispose of it.
Most windows users may remember that every windows 98 properties/settings window had a little question mark button next to other window buttons:
If you clicked on that button, all click events were overriden by different callback for that window. And that new callback would display element's individual help text.
I'd like to do the very same. My idea was to do it using class which holds all JComponent and Help associations:
public interface Help {
/** based on implementation, displays help to the used. May use
* JDialog, url redirection or maybe open document on the computer.**/
public void getHelp(JComponent comp, ActionEvent evt);
}
public class HelpLibrary {
public HashMap<JComponent, Help> helpLib;
public void getHelp(JComponent comp, ActionEvent evt) {
Help help = helpLib.get(comp);
if(help!=null) {
help.getHelp(comp, evt);
}
}
}
Writing these two classes was the easy part. The hard one is this:
How to override all click events in particular window and then remove override after help was called?
How to ensure help cursor will override all other cursors, and again, safely remove this setting?
I have no idea where to start with this. I really do not want to change the GUI structure or used classes just because of this, which is why I want to store the help and do the overrides from the outside.
public class HelpLibrary {
/**
* Overrides click events on the given window and displays help cursor.
* User then may click a JComponent, such as button, to initiate
* help callback for that element. If no help exists for that element,
* do nothing and stop the help mode.
* #param window the window to get help for
**/
public void waitForHelp(JFrame window) {
???
}
}
You can try following:
Register a global MouseListener using
Toolkit.getDefaultToolkit().addAWTEventListener(myListener, AWTEvent.MOUSE_EVENT_MASK)
Cast the incoming event to MouseEvent and check the event type using the getID() method
If the event is a click for a component, which has help, you need to show help, consume event and remove this listener from the global listener list.
You can also override mouseEnter/Exit event in this
listener for components which have help text, and set the cursor to
question/normal type (don't forget to consume this event).
This is only idea for you, I've not tested whether it works.
You could use a GlassPane.
Read the section from the Swing tutorial on How to Use Root Panes. The Glass Pane demo shows how to intercept mouse events and redispatch the event to the underlying components. YoOu would obviously change this code to find the component under the mouse click and then display the help context.
The glass pane can be toggeled on/off by making it visible or not.
I have an AWT modal dialog
public class d extends java.awt.Dialog {...
On the dialog frame, using netbeans gui designer I put dialog then panel then button.
I am trying to close the dialog by pressing the button.
I am not interested in System.exit(0).
The netbeans generator created
private void jButtonCloseActionPerformed(java.awt.event.ActionEvent evt){
I think I should call dispose in that function, however when called it disposes the dialog but dialog thread never ends.
I have the following handler working when window is closed by default dialog close button
dialog.addWindowListener(new java.awt.event.WindowAdapter() {
#Override
public void windowClosing(java.awt.event.WindowEvent e) {
Window window = SwingUtilities.getWindowAncestor(e.getComponent());
window.dispose();
}
});
and the above is working fine, i.e. thread ends.
I could use the same approach in the jButtonCloseActionPerformed but I don't know how can I get window object.
How can I achieve that?
Any other good solution is very welcomed as well.
I will appreciate your help very much.
I think the best answer (in short) is to use the following code
Window window = SwingUtilities.getWindowAncestor(this);
window.dispose();
this is important here.
I tried to get window object somehow by getting parent object from events, etc. In case of WindowClosing I could get window object reference in that way indeed, but in case of a button it didn't work ... then I realized I can simply refer to this.
Most examples in the Internet calls System.Exit(0) but IMHO calling System.Exit(0) could be OK in the case of examples ONLY, not in real app.