
This tutorial addresses an issue where a "Windows Background Sound" (beep) plays when pressing the Enter key within a non-editable JEditorPane in Java Swing. It provides a solution to disable this sound by removing the default Action associated with the Enter key, allowing you to handle Enter key presses without the unwanted audio feedback.
When using a JEditorPane with HTML content in Java Swing, you might encounter an unexpected behavior: pressing the Enter key triggers the system's default "Windows Background Sound" or beep. This occurs specifically when the JEditorPane is set to non-editable mode (htmlLabel.setEditable(false)). The reason is that the JEditorPane has a default Action associated with the Enter key. When the pane is not editable, this action triggers the system beep as a form of error feedback.
The most straightforward solution is to remove the default Action associated with the Enter key. This can be achieved using the getInputMap() method of the JEditorPane and setting the corresponding action to "none".
Here's the code snippet:
立即学习“Java免费学习笔记(深入)”;
htmlLabel.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "none");This line of code effectively tells the JEditorPane to ignore the default action associated with the Enter key press.
Here's a complete example demonstrating how to implement this solution:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.KeyStroke;
public class App {
public static void main(String[] args) {
Dimension frameDimension = new Dimension(600, 400);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setMinimumSize(frameDimension);
frame.setSize(frameDimension);
frame.setBackground(Color.white);
// Create HTML Editor Pane
JEditorPane htmlLabel = new JEditorPane("text/html", "");
htmlLabel.getInputMap().put(KeyStroke.getKeyStroke("pressed ENTER"), "none");
htmlLabel.setEditable(false);
htmlLabel.setBackground(Color.WHITE);
htmlLabel.setFont(new Font(htmlLabel.getName(), Font.PLAIN, 14));
htmlLabel.setVisible(true);
// Add the JEditorPane to the frame
frame.add(htmlLabel);
htmlLabel.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
// If Enter is pressed
if (e.getKeyCode() == 10) {
// DO STUFF
System.out.println("ENTER");
}
}
@Override
public void keyReleased(KeyEvent e) {
}
});
frame.setResizable(false);
frame.setVisible(true);
}
}In this example:
Instead of removing the default action, you could also create a custom Action to replace it. This approach gives you more control over what happens when the Enter key is pressed. However, for simply disabling the sound, removing the default action is the simpler and more efficient solution.
KeyListener Still Works: Even after removing the default Action, your KeyListener will still function correctly. This means you can still detect and handle Enter key presses within your application logic.
Other Key Bindings: The JEditorPane might have other key bindings that you are unaware of. You can iterate through all registered key bindings using the following code:
for (KeyStroke ks : htmlLabel.getInputMap().allKeys()) {
System.out.println(ks);
}By removing the default Action associated with the Enter key in a non-editable JEditorPane, you can effectively disable the unwanted "Windows Background Sound" and maintain control over how Enter key presses are handled within your application. This approach provides a clean and efficient solution to a common issue encountered when working with JEditorPane components in Java Swing. Remember to consider other key bindings if you need to customize the behavior of other keys as well.
以上就是Disabling Windows Background Sound on Enter Key Press in Java Swing JEditorPane的详细内容,更多请关注php中文网其它相关文章!
Windows激活工具是正版认证的激活工具,永久激活,一键解决windows许可证即将过期。可激活win7系统、win8.1系统、win10系统、win11系统。下载后先看完视频激活教程,再进行操作,100%激活成功。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号