Question

I'm french and newbie in Java.. I have a project to do concerning "regates" (races) of "voilier" (ship). There is a combo of regate and this must make a Jtable of ship where we can add time of the end of their race. My problem is that I want to make this Jtable when we click on one item of the combo but I don't know how to do..

My code :

package eole;

import java.awt.event.ItemEvent;

public class ArrivéesVoiliers extends JFrame implements ItemListener {

    private JPanel contentPane;
    private JTable tableArrivées;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ArrivéesVoiliers frame = new ArrivéesVoiliers();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ArrivéesVoiliers() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JComboBox combRegate = new JComboBox();
        combRegate.setBounds(30, 25, 90, 20);
        contentPane.add(combRegate);
        ArrayList<Regate> lesReg = Application.getRegates();

        for (Regate laReg : lesReg) {
            combRegate.addItem(laReg.getNomReg());
        }

        combRegate.addItemListener(this);

        String nom = (String) combRegate.getSelectedItem();
        Regate regSelec = Application.getRegate(nom);

        JLabel lblDateDep = new JLabel(regSelec.getDate());
        lblDateDep.setBounds(130, 31, 60, 14);
        contentPane.add(lblDateDep);

        JLabel lblHeuredepart = new JLabel(regSelec.getTime());
        lblHeuredepart.setBounds(200, 31, 60, 14);
        contentPane.add(lblHeuredepart);

        ArrayList<Voilier> voiliersPart = Application.getVoiliers(regSelec);

        String[] entetes = { "Voiliers participants", " Classe", "Rating",
                "Heure arrivée", "en seconde", "Abandon", "Stop Chrono",
                "Ajout" };

        DefaultTableModel voilPart = new DefaultTableModel();
        voilPart.setColumnCount(8);
        for (Voilier unVoil : voiliersPart) {
            voilPart.addRow(new Object[] { unVoil.getNom(), unVoil.getNum(),
                    unVoil.getRating(), "Heure arrivée", "en seconde",
                    new Boolean(false), new Boolean(false), "Ajouter" });
        }

        new AbstractTableModel() {
            public int getColumnCount() {
                return 0;
            }

            public int getRowCount() {
                return 0;
            }

            public Object getValueAt(int rowIndex, int columnIndex) {
                return null;
            }

            public boolean isCellEditable(int row, int col) {
                if (col == 4) {
                    return true;
                } else {
                    return false;
                }
            }
        };

        tableArrivées = new JTable(voilPart);
        tableArrivées.setBounds(50, 228, 312, -125);
        contentPane.add(tableArrivées);

    }

    @Override
    public void itemStateChanged(ItemEvent e) {
    }
}
Was it helpful?

Solution 2

Change your itemStateChanged(ItemEvent e) method in next way:

@Override
public void itemStateChanged(ItemEvent e) {
    if(e.getStateChange() == ItemEvent.SELECTED){
        voilPart.addRow(new Object[] {  "col1",  "col2",
                 "col3", "col4", "col5",
                new Boolean(false), new Boolean(false), "col8" });
    }
}

Also I recommend you to:

1)Use setLayout(null); and setBounds() methods instead of the, try to use LayoutManager, try to start from FlowLayout and BorderLayout it's really simple.

2)For setting size of your JFrame use method pack();

3)I think that entetes it's column names create your TableModel like next

DefaultTableModel voilPart = new DefaultTableModel(new Object[][]{},entetes);

4) Set voilPart as instance variable for using it in itemStateChanged() method for adding a new row.

5) This isn't important code, you can delete it, because it's local variable that never used :

new AbstractTableModel() {
    public int getColumnCount() {
        return 0;
    }

    public int getRowCount() {
        return 0;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return null;
    }

    public boolean isCellEditable(int row, int col) {
        if (col == 4) {
            return true;
        } else {
            return false;
        }
    }
};

6) add your table to JScrollPane for scrolling.

Usefull liks:

How to use Scroll Pane

JTable tutorial

Variables in Java

OTHER TIPS

Instead of using EventListener, you should use ChangeListener.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top