How to create dynamically a swing list with n row where for each row I put 2 data

后端 未结 2 1352
半阙折子戏
半阙折子戏 2021-01-25 14:38

I have less experience with Java Swing and I want to create a dynamic list where for each element must be specified 2 parameters.

I have n elements, where the number n i

相关标签:
2条回答
  • 2021-01-25 15:18

    How about using a JTable with two columns, one for each data value?

    There's a good tutorial on using tables at: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html

    JTable Example from docs.oracle.com

    0 讨论(0)
  • 2021-01-25 15:28

    By Using ArrayList You can :

    import java.util.ArrayList;
    
    public class ArrayList2d<Type>
    {
    ArrayList<ArrayList<Type>>  array;
    
    public ArrayList2d()
    {
        array = new ArrayList<ArrayList<Type>>();
    }
    
    public void ensureCapacity(int num)
    {
        array.ensureCapacity(num);
    }
    
    
    public void ensureCapacity(int row, int num)
    {
        ensureCapacity(row);
        while (row < getNumRows())
        {
            array.add(new ArrayList<Type>());
        }
        array.get(row).ensureCapacity(num);
    }
    
    
    public void Add(Type data, int row)
    {
        ensureCapacity(row);
        while(row >= getNumRows())
        {
            array.add(new ArrayList<Type>());
        }
        array.get(row).add(data);
    }
    
    public Type get(int row, int col)
    {
        return array.get(row).get(col);
    }
    
    public void set(int row, int col, Type data)
    {
        array.get(row).set(col,data);
    }
    
    public void remove(int row, int col)
    {
        array.get(row).remove(col);
    }
    
    public boolean contains(Type data)
    {
        for (int i = 0; i < array.size(); i++)
        {
            if (array.get(i).contains(data))
            {
                return true;
            }
        }
        return false;
    }
    
    public int getNumRows()
    {
        return array.size();
    }
    
    public int getNumCols(int row)
    {
        return array.get(row).size();
    }
    }
    
    0 讨论(0)
提交回复
热议问题