Question

I'm using JScrollPane to put inside a JTable with some values, but the JScrollPane has to be invisible until user clicks on a button. But the JScrollPane remains not visible until I maximise the window manually. I've tried almost everything, .validate(); .revalidate(); .repaint()... I'm using Netbeans, and on the Custom Code of the JScrollPane I've wrote this:

scrollPane = new javax.swing.JScrollPane();
this.scrollPane.setVisible(false);
// El código de los subcomponentes y del gestor de distribución - no se muestra aquí

// El código que agrega el componente al contenedor superior - no se muestra aquí

Then, on MouseReleased:

private void botoGeneraLlistatMouseReleased(java.awt.event.MouseEvent evt) {
    this.generaLlistat();
}

And on the end of generaLlistat():

public void generaLlistat(){{
   ...(Some code requesting data from DB)
   JTable table = new JTable(rows, headings);
   scrollPane.setViewportView(table);
   this.scrollPane.setVisible(true);
   statement.close();
   connection.close();
}

I will appreciate any help :) Thank you all, and sorry about my english grammar and spelling.

Était-ce utile?

La solution

Try doing this way:

package stack;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Aitor extends JFrame implements ActionListener{
    JTable table;
    JPanel panel = new JPanel();
    JButton button = new JButton("Add"); 

    String data[][]={{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},                        {"hey","hey"},{"hey","hey"}
,{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"},{"hey","hey"}
,{"hey","hey"},{"hey","hey"}};
    String columns[] = {"First","Second"};

    public Aitor(){
        setLayout(new BorderLayout());
        panel.setLayout(new BorderLayout());
        panel.setPreferredSize(new Dimension(500,400));
        button.addActionListener(this);
        add(panel,BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);

    }

    public static void main(String [] a){
        Aitor aitor = new Aitor();
        aitor.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        aitor.pack();
        aitor.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        panel.removeAll();
        table =new JTable(data,columns);
        panel.add(new JScrollPane(table),BorderLayout.CENTER);
        repaint();
        revalidate();
    }
}

Autres conseils

You can just call pack() on your container after calling setVisible(true) and and your scrollpane will appear.

You can use setPreferredScrollableViewportSize(), as shown here and here, although it has some of the same drawbacks mentioned here.

Try this coding if youu arer using jframe.

In constructor method, give

Jscrollpane.setVisible (false);

Give the particular jscrollpane which is associated with your jtable in jframe.

Then in actionperformed method give

Jscrollpane. setvisible (true); 
pack (); 

It would help.....

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top