Skip to content
Advertisement

I have a display problem with JTable in Java

Firstly I created a structure of JTable in the class of JPanel

 public void paint(Graphics g) {
        // TODO Auto-generated method stub
        this.setLayout(null);


        Object [][] data = {};

        String[] cols = {"Student ID", "Student Name", "D.O.B", "Contact info"};
        DefaultTableModel model = new DefaultTableModel(data, cols);
        JTable table = new JTable(model) {
             public boolean isCellEditable(int row, int column){
                    return false;
               }
        };
        JScrollPane scroll = new JScrollPane(table);
        scroll.setBounds(20, 50, 620, 200); 
        add(scroll);

and then I have updated a bunch of data into rows of JTable in a method of ActionListener when I add a button “Load” Load.addActionListener(listener);

ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub

                if(e.getSource() == Load) {
                    JFileChooser f = new JFileChooser();
                    int result = f.showOpenDialog(null);

                    if(result == JFileChooser.APPROVE_OPTION) {

                        String Path= f.getSelectedFile().getAbsolutePath();

                        path.setText(Path);



                        Object[][] vector = new String[Dem_Dong(Path)][4]; 
                        String[] cols = {"Student ID", "Student Name", "D.O.B", "Contact info"};

                        for (int i = 0; i < Dem_Dong(Path); i++) {

                        String[] array = Doc_File(Path, i).split("#");
                        vector[i][0] = array[0];
                        vector[i][1] = array[1];
                        vector[i][2] = array[2];
                        vector[i][3] = array[3];
                        }
                        model.setDataVector(vector, cols);
                }
            }

My JTable show up but it has a uncomfortable problem like this enter image description here

I have tried a lot of way that I searched on Google but nobody can help me. If you know it please give me an anwser, I am a Freshman of college in Vietnam hope you understand my question. Thank you very much

Advertisement

Answer

public void paint(Graphics g) {
     // you are currently create your JTable and GUI components here
}

You’re doing all this GUI building code within a painting method and that is, to be blunt, madness. Put it into code that is called once and only once, such as the class constructor. A painting method should be for painting and painting only.

Suggestions:

  • Get that code out of paint and into the class constructor
  • You shouldn’t be overriding paint to begin with
  • If you need to do custom drawing (you don’t show yet that you do), this should be done within the paintComponent method of a class that extends JPanel, not the paint method
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement