How to call the JTable function again - java

I'm trying to make a GUI interface using java swing that is able to add users to a text file that I treat as a database. The GUI interface has a JTable which pulls the existing items in the text file and displays it initially. Once I go to add a new item, it gets updated on the text file but not on the JTable. This is mainly because I only call the JTable once the way I set up my code. I'm wondering how I can call the JTable multiple times or make a button for it to call a refresh of the table and display the newly added items alongside the old items.
I want it to be able to either call the refresh of the table in the updateList function or once the viewButton is pressed which triggers an else if statement which can do that in the actionPerformed function.
Here is my code:
class UserManager extends JFrame implements ActionListener {
private JTextField firstNameField, lastNameField, salaryField;
private JButton addButton, removeButton, viewButton, sortButton, getButton;
private JList<Employee> userList;
private ArrayList<Employee> users;
public UserManager() {
setTitle("Employee Manager");
setSize(300, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
firstNameField = new JTextField(20);
lastNameField = new JTextField(20);
salaryField = new JTextField(20);
addButton = new JButton("Add");
addButton.addActionListener(this);
removeButton = new JButton("Remove");
removeButton.addActionListener(this);
viewButton = new JButton("Refresh List"); // POTENTIAL BUTTON TO REFRESH JTABLE?
viewButton.addActionListener(this);
sortButton = new JButton("Sort List");
sortButton.addActionListener(this);
// Pulling data from text file database
ArrayList<ArrayList<String>> databaseData = ReadFile();
users = new ArrayList<Employee>();
// Adding existing databaseData to users
for (int i = 0; i < databaseData.size(); i++) {
Employee user = new Employee(databaseData.get(i).get(0), databaseData.get(i).get(1), Integer.valueOf(databaseData.get(i).get(2)));
users.add(user);
}
userList = new JList<Employee>(users.toArray(new Employee[0]));
userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel firstNamePanel = new JPanel();
firstNamePanel.add(new JLabel("First Name:"));
firstNamePanel.add(firstNameField);
JPanel lastNamePanel = new JPanel();
lastNamePanel.add(new JLabel("Last Name:"));
lastNamePanel.add(lastNameField);
JPanel salaryPanel = new JPanel();
salaryPanel.add(new JLabel("Salary:"));
salaryPanel.add(salaryField);
JPanel buttonPanel = new JPanel();
buttonPanel.add(addButton);
buttonPanel.add(removeButton);
buttonPanel.add(viewButton);
buttonPanel.add(sortButton);
// Converting 2D arraylist to normal 2D array for JTable
String[][] data = databaseData.stream().map(u -> u.toArray(new String[0])).toArray(String[][]::new);
// Initializing column names of JTable
String[] columnNames = { "FName", "LName", "Salary" };
// Initializing the JTable (THIS IS WHAT I NEED TO REFRESH)
JTable j = new JTable(data, columnNames);
j.setBounds(1000, 1000, 900, 900);
// adding it to JScrollPane
JScrollPane table = new JScrollPane(j);
JPanel mainPanel = new JPanel(new GridLayout(5, 3));
mainPanel.add(firstNamePanel);
mainPanel.add(lastNamePanel);
mainPanel.add(salaryPanel);
mainPanel.add(buttonPanel);
mainPanel.add(table);
add(mainPanel);
}
public void actionPerformed(ActionEvent e) {
// "Add" button is clicked
if (e.getSource() == addButton) {
String firstName = firstNameField.getText();
String lastName = lastNameField.getText();
int salary = 0;
try {
salary = Integer.parseInt(salaryField.getText());
}
catch (NumberFormatException nfe) {
JOptionPane.showMessageDialog(this, "Please enter a valid salary", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Error check to see if full name and age is entered
if (!firstName.equals("") && !lastName.equals("")) {
Employee user = new Employee(firstName, lastName, salary);
users.add(user);
updateList();
firstNameField.setText("");
lastNameField.setText("");
salaryField.setText("");
}
else {
JOptionPane.showMessageDialog(this, "Please enter a valid full name", "Error", JOptionPane.ERROR_MESSAGE);
}
}
// POTENTIAL BUTTON THAT REFRESHES JTABLE?
else if (e.getSource() == viewButton) {
}
}
// Updates the list after a CRUD operation is called
private void updateList() {
userList.setListData(users.toArray(new Employee[0]));
try {
FileWriter fw = new FileWriter("db.txt", false);
// BufferedWriter bw = new BufferedWriter(fw);
// PrintWriter pw = new PrintWriter(bw);
// Loop through each student and write to the text file
for (int i = 0; i < users.size(); i++) {
fw.write(toString(users.get(i).getFirstName(), users.get(i).getLastName(), users.get(i).getSalary()));
}
fw.close();
// CALL A REFRESH FOR THE TABLE HERE??
}
catch (IOException io) {
}
}
// Combing multiple string and ints into a string
public String toString(String firstName, String lastName, int salary) {
return firstName + ", " + lastName + ", " + salary + "\n";
}
// Reading database
public static ArrayList<ArrayList<String>> ReadFile() {
try {
// Choose grades.txt file to look at
File myObj = new File("db.txt");
// Create scanner object
Scanner myReader = new Scanner(myObj);
// Create 2d list array to hold all the single list arrays of single information
ArrayList<ArrayList<String>> combinedArr = new ArrayList<ArrayList<String>>();
// While the file reader is still reading lines in the text
while (myReader.hasNextLine()) {
// Read strings of text in txt file
String data = myReader.nextLine();
// Get first and last name from a string
ArrayList<String> temp = GetName(data);
// Add the person and their salary to the combined array that holds everyones
combinedArr.add(temp);
}
// Close file once there are no more lines to read
myReader.close();
return combinedArr;
}
catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
// Return invalid list string with nothing if error
ArrayList<ArrayList<String>> Invalid = new ArrayList<ArrayList<String>>();
return Invalid;
}
// Parses name in db
public static ArrayList<String> GetName(String data) {
String first = "";
String last = "";
String sal = "";
// System.out.println(data[0])
for (int i = 0; i < data.length(); i++) {
if (data.charAt(i) == ',') {
// Start from 2 indexes after the first occurance of the comma
for (int j = i+2; j < data.length(); j++) {
if (data.charAt(j) == ',') {
for (int n = j+2; n < data.length(); n++) {
sal += data.charAt(n);
}
break;
}
last += data.charAt(j);
}
break;
}
first += data.charAt(i);
}
// Initializing package array to send all values
ArrayList<String> arr = new ArrayList<String>();
arr.add(first);
arr.add(last);
arr.add(sal);
return arr;
}
public static void main(String[] args) {
UserManager frame = new UserManager();
frame.setVisible(true);
}
}
class Employee {
// Initalizing variables
private String firstName;
private String lastName;
private int salary;
// Assigning variables
public Employee(String firstName, String lastName, int salary) {
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
// return first name
public String getFirstName() {
return firstName;
}
// return last name
public String getLastName() {
return lastName;
}
// return salary
public int getSalary() {
return salary;
}
}

Initializing the JTable (THIS IS WHAT I NEED TO REFRESH)
You don't refresh the table. The data for all Swing components is contained in a "model". You change the data in the model. When changes to the model are made the table is repainted to reflect the change.
So you could either:
create a new DefaultTableModel (which is unnecessary)
update the existing DefaultTableModel, by using the addRow(...) method to update the model every time you write a new item to your file.
Instead of using code like:
JTable j = new JTable(data, columnNames);
You can use:
model = new DefaultTableModel(data, columnNames)
table = new JTable(model)
Now you can access the DefaultTableModel any time you need to by creating an instance variable in your class or by using the table.getModel() method.

Usually lists and tables are updated through their models. They represent the information shown to the user. You can try this.
Create in your class a new field. Use this field when initializing the table in your constructor.
private JTable userTable;
In your update function, try this:
DefaultTableModel model = new DefaultTableModel();
users = // fetch here for your users
for (int i = 0, i < user.length, i++)
model.addRow(users[i]);
}
// finally set new model in your table
userTable.setModel(model);
userTable.doLayout();

Related

data from arraylist is not added to tableModel

I made a GUI using JTable(tableModel) and a JPanel where the user can add data through JTextFields to an Arraylist, but for some reason when I click the add JButton, the adding fails and points to the line where I want to add the arraylist row to the tableModel.
public final static ArrayList<Members> adat = new ArrayList<>();
DefaultTableModel tableModel;
String[][] dummy = new String[][]{{"001", "Jack Black", "London", "20"}};
String[] header = new String[]{"id", "Name", "City", "Age"};
tableModel = new DefaultTableModel(dummy, header);
JTable t = new JTable();
t.setModel(tableModel);
panel1.add(new JScrollPane(t));
frame.add(panel1);
JPanel jp = new EditPanel();
frame.add(jp);
public static void newData() { // the "add" button function
Members newMember = null;
try {
newMember = new Members(
Integer.parseInt(EditPanel.idEdit.getText()),
EditPanel.nameEdit.getText(),
EditPanel.cityEdit.getText(),
Integer.parseInt(EditPanel.ageEdit.getText())
);
adat.add(newMember);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error in modification", ERROR_MESSAGE);
}
toTable();
}
and the method toTable() gets the members data from the arraylist to the JTable
static void toTable() { // adding the users data to the table
if (adat.size() > 0) {
for (int i = 0; i < adat.size(); i++) {
int id = adat.get(i).id;
String name = adat.get(i).name;
String city = adat.get(i).city;
int age = adat.get(i).age;
Object[] data = new Object[4];
data[0] = id;
data[1] = name;
data[2] = city;
data[3] = age;
tableModel.addRow(data); // this is the line where the error points
}
}
}
You're encountering an error at tableModel.addRow(data); because tableModel does not have a method called addRow (docs).
You probably mixed up the DefaultTableModel tableModel with the actual JTable t.
It should be t.addRow(data), although you need to make t global first!

Getting a row of names from the first column of JTable

I have a JTable with the column names " Names " , " Quantity " and " Unit " .
I'm coding a program where you get the ingredients names.
So i need to get the whole row of one column and String it all up together,
because i need to store it in mySql and i have already set it all as String.
Any idea how i can do this ?
My code are as follows :
JTable Code:
DefaultTableModel model = (DefaultTableModel)table.getModel();
if(!txtQty.getText().trim().equals("")){
model.addRow(new Object[]{ingCB.getSelectedItem().toString(),txtQty.getText(),unitCB.getSelectedItem().toString()});
}else{
JOptionPane.showMessageDialog(null,"*Quantity field left blank");
}
Getting the values and for storing :
for(int i = 1; i<= i ; i++){
ingredients = table.getName();
}
This is for loop is wrong and it does not work because i have a constructor to take in Ingredients but because it is inside the loop, it cannot take it in.
Any suggestions please ? Thank you.
Constructor :
Food e2 = new Food(Name, Description, priceDbl, Image, Category, Ingredients, promotion );
e2.createFood();
I'm coding a program where you get the ingredients names. So i need to get the whole row of one column and String it all up together, because i need to store it in mySql and i have already set it all as String.
Want to do so, try this. Here I am getting result into ArrayList and String, as I am commented ArrayList you can avoid it.
public class TableValuePrint extends JFrame implements ActionListener{
private final JButton print;
private final JTable table;
private String str="";
public TableValuePrint() {
setSize(600, 300);
String[] columnNames = {"A", "B", "C"};
Object[][] data = {
{"Moni", "adsad", "Pass"},
{"Jhon", "ewrewr", "Fail"},
{"Max", "zxczxc", "Pass"}
};
table = new JTable(data, columnNames);
JScrollPane tableSP = new JScrollPane(table);
JPanel tablePanel = new JPanel();
tablePanel.add(tableSP);
tablePanel.setBackground(Color.red);
add(tablePanel);
setTitle("Result");
setSize(1000,700);
print=new JButton("Print");
JPanel jpi1 = new JPanel();
jpi1.add(print);
tablePanel.add(jpi1,BorderLayout.SOUTH);
print.addActionListener(this);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TableValuePrint ex = new TableValuePrint();
ex.setVisible(true);
}
});
}
#Override
public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==print){
// ArrayList list = new ArrayList();
for(int i = 0;i<table.getModel().getRowCount();i++)
{
//list.add(table.getModel().getValueAt(i, 0)); //get the all row values at column index 1
str=str+table.getModel().getValueAt(i,0).toString();
}
//System.out.println("List="+list);
System.out.println("String="+str);
}
}
}
Output
String=MoniJhonMax

Why won't JTable with DefaultTableModel Refreshed?

basically what i am trying to do is, i have a JList which contain a list of available drive, if one of that drive selected by user then i will show all html files which located in selected drive in a JTable, so i put an event listener for my JList and then i create a JTable and put all data there and show it in the container. the code look like this:
static class HtmlListing implements ListSelectionListener
{
public void valueChanged(ListSelectionEvent event)
{
if (!event.getValueIsAdjusting())
{ //trying to remove and re-add controls in container.
EastCont.removeAll();
globarr = new ArrayList<File>(); // global variable
FileListing fl = new FileListing();
fl.walk(fileList1.getSelectedValue() + "work\\airasia\\html", 500, 0);
//if(globarr.size() > 0)
//{
Object[][] data = new Object[globarr.size()][globarr.size()];
for(int i = 0; i < globarr.size(); i++)
{
if(globarr.get(i).isFile())
{
String filename = globarr.get(i).getName().toString();
String date = sdf.format(globarr.get(i).lastModified());
Object[] obj = new Object[] {filename, filename.substring(filename.lastIndexOf(".") + 1), date, globarr.get(i).getAbsolutePath()};
data[i] = obj;
}
}
Object[] column = new Object[]{"name ", "type", "date modified", "path"};
DefaultTableModel model = new DefaultTableModel(data, column);
model.fireTableDataChanged();
table = new JTable(model)
{
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column)
{
return false;
};
};
table.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
int rowIdx = table.getSelectedRow(); // path to your new file
TableModel tm = table.getModel();
String path = tm.getValueAt(rowIdx, 3).toString();
File htmlFile = new File(path);
try // open the default web browser for the HTML page
{
Desktop.getDesktop().browse(htmlFile.toURI());
//Desktop.getDesktop().open(htmlFile);
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
table.removeColumn(table.getColumnModel().getColumn(3)); //hide column path from display
table.setFillsViewportHeight(true);
table.setIntercellSpacing(new Dimension(0, 5));
table.setShowGrid(false);
scrollPane = new JScrollPane(table);
EastCont = new JPanel();
EastCont.setLayout(new BorderLayout());
EastCont.add(scrollPane);
EastCont.setPreferredSize(new Dimension(1050, 1000));
//EastCont.repaint();
//EastCont.revalidate();
gui.add(EastCont, BorderLayout.EAST);
gui.revalidate();
gui.repaint();
// }
// else
// {
// EastCont.remove(table);
// gui.remove(EastCont);
// gui.revalidate();
// gui.repaint();
// }
}
this code work only for first time, but does not working for second time and so on, so what i miss here? any help would be great. thank you.
DefaultTableModel model = new DefaultTableModel(data, column);
//model.fireTableDataChanged();
//table = new JTable(model)
table.setModel( model );
Don't create a new table change reset the model of your current table. The rest of the code in that method is not necessary either since you are not creating any new GUI components.
Also, never invoke a fireXXX method. That is the responsibility of the TableModel.

Overwriting pre-defined array

I have an array named columnsArray[] it is pre-defined to contain 6 Strings. When i run my method columns() it should overwrite the columnsArray[] with a new array of Strings that the user selects by checking boxes.
The way i have tried to implement this adds each box checked to an arrayList and then convert the arrayList to array[]. However when the code is run, columnsArray is not overwritten.
Here is my code so far:
public class EditView {
private JFrame frame;
JCheckBox appNo, name, program, date, pName, country, fileLoc, email, uni,
countryUni, degree, classification, funding, supervisor,
rejectedBy, misc;
public ArrayList<String> columnArrLst;
public String[] columnsArray = { "Application Number", "Name", "Program",
"Date", "Project Name", "Country of Uni" };
public EditView() {
}
public void makeFrame() {
frame = new JFrame("Edit View");
frame.setPreferredSize(new Dimension(300, 350));
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(0, 2, 20, 20));
appNo = new JCheckBox("appNo");
name = new JCheckBox("Name");
program = new JCheckBox("Program");
date = new JCheckBox("Date");
pName = new JCheckBox("Project Title");
country = new JCheckBox("Country of Origin");
fileLoc = new JCheckBox("Current File Location");
// countryRef = new JCheckBox("");
email = new JCheckBox("Email address");
uni = new JCheckBox("Last University");
countryUni = new JCheckBox("Country of last Uni");
degree = new JCheckBox("Degree");
classification = new JCheckBox("Degree Classification");
funding = new JCheckBox("funding");
supervisor = new JCheckBox("Supervisor");
rejectedBy = new JCheckBox("Rejected By");
misc = new JCheckBox("Miscelaneous");
contentPane.add(appNo);
contentPane.add(name);
contentPane.add(program);
contentPane.add(date);
contentPane.add(pName);
contentPane.add(country);
contentPane.add(fileLoc);
contentPane.add(email);
contentPane.add(uni);
contentPane.add(countryUni);
contentPane.add(degree);
contentPane.add(classification);
contentPane.add(supervisor);
contentPane.add(rejectedBy);
contentPane.add(misc);
JButton changeView = new JButton("Change View");
changeView.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
columns();
frame.dispose();
}
});
contentPane.add(changeView);
frame.pack();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
public String[] columns() {
columnArrLst = new ArrayList<String>();
if (appNo.isSelected()) {
columnArrLst.add("AppNo");
}
if (name.isSelected()) {
columnArrLst.add("Name");
}
if (date.isSelected()) {
columnArrLst.add("Date");
}
if (fileLoc.isSelected()) {
columnArrLst.add("file Location");
}
if (country.isSelected()) {
columnArrLst.add("Country");
}
if (email.isSelected()) {
columnArrLst.add("Email");
}
if (uni.isSelected()) {
columnArrLst.add("University");
}
if (countryUni.isSelected()) {
columnArrLst.add("Country of Uni");
}
if (degree.isSelected()) {
columnArrLst.add("Degree");
}
if (classification.isSelected()) {
columnArrLst.add("Degree Classification");
}
if (pName.isSelected()) {
columnArrLst.add("ProjectName");
}
if (funding.isSelected()) {
columnArrLst.add("Funding");
}
if (supervisor.isSelected()) {
columnArrLst.add("Supervisor");
}
if (rejectedBy.isSelected()) {
columnArrLst.add("rejected By");
}
if (misc.isSelected()) {
columnArrLst.add("Miscelaneous");
}
columnsArray = new String[columnArrLst.size()];
columnArrLst.toArray(columnsArray);
return columnsArray;
}
}
Any ideas why it isn't overwriting? Thanks for any help.
Try this one
columnsArray = columnArrLst.toArray(new String[columnArrLst.size()]);
Hope it helps.
replace 2nd last line [columnArrLst.toArray(columnsArray);] with following.
columnsArray = columnArrLst.toArray(columnsArray);
There is nothing wrong with your code, array contents are actually changing when you use it as
EditView e = new EditView();
e.makeFrame();
and insert a print loop after
columnArrLst.toArray(columnsArray);
Note that your JFrame is displayed in a different Thread. If you want to check the values, you need to explicitly wait until the button is pressed to see them changed. If you are doing something like:
EditView e = new EditView();
e.makeFrame();
for (String s : e.columnsArray) { System.out.println(s);}
This will print the old values, since the printing thread is actually a different one and prints the values immediately.

Java Arrays being overwritten

Hey I'm a little new here so my apologies if I messed something up in my post. Anyways the problem I'm having has to deal with arrays, what I'm trying to do basically is use the String[] arrays to populate my form and display it on screen and then have the getForm() function return a String[] with the title of the form and the info in text[i]. This all works fine until I use use the button I added to call the getForm() function and I change to a different form (createForm() attached to ListListener) and all the labels appear as whatever was returned in the getForm() function. I'm pretty sure it has something to do with the way I'm using my arrays but I thought they would be set back to normal after I chose another list item which goes through the createForm() function again resetting the arrays, so I'm not sure whats going on.
Thanks
I've included a screenshot of what I'm referring too bellow as well.
http://www.majhost.com/gallery/adc90/afsd/error.png
class Form extends JPanel
{
//Arrays for the forms
private String[] com = {"Communication","ICAO","Type","Frequency"};
private String[] fuel = {"Fuel","ICAO","Type"};
private String[] runway = {"Runway","ICAO","Number","Type","Length"};
private String[] airplane = {"Airplane","Make","Model","Type","Fuel Capacity", "Fuel Burn Rate", "Air Speed"};
private String[] airport = {"Airplane","ICAO","Name","Longitude","Latitude","crFreq","crType", "Fuel Type"};
//Declare variables
private JTextField[] text;
private String[] formReturn;
private String[] formArray;
private JButton submit,clear;
public Form()
{
createForm("Airplane");
}
public void createForm(String choice)
{
removeAll();
if(choice.equals("Communication"))
{
formArray = com;
}
else if(choice.equals("Fuel"))
{
formArray = fuel;
}
else if(choice.equals("Airplane"))
{
formArray = airplane;
}
else if(choice.equals("Airport"))
{
formArray = airport;
}
else if(choice.equals("Runway"))
{
formArray = runway;
}
int l = formArray.length + 1;
text = new JTextField[l];
//Layout info
GridLayout grid = new GridLayout(l,2);
grid.setHgap(0);
setLayout(grid);
//Set label
add(new JLabel(formArray[0]));
add(new JLabel(""));
for(int i = 1; i < formArray.length; ++i)
{
add(new JLabel(formArray[i]));
add(text[i] = new JTextField(20));
}
//Add in the buttons and the actionlisteners
submit = new JButton("Create");
clear = new JButton("Delete");
add(clear);
clear.addActionListener(new Button());
add(submit);
submit.addActionListener(new Button());
updateUI();
}
//Get form info
//This works so far
public String[] getForm()
{
formReturn = formArray;
formReturn[0] = formArray[0];
for(int i = 1; i < formReturn.length; i++)
formReturn[i] = text[i].getText();
return formReturn;
}
//Clear form
public void clearForm()
{
for(int i = 1; i < formArray.length; i++)
text[i].setText("");
}
}
public String[] getForm()
{
formReturn = formArray; /* (0) */
formReturn[0] = formArray[0];
for(int i = 1; i < formReturn.length; i++)
formReturn[i] = text[i].getText(); /* (1) */
return formReturn;
}
Look at line (1): you modify formReturn array which points to labels text. formReturn -> formArray -> com.
To fix it just create new String array at (0):
formReturn = new String[formArray.length];

Categories