How to put a dropdown list that has the list of every string, and when I select a item on that list then push the load button it will only show up what’s on that String. Here’s my code, I actually put the number of the string and and show the String’s data using while statements.
How can I actually put a Dropdown list and it’s content will be a number registered on every string. Just like this
1 231231
2 123124
3 123124
4 232312
If I select 4 and press “Load” it will show “232312”
and everytime I save a data a unique number will be registered in every line, just like
“4” is the unique no. and 232312 is it’s data
package datasaving;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class Datasaving {
/**
* @param args the command line arguments
* @throws FileNotFoundException
* @throws IOException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
JPanel panel = new JPanel();
JFrame frame = new JFrame();
final JTextField input0 = new javax.swing.JTextField(20);
final JTextField input1 = new javax.swing.JTextField(20);
final JTextField out = new javax.swing.JTextField(20);
final JTextField line = new javax.swing.JTextField(20);
JButton save = new javax.swing.JButton("Save");
JButton load = new javax.swing.JButton("Load");
frame.add(panel);
frame.setSize(240,200);
panel.add(input0);
panel.add(input1);
panel.add(save);
panel.add(line);
panel.add(out);
panel.add(load);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
save.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file = new File("data.dat");
try {
try (FileWriter writer = new FileWriter(file, true)) {
String data0 = input0.getText();
String data1 = input1.getText();
writer.write(data0+":"+data1+"\n");
}
System.out.println("Data Saved");
} catch (IOException | HeadlessException z) {
JOptionPane.showMessageDialog(null, e);
}
}
});
load.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int lines = Integer.parseInt(line.getText());
try {
FileInputStream fs= new FileInputStream("data.dat");
BufferedReader br = new BufferedReader(new InputStreamReader(fs));
for(int i = 0; i < lines; ++i) {
br.readLine();
}
out.setText(br.readLine());
JOptionPane.showMessageDialog(null, "Loaded");
} catch ( IOException | HeadlessException es) {
JOptionPane.showMessageDialog(null, e);
}
}
});
}
}
for example:
John blahblahblahblah
Keith blahblahblahblah
Joe blahblahblahblah
Kenneth blahblahblahblah
Christian blahblahblahblah
The first word “Names” will be added to JList or JComboBox
how to make the names a Array. I know how to use .split(); but I don’t know how to make that happen in every lines in the file
1) JTextField doesn’t support multi-line. Try JTextArea instead. With text areas, you can use myTextArea.append() to add lines as you read them.
2) But actually, it sounds like a JComboBox or a JList might be what you’re really looking for:
3) Your basic program looks OK
‘Hope that helps!