Java Swing: Cannot Edit JComponents in JWindow Extension - java

I wrote a class that extends JWindow that serves as a kind of customizable dialog box in my application. When I need to invoke one of these windows, I create a new instance of the class; to remove the window, I call the method dispose().
The problem I am having is that the user cannot edit components that have a text box, such as JTextField and JSpinner. The user can click on components such as drop-down boxes and buttons, and this works fine, but when it comes to entering text in a text box, this does not work.
Has anyone else experienced this problem?
Thanks!

There are a bunch of conditions to meet until a window child can receive focus, see the api doc for window.isFocusableWindow().
for most contexts it's enough to set its focusableWindowState property to true, like
JFrame owner = new JFrame();
owner.setVisible(true);
JWindow window = new JWindow(owner);
window.setFocusableWindowState(true);
window.add(new JTextField("edit me"));
window.setSize(200, 200);
window.setVisible(true);

1) maybe you are mixing AWT with Swing drop-down box and button, are you sure that all Components definitions starts with J, drop-down boxes == JComboBox, Button == JButton etc
2) don't create lots of Top-Level Containers on the Fly/Runtinme
3) Has anyone else experienced this problem? no, never
4) for real and better help sooner, please edit you post and sent here code that demonstrate your problem here are simples rulles sscce

Related

JFrame issue (close focus)

I have a very basic experience with java and have 2 questions.
Question 1: I have a button that opens a new JFrame, which works
perfectly. On the second iJFrame I have a button which -should- make
the app hide (lose focus).
I have looked around and found that this is easily done with JFrameName".setFocusableWindowState(false
The problem is I can't seem to be able to name the current jframe JFrame I'm in, so I can't call the function. I usually call the JFrame I've made into view with this in the public main of my starting code :
JFrameName newframe = new JFrameName();
newframe.setVisible(true);
Where exactly do I declare the name of the JFrameName instance I've made in my JFrame class so iI can call the setFocusableWindowState function?
Question 2: The above question is done because I want to link a
keyboard shortcut to a button. this keyboard shortcut should then be
used - within another window, not the java application. my question:
can iI manually define keyboard shortcuts (for example
control+alt+delete) or (control+f1) within java so my program will
execute the button hits for me?
I'm not sure I understand the question wholly. But maybe this will help.
Instead of making a JFrame, since it is not a "final" class, you also have the option of creating a new class that extends JFrame. The new class can have additional methods. One of those methods could be one to allow you to tell the new JFrame the name of the JFrame that you want to return focus to.
As far as I know, it's possible to create a KeyListener that can tell if a shift or alt or control key are pressed. I'd have to research the details to know for sure. A good starting point should be the tutorial How to Write a Key Listener.
I have a button which -should- make the app hide (lose focus).
If you want the window to hide then normally you would use:
window.setVisible( false ).
This is the opposite of showing the window.
The setWindowFocusableState(false) will still keep the window visible, you just won't be able to make any component on the window have focus.
The problem is I can't seem to be able to name the current jframe JFrame I'm in
That information can be found by coding the following in the ActionListener of your button:
JButton button = (JButton)e.getSource();
Window window = SwingUtilities.windowForComponent( button );
window.setVisible( false );

Advice: Java GUI Using JFrame

I Have created a form and designed it using Swing Components. It is linked to MySQL, so i have few buttons, like Submit (Which When Clicked validates and updates the database). But I also have buttons to view and edit database. When clicked i have worked on Delete Record using JOptionPane (YES_NO_OPTION etc), but when it comes to editing, i want to put Combo boxes and Text Fields etc, which might not be preferred in JOptionPane. Creating a new Window would help, but is there any other easier Default Classes like JOptionPane in which i can use Many components? Also to display Database records?
I believe what you are looking for are JDialogs, these are the "small" popup windows you are looking for. You may add any swing component you wish to it.
Here is an example of creating one and adding a JTextField to it.
JDialog popup = new JDialog();
//Set window title
popup.setTitle("Example");
//Set window size
popup.setWindowSize(300, 200);
//Force window to stay on top till exit
popup.setModal(true);
//Create and add a JTextField
JTextField input = new JTextField();
popup.add(input);
popup.setVisable(true);

Java Swing - resize Dialog on JLabel.setVisible(...)

I have a Java Swing Dialog with a hidden JLabel above each input component (i.e. JTextField). The purpose of this hidden JLabel is to use it as validation output for its input component.
Let's say, there is an input field for the description of some entity, which has to be non empty, and should contain some special stuff. On error, the action could call the following method:
private void invalidateDescription(String errMessage) {
errDescriptionLabel.setText(errMessage);
errDescriptionLabel.setVisible(true);
descriptionTextField.setBackground(ERR_COLOR);
}
After that, I call pack() and invalidate()
The problem is, that the JDialog still has the same vertical size, so that some of the components (the buttons in the bottom of the dialog) disapear (because they're out of view).
Do you have any suggestion how to fix it?
Best Regards.
edit: I forgott to mention, that the JDialog has a "Free Design" Layout (Netbeans GUI Builder default).
edit 2: I'm looking for a solution which doesn't require kind of a placeholder for (error) JLabel. "Empty Space" is not a desired solution because the dialog doesn't look balanced.
Use CardLayout place your labesl and empty JPanels ans swap thm when necessary.
Instead of using setVisible(), give errDescriptionLabel a background color that matches that of the enclosing panel when the entry is valid.
I have a Java Swing Dialog with a hidden JLabel above each input component
I would not use a hidden component for this. I would change:
private void invalidateDescription(String errMessage)
to
private void invalidateDescription(String errMessage, component inputComponent)
Then I would display a popup with the error message. You could use a non-decorated JDialog as the popup. You might even be able to use a JPopupMenu as the popup.
When you display the popup you would position the popup releative to the input component.

Java GUI Game Menu

I'm trying to work on a project - we are creating a little game with a GUI.
I decided to start by working on a 'main menu'. Essentially, there will be buttons such as "Single Player", "Help", etc...
I've made the GUI with the menu, etc. I have added listeners as well.
How do I approach the problem now? If someone clicks, say, 'Single Player', I'd like the screen to change to display a title showing "SINGLE PLAYER" and get rid of the main menu.
What do I need to do in my actionPerformed() method to get this effect?
I guess I will be able to work it out from there.
Any help would be greatly appreciated.
best of all options could be use JMenuItem to interact with Cards layed by CardLayout, options are described incl. images in the tutorial
best of questions asked about CardLayout ever
The best approach would be to put the corresponding windows onto a separate Jpanel and have one master panel in your gui. Whenever the user clicks on a button, you remove whatever was on the master panel add the corrseponding panel, then repaint the gui.
something like this:
if (e.getSource() == button){
masterPanel.removeAll();
masterPanel.add(newWindow);
this.setVisible(true);
this.repaint();
}

How to make a java swing popup window with combo box and 2 buttons?

I need to make a popup box with with a combo box and a couple of buttons. Please could someone advice on the best way to achieve this? I've had a look around and all I can find is alert boxes. Is this possible or will I need to create a whole new frame?
You can use the JOptionPane to achieve this. Please refer to the link below which explains this with sample code:
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html#input
JOptionPane.showInputDialog may be good enough if you are willing to leave how exactly the options are presented up to the UI.
I need to make a popup box with with a combo box and a couple of buttons
1) don't use another JFrame as popup window, use JFrame with JOptionPane/JDialog/JWindow these container are same as JFrame, but can take parent and owner
2) don't forget to setParent
3) depends if you needed decorated window then use JDialog, don't forget look for setModal() or ModalityTypes, if undecorated then use JWindow
4) don't create lots of JOptionPane/JDialog/JWindow on fly, becasue there Object are still in JVM memory, create this Container once and re-use that (by removing child) for another Action

Categories

Resources