Swing JFormattedTextField 更正多行粘贴
Swing JFormattedTextField correcting multiline pasting
JFormattedTextField
有一个错误。它不支持多行输入,但允许粘贴多行文本。我知道如何禁用 copy/paste 功能,但这不是我想要的。是否可以拦截 "paste" 操作并从传入字符串中删除所有 \n
?
漏洞证明:
package com;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new SimpleFTF());
f.pack();
f.setVisible(true);
}
public static class SimpleFTF extends JPanel {
public SimpleFTF(){
JFormattedTextField field = new JFormattedTextField(" ");
add(field, java.awt.BorderLayout.CENTER);
}
}
}
尝试粘贴:
1
2
您可以在 JFormattedTextField 上编写一个事件,这样如果粘贴了某些内容,它会删除 \n ascii 是 10 个字符并更新该字段。
/*Note This code works but if initial condition when you execute the code it will throw exception the the code works
Sorry but I don't know much about swing
*/
package com;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class SwingTest
{
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new SimpleFTF());
f.pack();
f.setVisible(true);
}
public static class SimpleFTF extends JPanel
{
public SimpleFTF()
{
JFormattedTextField field = new JFormattedTextField(" ");
field.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
modify();
}
@Override
public void removeUpdate(DocumentEvent e) {
}
@Override
public void changedUpdate(DocumentEvent e) {
}
public void modify()
{
String text=field.getText();
if(text.contains("\n"))
text=text.replaceAll("\n", "");
text=text.trim();
field.setText(text);
}
});
add(field, java.awt.BorderLayout.CENTER);
}
}
}
JFormattedTextField
有一个错误。它不支持多行输入,但允许粘贴多行文本。我知道如何禁用 copy/paste 功能,但这不是我想要的。是否可以拦截 "paste" 操作并从传入字符串中删除所有 \n
?
漏洞证明:
package com;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingTest {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new SimpleFTF());
f.pack();
f.setVisible(true);
}
public static class SimpleFTF extends JPanel {
public SimpleFTF(){
JFormattedTextField field = new JFormattedTextField(" ");
add(field, java.awt.BorderLayout.CENTER);
}
}
}
尝试粘贴:
1
2
您可以在 JFormattedTextField 上编写一个事件,这样如果粘贴了某些内容,它会删除 \n ascii 是 10 个字符并更新该字段。
/*Note This code works but if initial condition when you execute the code it will throw exception the the code works
Sorry but I don't know much about swing
*/
package com;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
public class SwingTest
{
public static void main(String[] args)
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(new SimpleFTF());
f.pack();
f.setVisible(true);
}
public static class SimpleFTF extends JPanel
{
public SimpleFTF()
{
JFormattedTextField field = new JFormattedTextField(" ");
field.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
modify();
}
@Override
public void removeUpdate(DocumentEvent e) {
}
@Override
public void changedUpdate(DocumentEvent e) {
}
public void modify()
{
String text=field.getText();
if(text.contains("\n"))
text=text.replaceAll("\n", "");
text=text.trim();
field.setText(text);
}
});
add(field, java.awt.BorderLayout.CENTER);
}
}
}