如何在JTable中显示我的双重LinkedList数据? [英] How Can I Show My Doubly LinkedList Data in a JTable?

查看:143
本文介绍了如何在JTable中显示我的双重LinkedList数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何将链接列表数据添加到Jtable的许多小时,但是我发现的结果都不令人满意.我试图自学Java,所以对我来说有点困难.无论如何,这是我的代码.我知道这可能真的很糟糕,所以请耐心等待我,并帮助我改善它.

I've been searching for many hours on how to add linked list data to a Jtable but none of the results I found were satisfying. I'm trying to teach myself java so it's a bit difficult for me. Anyways, here is my code. I know it's probably really bad so please be patient with me and help me in making it better.

public class node {

    public node next,pre;
    public String name;
    public double price;

    public node (String n,double p){            
        this (n,p,null,null);
    }

    public node (String n,double p, node ne,node pr){           
        name = n;
        price = p;
        next = ne;
        pre = pr;
    }       
}


public class list {

    public node head, tail;

    public list (){         
        head = tail = null;
    }

    public void addHead (String n,double p){            
        if (head != null){              
            head = new node (n,p,head, null);
            head.next.pre = head;
        }           
        else
            head = tail = new node (n,p);
    }

    public int  size (){            
        int size = 0;
        for(node n = head; n.next != null; n = n.next)
          size++;     
          return size;
    }       
    public String print (){
        String s = "";
        if (head !=null){               
            for (node p = head; p !=null; p = p.next)
                return p.name +"\t"+ p.price;                   
        }           
        return s;           
    }
}


我不知道在getValueAt方法中写什么


I don't know what to write in the getValueAt method

import javax.swing.*;
import java.awt.*;
import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.table.*;
class gui extends JFrame implements ActionListener {

    list li = new list ();
    JButton ad;
    JTextField t,t1;
    JTextField t2;
    JTable table  = new JTable (new table_model());

    public gui (){          
        setSize(500,500);
        setTitle("DEMO");
        setLocationRelativeTo(null);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(2,2));
        t = new JTextField ("");
        add(t);
        t1 = new JTextField ("");
        add(t1);
        ad = new JButton ("add");
        ad.addActionListener(this);
        add(ad);
        add(table);
    }

    public class table_model extends AbstractTableModel {
        public list data;
        String [] columns = {"Name","Price",
        };          
        public void addList (list data){                
            this.data = data;
        }
        public int getColumnCount() {
        return columns.length;
        }
        public int getRowCount() {
         return data.size();
        }
           public String getColumnName(int col) {
            return columns[col];
        }

        public Object getValueAt(int row, int col)
        {            

        }
    }

    public void actionPerformed (ActionEvent e){
        if (e.getSource() == ad && !t.equals("")){              
            li.addHead(t.getText(),Integer.parseInt(t1.getText()));         
        }
    }
}

class test{

    public static void main (String [] aed){            
        gui g = new gui();
        g.setVisible (true);            
    }
}

推荐答案

我绝对建议您使用

I would definitely recommend that you use the LinkedList class from the java.util package instead of implementing your own. If you use Java's LinkedList, then it will have the method T get(int index) which will return the element at the specified index. Then all you have to do is map your objects properties to the correct column.

class MyObj {
    public String name;
    public double price;
}

class MyTableModel extends AbstractTableModel {

    private List<MyObj> contents;

    public MyTableModel(List<MyObj> contents){
        this.contents = contents;
    } 

    @Override
    public int getRowCount(){
        return this.contents.size();
    }

    @Override
    public int getColumnCount(){
        return 2;
    }

    @Override
    public Object getValueAt(int row, int column){
        MyObj myObj = contents.get(row);

        //map column index to property
        return (column == 0)? myObj.name : myObj.price;
    }
}

这并不需要专门的LinkedList.所有必需的方法都在列表界面,当然是LinkedList类实现的.如果必须使用自己的LinkedList实现,则还应该实现List接口.这将使您的实现可以在需要列表的任何地方使用.然后,您将编程为接口,而不是一个实现.

This does not require a LinkedList specifically. All of the required methods are defined in the List Interface, which the LinkedList class of course implements. If you must use your own LinkedList implementation, then you should also implement the List interface. This will allow your implementation to be used wherever a List is required. Then you will be programming to an interface, not an implementation.

这篇关于如何在JTable中显示我的双重LinkedList数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆