I want to create editable JList
. This is my sample code:
package okienko;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
class Frane extends JFrame {
private JPanel contentPane;
private JList<String> list;
private JLabel lblLabel;
private DefaultListModel<String> model;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frane frame = new Frane();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Frane() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 0};
gbl_contentPane.rowHeights = new int[]{0, 0, 0};
gbl_contentPane.columnWeights = new double[]{1.0, Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);
lblLabel = new JLabel("LABEL");
GridBagConstraints gbc_lblLabel = new GridBagConstraints();
gbc_lblLabel.insets = new Insets(0, 0, 5, 0);
gbc_lblLabel.gridx = 0;
gbc_lblLabel.gridy = 0;
contentPane.add(lblLabel, gbc_lblLabel);
JScrollPane scrollPane = new JScrollPane();
GridBagConstraints gbc_scrollPane = new GridBagConstraints();
gbc_scrollPane.fill = GridBagConstraints.BOTH;
gbc_scrollPane.gridx = 0;
gbc_scrollPane.gridy = 1;
contentPane.add(scrollPane, gbc_scrollPane);
model = new DefaultListModel<String>();
list = new JList<String>(model);
JTextField field = new JTextField("TEXT");
field.setEditable(true);
model.addElement(field.getText());
scrollPane.setViewportView(list);
list.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getExtendedKeyCode() == 10) {
JTextField tf = new JTextField("A");
model.addElement(tf.getText());
}
}
});
}
}
So I want to have a list
where I could enter new lines where each line will be a separate JTextField
. My code creates a list
with one textfield
and after ENTER
key is pressed next JTextField
is added. But it shows only the text passed to the constructor of JTextField
.
After I press ENTER
key, I want to write my own text, and when I press ENTER
key again, I want a new field to be created.
Also I want to switch between fields by clicking the mouse and to edit these fields.
Is it possible to create this kind of a JList
? Maybe there are other solutions to my problem. I'm open for suggestions.
When new item(some kind of a Object
) is added to a JList
, that Object
's toString()
method is called. And the resulting String
is showed in the JList
as a JLabel
. So you cannot directly add a JTextField
to a JList
. Because toString()
of JTextField
wont return any readable information.
If you want to add JTextField
s inside a JList
you will have to create your List
class by extending JList
and you will have to modify it to add JTextField
as items instead of JLabel
s. But I wont think it would be a simple task to you and it would be so messy..
Simplest way to do your task is to have a JTextField
in the JFrame
and when you type a text and press enter
, that text will appear as a item in the JLabel
.
And to edit items you can have another JTextField
. You can make it hidden. And when you double click on an item in the JList
, it will be visible. You can edit the text there and after you press enter, that item will be updated and that JTextField
will be hidden again..
I think this might be helpful to you..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.