简体   繁体   中英

Populating a JTable with parsed information

I am trying to populate a JTable with information taken from a website (namely price and item name). I have a class that asks the user to input a URL and scans the page for the price and item name as well as the URL. Currently it takes all the parsed information and stores it in three different text files, one for price, one for item name, and one for the URL. I am trying to populate a JTable containing three columns (item name, price, and URL) with this information but every time I scan a new page the text files are overwritten and the previous information is lost. I don't necessarily need the JTable to be populated via the text file, I just need it to somehow get the information. Here is some of my code.

  public BestBuy (JFrame frame){
            super (frame, "Best Buy URL", true);
            setLayout (new FlowLayout());

            label = new JLabel ("Enter Best Buy URL");
            add (label);

            url = new JTextField ("Enter URL Here", 40);
            add (url);

            submit = new JButton ("Submit");
            add (submit);

            event b = new event ();
            submit.addActionListener (b);
        }

        public class event implements ActionListener{
            public void actionPerformed (ActionEvent b){
                try {    
                    String datab = url.getText(); //perform your operation
                    datab = datab.trim();
                    datab = datab.toLowerCase();
                    Document document = Jsoup.connect(datab).get();
                    String amountb = document.select(".amount").first().text();
                    String nameb = document.select(".product-title").first().text();
                    FileWriter stream = new FileWriter     ("C:\\Users\\Daniel\\Desktop\\price.txt");
                    BufferedWriter out = new BufferedWriter (stream);
                    out.write(amountb + "\n");
                    out.newLine();
                    out.close();
                    FileWriter stream1 = new FileWriter ("C:\\Users\\Daniel\\Desktop\\itemName.txt");
                    BufferedWriter out1 = new BufferedWriter (stream1);
                    out1.write(nameb + "\n");
                    out1.newLine();
                    out1.close();
                    FileWriter stream2 = new FileWriter ("C:\\Users\\Daniel\\Desktop\\url.txt");
                    BufferedWriter out2 = new BufferedWriter (stream2);
                    out2.write(datab + "\n");
                    out2.newLine();
                    out2.close();
                }
                catch (Exception ex) {

                }   
                setVisible (false);
            } 

This class asks the user for a Best Buy URL and parses the given page for item name, and price then writes it to files on my desktop.

public FirstGui (){
        setLayout (new FlowLayout ());

        String[] columnName = {"Item Name", "Price", "URL"};

        Object [] [] data = {

        };


        table = new JTable (data, columnName);
        table.setPreferredScrollableViewportSize(new Dimension (500, 300));
        table.setFillsViewportHeight (true);

        JScrollPane scrollpane = new JScrollPane (table);
        add (scrollpane);

Now I am trying to get that parsed information onto my JTable but I have no idea how to do so. I tried to do

public getdatab() {
return datab;
}
public getnameb() {
return nameb;
}
public getamountb() {
return amountb;
}

but all these strings are within a void so that did not work. As you can probably see I am quite new to java and this might have an obvious solution but I have been stuck on this for a few days and cant figure it out. Thank you.

I'm not sure exactly how your getting your data, but you want to do something like this. Since you're trying to write the data to three different files, I will assume the data is coming in from three different streams. Here's the thing though. For this to work, all the data needs to be in parallel, Meaning that the first item, should correspond to the first price and first url, and so on. If this is the case, you can do something like this.

  1. Have three separate lists.

     List<String> names = new ArrayList<String>(); List<String> prices = new ArrayList<String>(); List<String> urls = new ArrayList<String>(); 
  2. Then for each item you were going to add to a file, add to the list instead.

  3. Use a DefaultTableModel as the model of your JTable

     String[] columnName = {"Item Name", "Price", "URL"}; DefaultTableModel model = new DefaultTableModel(columnNames, 0); table = new JTable(model); 
  4. Now you can just add rows, using the data from the lists. Use the method model.addRow(row) , where row is an array of Objects

     for (int i = 0; i < names.size(); i++) { String name = names.get(i); String price = prices.get(i); String url = urls.get(i); model.addRow(new Object[] { name, price, url }); } 

That's all there is to it. The model will update the table for you dynamically. But remember, like I said, the data in the lists must be in sync with one another for you to get the desired result.

If you're getting data in one row at a time, instead of one column at a time, that makes it even easier. For each set of data, that comes in, just add it as a row like it did in step 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM