Java Record Navigation

随声附和 提交于 2019-12-24 10:37:09

问题


your valuable help needed again. I have the following code in which i am reading file contents for each file. each file is related to an individual staff. On click of a button called "show staff record", i want to show all staff file data in a GUI. but instead of all them appearing at one i want it to have navigation next and previous like in MS Access? any ideas. a code perhaps?

/*********************Calculate Staff Balance***************************/   
public class calcBalanceListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        try {
            File folder = new File("/register/");
           filePaths = new ArrayList<String>();
            if (folder.isDirectory()) {
                                for (File file : folder.listFiles()) {
                                    filePaths.add(file.getPath());
                                    }
            }

        }//end try

        catch (Exception f) {
            f.printStackTrace();
          } 

            callDetail();
            }}

/*************************************************************************/

public void callDetail() {


    File f = new File(filePaths.get(indexCounter));
    try{
        FileReader fileReader = new FileReader(f);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String name = bufferedReader.readLine();
        int id = Integer.parseInt(bufferedReader.readLine());
        bufferedReader.readLine();
        String address = bufferedReader.readLine();
        int amount = Integer.parseInt(bufferedReader.readLine());
        bufferedReader.readLine();

        balanceFrame = new JFrame("Monthly Staff Balance");

            lID.setText("Staff ID: " + id);
            lname.setText("Staff ID: " + name);
            laddress.setText("Staff ID: " + address);
            lbalance.setText("Staff ID: " + amount);


            balanceFrame.add(lID);
            balanceFrame.add(lname);
            balanceFrame.add(laddress);
            balanceFrame.add(lbalance);

        bufferedReader.close();
        fileReader.close();

    }//end try
    catch(IOException z){

        z.printStackTrace();
    }   //end catch     


}

/***************************************************************************************************/


回答1:


What you might do is that instead of reading the files, in your loop, you might want to iterate and obtain the file location of all the files in your directory and place their address inside an array list.

You can then use the back/forward buttons to traverse the array list, each time loading the file according to which location you are currently in your array list.

List<String> filePaths = new ArrayList<String>();
if (folder.isDirectory()) {
                    for (File file : folder.listFiles()) {
                        filePaths.add(file.getPath());
                        }
                    }
                }

All you need to do is to have some global counter which you use to then navigate the array list when the forward/backward buttons are pressed. Once the button is pressed, load the appropriate file (determined by the counter) and display its content.



来源:https://stackoverflow.com/questions/8743187/java-record-navigation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!