是否可以创建一个对话框,用户可以从列表中选择值,还可以编辑文本? (类似于带有可编辑JComboBox的对话框。)
答案 0 :(得分:2)
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
class ShowBothInputs {
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
String[] items = {
"Apple",
"Banana",
"Grape",
"Cherry"
};
// what was requested
EditableListPanel elp = new EditableListPanel(items);
JOptionPane.showMessageDialog(null, elp);
System.out.println( "EditableListPanel value: " + elp.getValue() );
// probably what this UI actually needs
JComboBox jcb = new JComboBox(items);
jcb.setEditable(true);
JOptionPane.showMessageDialog(null, jcb);
System.out.println( "JComboBox value: " + jcb.getSelectedItem() );
}
};
SwingUtilities.invokeLater(r);
}
}
class EditableListPanel extends JPanel {
private JTextField value;
EditableListPanel(String[] items) {
super( new BorderLayout(5,5) );
final JList list = new JList( items );
list.addListSelectionListener( new ListSelectionListener(){
public void valueChanged(ListSelectionEvent lse) {
value.setText( (String)list.getSelectedValue() );
}
} );
add( list, BorderLayout.CENTER );
value = new JTextField("", 20);
add( value, BorderLayout.NORTH );
}
public String getValue() {
return value.getText();
}
}
答案 1 :(得分:0)
JDialog通常用于向用户显示通知。我认为,如果你想要这样的行为,你将不得不创建一个简单的JFrame来满足你的需要。
答案 2 :(得分:0)
不确定“选择后编辑文字”是什么意思。
但是,您可以使用JOptionPane轻松创建简单对话框。您可以将JPanel添加到选项窗格。查看Dialog Focus了解详情。