What I have written (with some help from a few of the forum members here) is a JComboBox which gets its string list from a txt file.
Now, under that JComboBox, I have an “edit” button, which adds items to this text file. The problem is that I need to reload/re-read the txt file so that the JComboBox is updated and the new addition is made viewable. The trick is I want to do this without destroying and recreating the window. Any ideas? I’ve exhausted what little I know.
Sorry for the lenght of code. I have highlighted the places that pertain to the main problem.
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class data_entry extends JFrame{
private String govt_name_selection = "";
//which ComboBox to refresh
[color=blue]private static String edit_button_pressed = "";[/color]
public void data_entry() {
//---start govt_dept_dept panel----
//Govt_name DDM (Drop Down Menu)
JLabel govt_nameL = new JLabel("Government Name:");
[color=blue]JComboBox govt_nameCB = new JComboBox(open_dropdownlist("F:\\\\DDM_govt_name.txt"));[/color]
govt_nameCBactn actn_govt_nameCB = new govt_nameCBactn();
govt_nameCB.addActionListener([color=blue]actn_govt_nameCB[/color]);
JButton govt_editB = new JButton("Edit");
govt_editBactn actn_govt_editB = new govt_editBactn();
govt_editB.addActionListener(actn_govt_editB);
JPanel govt_editBP = new JPanel();
govt_editBP.setLayout(new FlowLayout());
govt_editBP.add(govt_editB);
JPanel govtP = new JPanel();
govtP.setLayout(new BorderLayout());
govtP.add(govt_nameL, BorderLayout.NORTH);
govtP.add(govt_nameCB, BorderLayout.CENTER);
govtP.add(govt_editBP, BorderLayout.SOUTH);
JPanel govt_dept_unitP = new JPanel();
govt_dept_unitP.setLayout(new FlowLayout());
govt_dept_unitP.add(govtP);
//----end govt_dept_unit panel----
//main container
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
pane.add(govt_dept_unitP, BorderLayout.CENTER);
}
//----start drop down menu string list template------
public Object[] open_dropdownlist(String dir_path){
java.util.List list = new ArrayList();
try {
String line = "";
BufferedReader in = new BufferedReader(new FileReader(dir_path));
while((line = in.readLine()) != null){
list.add(line);
}
in.close();
}
catch(IOException ioeRef) {
System.out.println("Exception " + ioeRef.toString());
JOptionPane.showConfirmDialog(null, "Drop down menu database not found.", "Program Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
return list.toArray(new String[]{});
}
//----end drop down menus string list template------
//-----start drop down menu actions-----
//govt name action
private class govt_nameCBactn implements ActionListener {
public void actionPerformed(ActionEvent e) {
//grabs CB selection
JComboBox box = (JComboBox)e.getSource();
String CB_name = (String)box.getSelectedItem();
govt_name_selection = make_CB_selection(CB_name, "F:\\\\DDM_govt_name.txt");
}
}
//set JComboBox value (template)
//first entry in drop down menu txt file is the label name, which = "";
private String make_CB_selection(String CB_name, String dir_path){
String line = "";
String matched_line = "";
try{
BufferedReader infile = new BufferedReader(new FileReader(dir_path));
line = infile.readLine();
while((line = infile.readLine()) != null){
//if match found between line read from file and CB selection
if (line.equalsIgnoreCase(CB_name)){
matched_line = line;
}
}
infile.close();
}
catch(IOException ioeRef) {
System.out.println("Exception " + ioeRef.toString());
JOptionPane.showConfirmDialog(null, "Error: match for " + dir_path + " not found in database.", "Program Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
return matched_line;
}
//----end drop down menu actions-----
//-----start drop down edit buttons-----
private class [color=blue]govt_editBactn[/color] implements ActionListener {
public void actionPerformed(ActionEvent e) {
DDM_edit_button edit = new DDM_edit_button();
edit.DDM_edit_button("F:\\\\DDM_govt_name.txt");
edit.setTitle("Edit Government Name List");
edit.setSize(380, 140);
edit.setVisible(true);
[color=blue]//which CB list to refresh
edit_button_pressed = "govt_nameCB";[/color]
}
}
//-----end drop down edit buttons-----
[color=blue]//----redraw drop down list
public void redrawlist(String dir_path){
if(edit_button_pressed.equalsIgnoreCase("govt_nameCB")){
JComboBox govt_nameCB = new JComboBox(open_dropdownlist("F:\\\\DDM_govt_name.txt"));
}
else{
JOptionPane.showConfirmDialog(null, "Error reloading dropdown menu" + edit_button_pressed + ". Please logout and log back in.", "Program Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
}[/color]
}
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
public class DDM_edit_button extends JFrame implements ActionListener{
private JTextField new_entryTF = new JTextField(30);
private String dir_path = "";
public void DDM_edit_button(String dir_pathIN){
dir_path = dir_pathIN;
//close this window
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
//text field
JPanel new_entryTFP = new JPanel();
new_entryTFP.setLayout(new FlowLayout());
new_entryTFP.add(new_entryTF);
//button
JButton new_entryB = new JButton("Add Entry");
new_entryB.addActionListener(this);
JButton close = new JButton("Close");
close.addActionListener(this);
JPanel new_entryBP = new JPanel();
new_entryBP.setLayout(new FlowLayout());
new_entryBP.add(new_entryB);
new_entryBP.add(close);
JPanel mainP = new JPanel();
mainP.setLayout(new BorderLayout());
mainP.add(new_entryTFP, BorderLayout.CENTER);
mainP.add(new_entryBP, BorderLayout.SOUTH);
Container pane = getContentPane();
pane.setLayout(new FlowLayout());
pane.add(mainP);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Add Entry")){
String entry = "";
entry = new_entryTF.getText();
try {
boolean entry_exists = false;
BufferedReader infile = new BufferedReader(new FileReader(dir_path));
String line;
if(entry.equalsIgnoreCase(""))
JOptionPane.showConfirmDialog(null, "Please input an entry.", "Entry Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
else{
while((line = infile.readLine()) != null){
if (line.equalsIgnoreCase(entry)){
entry_exists=true;
}
}
infile.close();
}
if (!entry_exists){
//write entry to file
FileWriter outfile = new FileWriter(dir_path, true);
outfile.write(entry + "\
");
outfile.close();
//confirm message
JOptionPane.showConfirmDialog(null, "The entry has been successfully added.", "User Added", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
//reset TF
entry = "";
new_entryTF.setText("");
[color=blue]//redraw DDM
data_entry redrawDDM = new data_entry();
redrawDDM.data_entry();
redrawDDM.redrawlist(dir_path);[/color]
}
else{
JOptionPane.showConfirmDialog(null, "Entry already exists.", "Entry Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
} //end try
catch(IOException ioeRef) {
System.out.println("Exception " + ioeRef.toString());
JOptionPane.showConfirmDialog(null, "Error: match for " + dir_path + " not found in database.", "Program Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
dispose();
}
}//end add entry button
if(e.getActionCommand().equals("Close"))
dispose();
}//end actionlistener
}