0

So, here is the problem - i want to add item if class in arraylist, and after filling all pols in class i need to refresh Jtable, but i don't know how.

here's my code: Main() {

    JFrame frame = new JFrame();
    frame.setSize(550, 450);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new FlowLayout());

    JButton jb1 = new JButton("Добавить");
    JButton jb2 = new JButton("Удалить");
    JButton jb3 = new JButton("Редактировать");
    JButton jb4 = new JButton("Найти мощную машину");


    frame.add(jb1);
    frame.add(jb2);
    frame.add(jb3);

    String[] tbheader = {"ID", "Машина", "Модель", "Цвет", "Мощность", "Объём бака"};
    String[][] tbdata = new String[cdb.size()][6];
    int i = 0;
    for (CarDB temp:cdb){
        tbdata[i][0] = Integer.toString(i + 1);
        tbdata[i][0] = temp.Brand;
        tbdata[i][0] = temp.Model;
        tbdata[i][0] = temp.Color;
        tbdata[i][0] = Short.toString(temp.Power);
        tbdata[i][0] = Short.toString(temp.TankVol);
        i++;
    }

    JTable jt = new JTable(tbdata, tbheader);
    frame.add(new JScrollPane(jt));

    jt.setSize(550, 320);
    jt.setPreferredScrollableViewportSize(jt.getSize());
    jt.setShowHorizontalLines(true);
    jt.setShowVerticalLines(true);

    jb1.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            AddCarInList();
            jt.repaint();
        }
    });
    jb2.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            DeleteCarFromList();
        }
    });
    jb3.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            ChooseCarId();
        }
    });
    frame.add(jb4);
    jb4.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            FindPowerfulCar();
        }
    });

    jb2.addActionListener(this);
    jb3.addActionListener(this);
    jb4.addActionListener(this);
    frame.setVisible(true);
    this.cdb = new ArrayList<>();
}

public void AddCarInList() {
    JFrame addCar = new JFrame();
    addCar.setSize(250, 300);
    addCar.setLayout(new FlowLayout());

    JLabel l_br = new JLabel("Введите марку");
    JLabel l_md = new JLabel("Введите модель");
    JLabel l_cl = new JLabel("Введите цвет");
    JLabel l_pw = new JLabel("Введите мощность");
    JLabel l_tv = new JLabel("Введите объём бака");

    JTextField t_br = new JTextField(20);
    JTextField t_md = new JTextField(20);
    JTextField t_cl = new JTextField(20);
    JTextField t_pw = new JTextField(4);
    JTextField t_tv = new JTextField(3);

    JButton conf = new JButton("Добавить");

    addCar.add(l_br);
    addCar.add(t_br);
    addCar.add(l_md);
    addCar.add(t_md);
    addCar.add(l_cl);
    addCar.add(t_cl);
    addCar.add(l_pw);
    addCar.add(t_pw);
    addCar.add(l_tv);
    addCar.add(t_tv);
    addCar.add(conf);
    conf.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (CheckStr(t_br.getText(), t_md.getText(), t_cl.getText())) {
                if (CheckShort(t_pw.getText(), t_tv.getText())) {
                    CarDB car = new CarDB(
                            t_br.getText(),
                            t_md.getText(),
                            t_cl.getText(),
                            Short.parseShort(t_pw.getText()),
                            Short.parseShort(t_tv.getText()));
                    cdb.add(car);
                    System.out.println(cdb.indexOf(car) + 1
                            + " " + cdb.get(0).Brand
                            + " " + cdb.get(0).Model
                            + " " + cdb.get(0).Color
                            + " " + cdb.get(0).Power
                            + " " + cdb.get(0).TankVol);
                    addCar.setVisible(false);
                } else Alert(1);
            } else Alert(3);
        }
    });
    addCar.setVisible(true);
}

Anybody know how to solve this?

genesi5
  • 455
  • 3
  • 17
  • 1
    Update the `JTable`'s `TableModel`, it will trigger appropriate events to update the `JTable` appropriately. See [How to Use Tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) for more details. For [example](http://stackoverflow.com/questions/16785982/how-to-refresh-data-in-jtable-i-am-using-tablemodel/16786120#16786120) – MadProgrammer Nov 25 '15 at 19:50

2 Answers2

0

Object oriented programming is all about extending classes.
One of the classes you should extend is AbstractTableModel as a model to your JTable.
Check this as an example.
After which, you update this model class and then refresh by calling:
model.fireTableDataChanged() (update rows) or
model.fireTableStructureChanged() (update row + columns)

Leet-Falcon
  • 2,107
  • 2
  • 15
  • 23
  • 1
    Or use a `DefaultTableModel` which has most of the functionality built in by default, but, that would come down to knowing in what format the data is in before making further decisions (I personally prefer using `AbstractTableModel` for most cases, but lets no discount the simplicity of the default model) – MadProgrammer Nov 25 '15 at 21:41
0

The cdb field is a list of cars. In the AddCarInList method, a car is added to the list. The problem is that this list is only used once to initialize the table. As MadProgrammer already suggested, you can solve this by updating the table model.

You can change the table initialization like this:

Vector<String> columnNames = new Vector<>(Arrays.asList(tbheader));
Vector<Vector<Object>> dataVector = new Vector<>();
tableModel = new DefaultTableModel(dataVector, columnNames);
JTable jt = new JTable(tableModel);

The code above assumes you have added a tableModel field of type DefaultTableModel. In the AddCarInList method, you could add a row to the table model:

final Vector<String> rowVector = new Vector<>();
rowVector.add(t_br.getText());
rowVector.add(t_md.getText());
rowVector.add(t_cl.getText());
rowVector.add(t_pw.getText());
rowVector.add(t_tv.getText());
tableModel.addRow(rowVector);

Adding a row to the table model should automatically refresh the table.

Community
  • 1
  • 1
Freek de Bruijn
  • 3,552
  • 2
  • 22
  • 28