Reputation: 479
I have Jtable that I want to add to the main JFrame when a MenuItem is triggered, but the problem is that will not show.
If I add the table to panel from the beginning it shows, but I need it to show when the action occures.
Here is the main class that creates the frame(I removed several items which are not needed to be posted like creating the menubar,menu etc):
package gestiune;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
public class Gestiune {
static Gest gest;
static Action actListaAng;
static JPanel panouPrinc;
static ListaAngajati lang;
static JMenuItem listaAng;
static class Gest extends JFrame{
public Gest(){
actListaAng = new ActListaAng("List");
listaAng=new JMenuItem(actListaAng);
panouPrinc = new JPanel();
panouPrinc.setBackground(Color.white);
Container cp = getContentPane();
cp.add(panouPrinc);
pack();
setTitle("Some title");
setSize(1000,700);
setLocation(0,0);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
}
public static void main(String[] args) {
gest = new Gest();
}
//class for listing action
static class ActListaAng extends AbstractAction {
public ActListaAng(String text){
super(text);
}
@Override
public void actionPerformed(ActionEvent e) {
lang = new ListaAngajati();
panouPrinc.add(lang);
}
}
}
Here is the table class:
package gestiune;
import javax.swing.*;
import java.awt.*;
public class ListaAngajati extends JPanel {
JTable tabel;
JScrollPane panouScroll;
public ListaAngajati() {
panouScroll = new JScrollPane(tabel);
String[] numeCol = {
"Nume",
"Prenume",
"Categorie",
"Data Adaugare",
"Ultima Modificare"
};
Object[][] linii = {
{"verban","adrian","sds","16-03-1989","acum"}
};
tabel = new JTable(linii,numeCol);
setLayout(new BorderLayout());
add(tabel.getTableHeader(), BorderLayout.PAGE_START);
add(panouScroll);
add(tabel);
}
}
I've tried several things, like using repaint on ActionEvent, or directly into the the table class......and while if I add lang=new ListaAngajati(); panouPrinc.add(lang);
directly into the main jframe constructor works, from action it doesn't, so I kind of run out of options, so can someone give me a hand?
Upvotes: 1
Views: 75
Reputation: 209002
If you revalidate()
and repaint()
it works. You should always revalidate()
and repaint()
after adding a component during runtime.
@Override
public void actionPerformed(ActionEvent e){
lang=new ListaAngajati();
panouPrinc.add(lang);
panouPrinc.revalidate();
panouPrinc.repaint();
}
Also there was no button to click on, so I added a button here, and added the Action
Container cp=getContentPane();
JButton button = new JButton(new ActListaAng("Action"));
button.setText("Open");
cp.add(button, BorderLayout.PAGE_START);
cp.add(panouPrinc);
pack();
Works fine.
As a side note, you really need go through a tutorial on static
. You're totally overly and unnecessarily using it.
Upvotes: 1
Reputation: 20755
//Global Declaration
private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header
private int count=0;
//Display only header on form load
//create header for the table
header = new Vector<String>();
header.add("Column1");
header.add("Column2");
...
model=new DefaultTableModel(data,header);
table = new JTable(model);
//in actionPerformed()
public void actionPerformed(ActionEvent ae){
if(ae.getSource()==yourMenuItem){
data=get();
for(int i=0;i<count;i++){
Object[] d={data.get(i).get(0),data.get(i).get(1),data.get(i).get(2)};
model.addRow(d);
}
}
}
This will help you to get data from database
get(){
Vector<Vector<String>> doublevector = new Vector<Vector<String>>();
Connection conn = dbConnection();//Your Database connection code
PreparedStatement pre1 = conn.prepareStatement("select * from Table");
ResultSet rs1 = pre1.executeQuery();
while(rs1.next())
{
Vector<String> singlevector = new Vector<String>();
singlevector.add(rs1.getString(1));
singlevector.add(rs1.getString(2));
....
doublevector.add(singlevector);
count++
}
return doublevector;
}
Upvotes: 1
Reputation: 301
call updateUI() or repaint() or revalidate(); as the table is added but the UI is not getting refreshed.
Upvotes: 1