I have a JTextField that is defined as follows:
JTextField chatTextField = new JTextField();
chatTextField.setRequestFocusEnabled(false);
chatTextField.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
chatTextField.requestFocusInWindow();
}
});
chatTextField.setFocusTraversalKeysEnabled(false);
chatTextField.setVisible(true);
There are some other method calls I have removed for simplicity purposes.
My chatTextField lives in a JPanel that is added to 'theDesktop'.
JFrame frame = new JFrame("Test");
JLayeredPane theDesktop = new JDesktopPane();
frame.getContentPane().add(theDesktop);
frame.setFocusable(true);
I have many other JPanels that are also added to 'theDesktop' that represent other windows (backpack, bank, etc.). I only want chatTextField to gain focus when I click the mouse into it (or when I press enter which I also have wired up via an action).
Sometimes it is getting focus, when closing other windows, and driving me insane. You are unable to close the chat panel or the button panel. Can anyone see anything that is wrong? I am having trouble coming up with SSCCE because my gui stuff is huge and this is a networked game. Any help would be greatly appreciated.
Here is how I solved this. Kinda hacky, but it works I suppose.
Anytime the focus was transferred to the chat text box (i.e., closing a certain window) I switched the focus to my button bar (always visible):
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
MyClient.buttonBarFrame.requestFocusInWindow(); //so chat does not request focus
}
});
Related
I'm relatively new to Java Swing in general and decided to use Intellij's GUI Designer. For those not familiar with it, Intellij creates 2 files for each GUI "screen". One is the "design" in the form of a .form file where you drag and drop UI components, and one is the "controller".
Anyway, I'm doing a 5-step questionnaire implemented on Java Swing. The first part asks the user what is their favourite fruit, and when a choice button is clicked, the choice is saved and the next JFrame appears with the next question and so on. JFrame1 transitioning to JFrame2 worked fine, and all UI components were shown. However, when I tried to transition from JFrame2 to JFrame3, JFrame3 showed up blank instead.
I've tried to call .pack() before setVisible(true), and then calling .toFront() after that, but that didn't help.
Below shows a section of my code. JFrame1, 2, and 3 all use the same exact code in its constructors and calling of the next JFrame. JFrame1 only differs by a single line which will be stated later in the code.
JFrame1.java
public class Frame1 extends JFrame {
private JPanel mainPanel;
private JLabel narrationLabel;
private JButton option1_btn;
private JButton option2_btn;
private JButton option3_btn;
public Frame1()
{
setTitle("Questionnaire");
setSize(1000, 1000);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame2 nextScreen = new Frame2 (); //declare the next screen to show
add(mainPanel); //this is only for the first starting JFrame
//JFrame2 and JFrame3 do not have this
//repeat this for buttons option2_btn and option3_btn
option1_btn.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
//swap screens to the next main filler selection screen
mainPanel.setVisible(false);
nextScreen.pack();
add(nextScreen.getPanel());
nextScreen.getPanel().setVisible(true);
}
});
}
public JPanel getPanel()
{
return mainPanel;
}
}
Main.java
public class Main
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame1 frame1 = new JFrame1(); //create start screen GUI
frame1.setVisible(true); //display GUI
}
});
}
}
My hunch tells me it might a concurrency issue, but I'm not sure if I am right, or where is the issue and how to resolve it. If a .form file is needed, I would gladly upload the code for it. Any help appreciated.
I develop a complex music application in java (8) based on the Netbeans RCP 8.2 and I have a strange problem that occurs randomly.
I have a JFrame with a panel which contains many JComponents. I use the panel's InputMap/ActionMap to capture "a","b",...,"g" key presses and call an action.
The action retrieves the key char then shows a JDialog which contains a JTextField used to edit some text data.
Before showing the dialog with dialog.setVisible(true), the action calls dialog.prepare(char key) so that the JDialog can initialize itself before being shown. Actually dialog.prepare(char key) only appends the passed char (converted to uppercase) in the JTextField.
This works most of the time: I press for example "c" in the JFrame then the JDialog appears with "C" at the end of the JTextField.
But sometimes, maybe 1/20 times, I get "Cc" at the end of the JTextfield !
It's like the original key press event (which comes from a JComponent in the JFrame's panel and was processed using InputMap/ActionMap) was also redundantly processed by the JDialog.
I verified that it's not a keyboard hardware problem. I reproduced the issue on a second computer with Win8 (mine is Win10).
I tried without success 1/ using a KeyListener instead of InputMap/ActionMap
and 2/ use java.awt.EventQueue.invokeLater() to append the key char in the JTextField.
I created a small independant app (see below) to reproduce the issue and facilitate debugging...but this small app works fine, I could not reproduce the issue :-( Then I compared again with my real app code, and it's really the same code, except the real app is a complete Netbeans RCP application.
So could it be that Netbeans RCP impacts the way Swing handle key events ? It looks odd to me...
I'm lost, any hint/suggested test would be greatly appreciated !
/**
* Try to reproduce double key problem... Failed because this works OK !! :-(
*/
public class PbKeyDouble extends JFrame {
MyDialog dialog;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
PbKeyDouble o = new PbKeyDouble();
o.setVisible(true);
}
});
}
public PbKeyDouble() {
// GUI INITIALIZATION
// Add a basic panel
JPanel panel = new JPanel();
getContentPane().add(panel);
panel.setPreferredSize(new Dimension(300, 200));
JButton button = new JButton("BUTTON");
panel.add(button);
// Button not used, it's only to simulate the real app where a component in the panel has the focus
button.requestFocusInWindow();
// If "A" or "B" key is pressed anywhere, MyAction.actionPerformed() will be called
panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("A"), "MyAction");
panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("B"), "MyAction");
panel.getActionMap().put("MyAction", new MyAction());
// Prepare JFrame
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
}
private class MyAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("EDT? " + SwingUtilities.isEventDispatchThread()); // Always prints TRUE
if (dialog == null) {
dialog = new MyDialog();
}
// Retrieve the key used to trigger the action
char c = e.getActionCommand().charAt(0);
// Prepare the dialog (insert the char)
dialog.prepare(c);
// Show dialog
dialog.setVisible(true);
}
}
private class MyDialog extends JDialog {
JTextField textfield;
/**
* A simple dialog with just a textfield.
*/
public MyDialog() {
textfield = new JTextField("Hello");
textfield.setColumns(100);
getContentPane().add(textfield);
pack();
setLocationRelativeTo(null);
}
/**
* Append the key (uppercased) at the end of the textfield
*/
public void prepare(char c) {
String text = textfield.getText();
textfield.setText(text + " " + Character.toUpperCase(c));
}
/**
* Overridden to add a global key binding on ESC key to exit the dialog.
* <p>
* This is only to facilitate the test where I need to try many times the process pressing "a" ESC "a" ESC etc.
*
* #return
*/
#Override
protected JRootPane createRootPane() {
JRootPane contentPane = new JRootPane();
contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ESCAPE"), "actionCancel");
contentPane.getActionMap().put("actionCancel", new AbstractAction("Cancel") {
#Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
return contentPane;
}
}
}
I found the problem, though it is still not logical to me. Explanation welcome !
All Swing components should be created and modified on the Event Dispatch Thread (EDT).
Yes, it was the case in my code and still it was not working...
To try to understand what happens I attached a KeyListener to the JTextField of the JDialog.
I found out that when it was working (key not doubled) my KeyListener received only a keyReleased() event. When it was not working (key doubled "Cc") my KeyListener received a keyTyped() event then keyReleased().
So I understand that AWT/Swing event handler mechanism "sends" each KeyEvent to the currently focused component (instead of to the component which the KeyEvent originates from). As I show the Dialog somewhere in the middle of the keyPressed/keyTyped/keyReleased sequence, sometimes the keyTyped was "wrongly" directed to the JTextField.
To solve this I executed the whole actionPerformed() code using SwingUtilities.invokeLater(), to make sure Dialog is shown after all EDT pending events are processed, and it seems to work so far...
I could find some good information in Java keybinding but what I don't understand is that it is recommended to use InputMap/ActionMap to avoid all the KeyListeners problems with focus changes etc. I used only InputMap/ActionMap and still it did not help...
So why InputMap does not react only to keyTyped() event ?
But sometimes, maybe 1/20 times, I get "Cc" at the end of the JTextfield !
Random issues are usually the result of threading issues.
All Swing components should be created and modified on the Event Dispatch Thread (EDT).
The code from your main() method is not executed on the EDT which could be the problem.
The code to create the GUI should be wrapped in a SwingUtilities.invokeLater(...).
Check out the Swing tutorial on Concurrency for more information.
I have this strange problem when I add buttons to the toolbar. I added action listener to one button added before the frame is shown and it works fine:
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
toolbar.add(new JButton("new button"));
}
});
I also added a piece of code that is supposed to add a new button after some plugins are loaded, and for some reason that piece of code does not work.
#Override
public void handle() {
System.out.println("Button added");
MainFrame frame = Application.getInstance().getMainFrame();
frame.getToolbar().add(new JButton("Plugin button"));
frame.getToolbar().revalidate();
frame.getToolbar().repaint();
System.out.println(frame.getToolbar().getComponents().length); // It is definitely being added, just not shown
}
The button is definitely being added, just not shown.
I would really appreciate any help since this thing is blocking me from progressing any further.
I found out what the problem was. The problem was that I instantiated MainFrame twice, first by calling Application constructor in main and then when calling Application.getInstance(), so all components added to the MainFrame were removed.
I am trying to make a refresh button that will essentially restart the program when ever I click the button. I don't know how I should go about doing this.
I've place the Graphical User Interface i decided to use do complete this action. Any and all help would be greatly appreciated.
package pdfView;
import javax.swing.*;
import java.awt.*;
public class View extends JFrame {
public View() {
super("PDF Viewer");
setLookAndFeel();
setSize(500, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JTextField Search = new JTextField ("Search", 29);
JButton Search1 = new JButton("Search");
//this is where i have the button
JButton ReFresh = new JButton("ReFresh");
add(Search);
add(Search1);
add(ReFresh);
setVisible(true);
}
private void setLookAndFeel() {
try {
UIManager.setLookAndFeel(
"com.sun.java.squing.plaf.nimbus.NimbusLookAndFeel"
);
} catch (Exception exc){
}
}
public static void main(String[] args) {
View pdf = new View();
}
}
What do you mean by refresh or restart?
Do you mean:
Let the application be as it is, just update what it's showing?
Really restart the application?
Updating what the application is showing
You first need to decide what actually should cause your application to refresh. You already talked about a Button. The mechanism for activating something like a button is called Action. You can do that stuff manually, using an ActionListener, or you could extend AbstractAction, which is what I recommend. Extending AbstractAction allows you to use the same logical action something in more than one location on the UI. Look at typical applications, they offer Cut/Copy/Paste through menu, toolbar, popupmenu and keyboard shortcuts. The simplest way to achieve this in Java is using Action by extending AbstractAction.
The methods you need to call to update your application are invalidate(), validate() or repaint().
Restarting an application
So you want to run through main() again? That should actually not be required, unless you have an application that supports updating itself. Even then it can sometimes be avoided by smart usage of a ClassLoader.
Some more notes on your code
Usage by extension anti-pattern
I wouldn't extend JFrame just to display a window on the screen. Usage by extension is an anti-pattern. You don't need to extend JFrame to get a JFrame displayed on the screen and do what you want.
Referring static members
I would refer to constants via their original declaration. I.e. I'd refer to EXIT_ON_CLOSE via WindowConstants.EXIT_ON_CLOSE, not JFrame.EXIT_ON_CLOSE.
Typo
You have a typo in your UIManager.setLookAndFeel() code. Search for swing and you will see the typo.
Exception information
You might actually want to print the exception to stderr using exc.printStackTrace() instead of ignoring it completely, because when you have a typo in the LaF class name, as you do, and you don't print the exception, you might actually not come to know what's going wrong.
Sequence of widget construction and UIManager.setLookAndFeel()
The sequence of UIManager.setLookAndFeel() and the effective new JFrame() via super(...) does not guarantee you that the whole UI will be in Nimbus, parts of it might still be in Metal. I recommend to set the LaF before even constructing the first widget, to be on the safe side. As far as I remember, it's not guaranteed that changing the LaF after component construction has an effect, unless you tell the UIManager to update the LaF. See also this quote from the documentation of UIManager:
Once the look and feel has been changed it is imperative to invoke updateUI on all JComponents. The method SwingUtilities.updateComponentTreeUI(java.awt.Component) makes it easy to apply updateUI to a containment hierarchy. Refer to it for details. The exact behavior of not invoking updateUI after changing the look and feel is unspecified. It is very possible to receive unexpected exceptions, painting problems, or worse.
http://docs.oracle.com/javase/8/docs/api/javax/swing/UIManager.html
setSize() vs. pack() with a little help of Insets and Border
Instead of setting the size manually, you might want to play with Insets or Border and JFrame.pack() in order to get a decent layout of your window. Setting the size manually assumes that you know a lot about the screen resolution and the font size of the user.
The pack() method performs automatic size calculation based on the contents. Insets and Border allow you to create some space and borders, even with some designs or labels, around components so they wouldn't be cramped tightly in a window but be nicely spaced.
First you have to assign an actionListener to the ReFresh Jbutton.
You can either implement the interface ActionListener to the class, and override the actionPerformed() method like this
public class View extends JFrame implements ActionListener{
private JButton ReFresh;
public View() {
super("PDF Viewer");
setLookAndFeel();
setSize(500, 125);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout flo = new FlowLayout();
setLayout(flo);
JTextField Search = new JTextField ("Search", 29);
JButton Search1 = new JButton("Search");
//this is where i have the button
ReFresh = new JButton("ReFresh");
ReFresh.addActionListener(this);
add(Search);
add(Search1);
add(ReFresh);
setVisible(true);
}
private void setLookAndFeel() { //right way for nimbus: http://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/nimbus.html
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void actionPerformed(ActionEvent e)
{
if(e.equals(ReFresh))
{
super.repaint();
}
}}
public static void main(String[] args) {
View pdf = new View();
}
Or you can do inline assignment to addActionListener, like this
ReFresh.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
super.repaint();
}
});
You can try these methods to refresh/reload the JFrame,
invalidate();
validate();
repaint();
you can also use dispose(); and then new View(); to create the new JFrame, but in this sequence it will close the window and create new one.
or you can even try setVisible(false); then setVisible(true);
I recommend the first 3.
I've looked at other JLabel threads and though similar, a few just don't seem to apply to what I'm experiencing. First, I want to say I am a novice when it comes to Java. Next, I am trying to follow tutorials and demos at the docs.oracle.com site. Now, I can update the label when I type something into a JTextField and there is an ActionListener on that...
But I also have a Menu, and when I select a Menu Item, that Action does not want to update the label.
Questions -
How do I have action listeners on both JTextFields and JMenuItems? Are there two ActionEvent methods or do I use one method and somehow identify each type of action?
If I use the same basic code in the JTextField ActionEvent and JMenuItem ActionEvent, the JLabel updates correctly with the JTextField event but not JMenuItem event. They both use the messageField.setText property. Could the JMenuItem action be doing something to block the setText?
I can easily copy code in here, but it's pretty spaghetti-like at the moment, so if you want to see anything, let me know specifically and I'll post it.
I would appreciate any assistance that anyone would be able to provide.
---edit---
Wow!! Thanks for all of the comments and suggestions.
1 - I know it has to be my code. As I mentioned, I am really just cobbling stuff together from demos and tutorials, and trying to learn Java along the way. I've just never gotten the hang of object oriented....
2 - I do know the individual Listeners are working. I'm using System.out.println to validate, as well as inspecting those labels in debug mode to see they have indeed changed.
3 - I will look at the various links and code posted here and see if I can figure out what's wrong with my code.
Really, thanks again!
---edit---
Here is what I originally had in my createAndShowGUI method....
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Create XML for Photo Gallery");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CreateGalleryXML window = new CreateGalleryXML();
frame.setJMenuBar(window.createMenuBar());
frame.add(new CreateGalleryXML());
frame.pack();
frame.setVisible(true);
}
Seems like you yourself are doing something wrong, in your code. Without a proper SSCCE it's hard to say what thing you doing wrong, since it works perfectly, using same ActionListener for both JMenuItem and JTextField.
Here is a sample program to match with yours :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class UpdateLabel
{
private JLabel label;
private String labelText;
private ActionListener action = new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
setLabelText((String) ae.getActionCommand());
label.setText(getLabelText());
}
};
private void setLabelText(String text)
{
labelText = text;
}
private String getLabelText()
{
return labelText;
}
private void createAndDisplayGUI()
{
final JFrame frame = new JFrame("Update Label");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationByPlatform(true);
JMenuBar menuBar = new JMenuBar();
JMenu programMenu = new JMenu("Program");
JMenuItem exitMenuItem = new JMenuItem("Exit");
exitMenuItem.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
frame.dispose();
}
});
JMenu labelMenu = new JMenu("Label");
JMenuItem updateMenuItem = new JMenuItem("Update Label");
updateMenuItem.setActionCommand("Updated by JMenuItem");
updateMenuItem.addActionListener(action);
programMenu.add(exitMenuItem);
labelMenu.add(updateMenuItem);
menuBar.add(programMenu);
menuBar.add(labelMenu);
frame.setJMenuBar(menuBar);
JPanel contentPane = new JPanel();
label = new JLabel("I am the LABEL which will be updated!!");
contentPane.add(label);
JTextField tfield = new JTextField(10);
tfield.setActionCommand("Updated by JTextField");
tfield.addActionListener(action);
frame.add(contentPane, BorderLayout.CENTER);
frame.add(tfield, BorderLayout.PAGE_END);
frame.pack();
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new UpdateLabel().createAndDisplayGUI();
}
});
}
}
And here is the output in both the cases :
and
Do check out the main method, might be you had failed to put your code inside EDT - Event Dispatcher Thread, that can lead to such issues too. All updates on the Swing GUI, must be done on the Event Dispatcher Thread.
LATEST EDIT
It seems to me that CreateGalleryXML extends JPanel by the look of it. See at Line 3 of this below code taken from your update, here you are initializing a new Object of CreateGalleryXML inside, when you already had one Object window created at Line 1:
CreateGalleryXML window = new CreateGalleryXML();
frame.setJMenuBar(window.createMenuBar());
frame.add(new CreateGalleryXML());
So try to change the above thingy to this
CreateGalleryXML window = new CreateGalleryXML();
frame.setJMenuBar(window.createMenuBar());
frame.add(window);
and see what happens and Please do revert back again :-)
Use Action to encapsulate common functionality that must be shared by menus and related components. See this example that extends AbstractAction.
Pretty much everything you could need to know is in the Java tutorials. Down the bottom they have demo's on how to do both menu's and text fields. They include source code to look at as well.
There's not much more I can say that they don't say better, but in answer to your questions:
Both, you can have separate listener's on each component, or one that figures out what component is responsible for causing the action event. I would suggest you use separate ones for each thing.
Can't really say with out seeing the code, are you sure that the action on the JMenu is even firing? Does it print something to the console etc if you use System.out.println somewhere?