Reputation: 9
Hello everyone I need to make Prev and next Jbutton to read lines from a csv file to JTextField I managed to make Load button but now I am stuck. This is for my homework. So if anyone could help it would be nice :D. This is the question in homework: The Next and Previous buttons display the next or previous order from the file. Use the String’s split method. Separate methods are needed for next, previous. Do not duplicate any significant amount of code. Guideline is 3 lines or less is OK to duplicate.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.text.*;
public class OrderSystem extends JFrame implements ActionListener
{
private JButton exit, cal, save, clear, load, prev, next;
private JTextField text, text1, text2, text3;
private int counter;
//ArrayList<String> splitLine = new ArrayList<String>();
String[] textLine = new String[3];
public OrderSystem()
{
JFrame myFrame = new JFrame("{Your Name} Item Orders Calculator");
myFrame.setLayout(new BorderLayout(2, 2));
JPanel panel = new JPanel();
panel.setLayout( new GridLayout(0, 2));
panel.add( new JLabel("Item name: ", SwingConstants.RIGHT));
text = new JTextField("", 25);
panel.add( text );
panel.add( new JLabel("Number of: ", SwingConstants.RIGHT));
text1 = new JTextField("", 20);
panel.add( text1 );
panel.add( new JLabel("Cost: ", SwingConstants.RIGHT));
text2 = new JTextField("", 20);
panel.add( text2 );
panel.add( new JLabel("Amount owed: ", SwingConstants.RIGHT));
text3 = new JTextField("", 20);
text3.setEnabled(false);
panel.add( text3 );
add(panel,BorderLayout.CENTER);
JPanel panelS = new JPanel( new FlowLayout(FlowLayout.CENTER, 20,3) );
// Create some buttons to place in the south area
cal = new JButton("Calculate");
save = new JButton("Save");
clear = new JButton("Clear");
exit = new JButton("Exit");
load = new JButton("Load");
prev = new JButton("<prev");
next = new JButton("next>");
exit.addActionListener(this);
clear.addActionListener(this);
cal.addActionListener(this);
save.addActionListener(this);
load.addActionListener(this);
prev.addActionListener(this);
next.addActionListener(this);
panelS.add(cal);
panelS.add(save);
panelS.add(clear);
panelS.add(exit);
panelS.add(load);
panelS.add(prev);
panelS.add(next);
add(panelS, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == exit)
{
System.exit(0);
}
else if(e.getSource() == cal)
{
double amount = Double.parseDouble(text1.getText()) * Double.parseDouble(text2.getText());
text3.setText(String.format(Locale.ENGLISH, "%.2f", amount));
}
else if(e.getSource() == save)
{
boolean append = true;
try
{
BufferedWriter out = new BufferedWriter(new FileWriter("121Lab1.csv", append));
out.write(String.format("%s,%s,%s,%s", text.getText(), text1.getText(), text2.getText(), text3.getText()));
out.newLine();
out.flush();
out.close();
}
catch (IOException ex)
{
JOptionPane.showMessageDialog(null, "Error");
}
counter++;
}
else if(e.getSource() == clear)
{
text.setText("");
text1.setText("");
text2.setText("");
text3.setText("");
}
else if(e.getSource() == load)
{
try {
String splitLine;
BufferedReader br = new BufferedReader( new FileReader("121Lab1.csv"));
while ((splitLine = br.readLine()) != null)
{
textLine = splitLine.split(",");
text.setText(textLine[0]);
text1.setText(textLine[1]);
text2.setText(textLine[2]);
text3.setText(textLine[3]);
}
}
catch (IOException exp)
{
JOptionPane.showMessageDialog(null, "Error no file found.");
}
}
else if(e.getSource() == prev)
{
//Prev line
}
else if(e.getSource() == next)
{
//Read next line
}
}
public static void main(String[] args)
{
OrderSystem main = new OrderSystem();
main.setTitle("Item Orders Calculator");
main.setSize(450, 150);
main.setLocationRelativeTo(null);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setVisible(true);
}
}
Upvotes: 0
Views: 2682
Reputation: 5474
Load the csv data upon clicking the load button. I recommend using OpenCSV for reading the whole csv file in one go. CSVReader's readAll()
will give you a list of String array.
else if (e.getSource() == load) {
CSVReader reader = null;
try {
reader = new CSVReader(new FileReader("csv.csv"));
myEntries = reader.readAll();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
ptr = 1;
navigate(ptr);
}
myEntries
should be instance level
private List<String[]> myEntries;
Define an instance level int variable that will decrement when prev button is pressed otherwise increment when next button is pressed.
private int ptr;
Define a private method that will retrieve data from the myEntries list based on the index that it will receive.
private void navigate(int index){
String[] data = myEntries.get(index);
text.setText(data[0]);
text1.setText(data[1]);
text2.setText(data[2]);
text3.setText(data[3]);
}
In your prev and next button, increment/decrement ptr then happily use the navigate method passing the resultant value.
else if (e.getSource() == prev) {
if(ptr > 1){
ptr--;
navigate(ptr);
}
} else if (e.getSource() == next) {
if(ptr < (myEntries.size()-1)){ //lists are 0 based
ptr++;
navigate(ptr);
}
}
Upvotes: 1