Setting a JTextfield focused from start - java

Making a project for school that involves scanning something from my "start frame" that looks like this --> http://i.gyazo.com/21ed444ad98b441e95c69901eceaef00.png
the textfield in the left bottom corner is where your scanned ID goes. to make it look cleaner i want to set it focused from start and then invisible. (does this even work?) so you can just scan without seeing numbers appear on the screen.
But setting the focus does not work for me. I've tried all these methodes
txtKlantID.grabFocus();
txtKlantID.requestFocus();
txtKlantID.requestFocusInWindow();
txtKlantID being the name of the textfield.
thanks in advance

i want to set it focused from start... But setting the focus does not work for me. I've tried all these methods
The proper method to use is the requestFocusInWindow() method. However, you can't request focus on a component unless the component is displayed in a visible GUI. So the basic code must be structured like:
frame.pack();
frame.setVisible(true);
component.requestFocusInWindow();
It may not always be possible to structure your code that way and you want a component to request focus when it is created. In this case you can add an AncestorListener to the component so that it will request focus when it is displayed. Check out the RequestFocusListener for code that does this for you.

Related

How to create dynamic wizard?

I am making project with GUI. The thing is, that I have a button and what I need to do is that after clicking this button I need to change Frame layout. For example, like when you are installing some program and you click "next" button, the Frame layout changes and you can see some different content. Basicly, dynamic wizard.
I have tried use another Frame, but it opens in another window and that is not what I want. I want to open it in the same window.
Another thing I have tried is set visibility of these components I don't want to be displayed to false, but I find it unprofessional and it is overlook in making a desing, when I have components over themselfs.
So do you guys have any idea? Thank you.
Most of the times for a wizard like GUI, you should have JFrame and a set of JPanels. In each step you can pass the shared data as constructor arguments to each panel, and when you are making one of them invisible and make another one visible, you can get some date from the previous step panel and pass it to the next step panel(if needed).
It is very common that your panels extend the JPanel and have some argument in their constructor(s). You use these data for initializing your panel and managing the state of the overall progress.
There is no a total plan for all situations. So you should decide what to do which is best fit for your case.
Try not to have multiple JFrames.
Hope this would be helpful.

Context menu on Pane that disappears correctly

I am trying to have a JavaFX Pane (VBox in my case, but I don't think it matters) that has a ContextMenu that behaves correctly.
I have found these two questions: why Panes can't have ContextMenus and How to create ContextMenu within a Pane.
The problem I'm having with these two solutions (which are very similar) is that while the context menu correctly disappears if I click on the pane, it doesn't disappear if I click inside a control within that pane. The simplest way to observe this flaw is to create such a pane with a TextField. Right-click on the pane to show the context menu, then click inside to TextField to focus on it. While a proper context menu would disappear at this point, this "hacked-in" context menu (for lack of a better term) happily stays in it's place, possibly blocking the user's view of the text field they are trying to fill.
Now, I know I can add a change listener to the focused property of each and every control on my pane, but that feels redundant. Is there a better way to make sure the context menu is hidden when a control in my pane is selected (or, more accurately - when the user click the mouse anywhere in the owning window outside the context menu)?
What I tried so far and doesn't work -
Adding a change listener to the pane's focused property - it appears the pane isn't considered focused if one of it's children is
Adding a change listener to the context menu's focused property - it appears the context menu's focus isn't changed when clicking outside of it.
Ok, so after some digging in the source code for JavaFX I have found this workaround (which is what "solves" this problem for normal controls). Simply add this line of code -
contextMenu.setImpl_showRelativeToWindow(true);
Now, I know using internal implementation methods is discouraged as they may disappear, but this is the only solution I have found. If anyone has a better solution I'll be glad to hear it, but I suspect this is simply a bug that should be filed (i.e. - there should be a way to use the showRelativeToWindow mechanism when setting context menus on panes).
I guess a somewhat safer solution would be to have a throwaway control (not in the scene graph) on which to set the context menu, but I don't know if this would have any unwanted side effects:
Label throwaway = new Label(); // No special reason for using Label, could be any Control.
throwaway.setContextMenu(contextMenu); // note that this is the only place `throwaway` is used, it is never added to the scene graph
// or referenced again, but just setting the context menu on a control solves the problem.
Edit
After digging some more I have found this, and a solution in the discussion - you should call the show method overload which takes a window, not a node! Not very clear, but it works:
myPane.setOnContextMenuRequested(event ->
contextMenu.show(myPane.getScene().getWindow(), event.getScreenX(), event.getScreenY())
);

.pack() causes the components to be in the wrong place but works after window resize

I am creating a game in java using Swing.
I am using
mainJFrame.setExtendedState(mainJFrame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
on my JFrame to start it as a maximized window.
After this I am using
mainJFrame.pack();
to be able to get the correct x & y coordinates of components in the GUI. I need those coordinates since i'm painting custom images to the GUI. So far everything is fine.
When a user has passed a level, i rebuild the GUI to updated it to the new level settings and then i call pack() again.
When calling pack() the components are placed in the wrong place, slightly below where they are supposed to be.
However, manually resizing the window causes the components to go to the right place again.
I have tried calling mainJFrame.revalidate() and mainJFrame.repaint() after the pack() but this gives no effect.
If needed I can supply printscreens and code.
Thanks for your time.
Manually resizing the window validates the enclosing container and all of its subcomponents. To avoid the effect you describe, call setVisible() only after you have added components and invoked pack(). More related suggestions may be found here. If the problem persists, please edit your question to include an mcve that exhibits the problem.

OverlayLayout doesn't respect the overlay order with focus change

I encountered a problem with OverlayLayout.
Basically, I created a component which allows to unroll an overlay content above a main content (like a drop down menu). It works!
The problem is: if I put a JButton on the main content (the content below), when I click on this button, then the button starts to appear above the overlay content !
I don't know why?
Does anyone know what the problem is ? Is it maybe the focus ?
I finally found the answer!
It comes from another post on stackoverflow...
When components overlap on the panel then you need to tell the panel so
it can make sure it repaints the components in their proper ZOrder:
You do this by overriding the isOptimizedDrawingEnabled() method of
the JPanel to return false.

Small Window Over Components

I already look at java library and dont know what to use to do this..
I already tryed JInternalFrame but thats not what I really want.. because it needs to be added to a JDesktopPanel right??
And in my program I have a JFrame with content pane using BorderLayout..
Then on borderlayout center I have a JTextArea, on borderlayout east I have a list.. and on borderlayout south I have a JPanel..
What I want is, when I do a certain action, it will pop up a "mini window" where I need to choose something.. u see?
and if I create JDesktopLane it will overlap what I have on the container..
the window will be made by my like a color chooser pallete , like a grid with colors.. and a label on top saying some text..
I just dont know how to make a "window" over the other components, and users can still drag over the frame, and interact with all the other components.. the jtextarea and such..
I guess you understood, thanks alot in advance!!
If u dont understand something please tell me, I really want to do this :)
Just dont know what to use..
Thanks again ;)
Have you tried JDialog?
It's because Jdialog are not component to be add in a JFrame, it's an independant thing running on it's own
if you use JDialog, the construct parameter parent indicate wich frame the JDialog is related to.
The typical class for this task is JWindow, a borderless top-level window that can be freely positioned. You could use SwingUtilities.getPointFromComponent to get the screen coordinates for a realized coordinate.
Top-level windows (JFrame, JDialog, JWindow) are not added to containers. They can get other windows as parent.
I dont want to use another JFrame.. that is kinda bad for code, its a small window with a simple function..
Structure your code so you can read it, others can read it, and you can debug it easily (the latter is a result from the first). A low class count is useless and -most of the time- contraproductive.
Why should another JFrame (or other window) be bad?
If you absolutely want to avoid opening top level windows (e.g. to avoid applet warning icons or to implement a special kind of user interface) you could use a JLayeredPane to add additional JPanels above your existing GUI elements.

Categories

Resources