data won't show up in jtable
Hello,
I've got a problem with putting data into a JTable with a defaultTableModel from another class.
Here is some example code
i've got a actionPerformed in class A
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
getDate();
verlofTabel test = new verlofTabel();
Object[] tempRow = new Object[]{"test1", "test2", "test 3", "test 4", "test5"};
test.addRow(tempRow);
this.setVisible(false);
}
}
The Object[] tempRow has to update my DefaultTableModel in class B
public class verlofTabel extends JPanel {
private Object[][] databaseInfo;
private JScrollPane pane;
public Object[][] getDatabaseInfo() {
return databaseInfo;
}
public void setDatabaseInfo(Object[][] databaseInfo) {
this.databaseInfo = databaseInfo;
}
private String[] columns = {"Begin datum", "Eind datum", "Uren berekend", "Werkelijke uren", "Opmerkingen"};
private MyTableModel dTable = new MyTableModel(databaseInfo, columns);
public verlofTabel()
{
JTable tabelVerlof = new JTable(dTable);
this.add(new JScrollPane(tabelVerlof));
}
public void addRow(Object[] row)
{
dTable.addRow(row);
System.out.println("row added");
}
}
Re: data won't show up in jtable
If you want help, you'll have to provide an SSCCE that demonstrates exactly what you're doing, in as few lines as possible. This should not be your whole program, but it should be enough for us to run it and see what's going on. Also, don't forget the highlight tags.
Re: data won't show up in jtable
Quote:
Originally Posted by
KevinWorkman
If you want help, you'll have to provide an
SSCCE that demonstrates exactly what you're doing, in as few lines as possible. This should not be your whole program, but it should be enough for us to run it and see what's going on. Also, don't forget the highlight tags.
I hope this will help.
Code :
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Medewerker extends JFrame implements ActionListener {
JPanel p1 = new JPanel();
JButton verlof;
//Components JPanel2
verlofTabel p2 = new verlofTabel();
public Medewerker()
{
p2.setVisible(true);
initGui();
this.setLayout(new GridLayout(1,2));
this.getContentPane().add(p1);
this.getContentPane().add(p2);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void initGui()
{
verlof = new JButton("Registreer verlof");
verlof.addActionListener(this);
p1.add(verlof);
}
public static void main(String[] args)
{
Medewerker test = new Medewerker();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == verlof)
{
ClassDateLayout dl = new ClassDateLayout();
dl.setVisible(true);
dl.pack();
}
}
}
Code :
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class ClassDateLayout extends JDialog implements ActionListener{
JButton ok;
public ClassDateLayout()
{
initGui();
this.setVisible(true);
}
public void initGui()
{
ok = new JButton("Ok");
ok.addActionListener(this);
add(ok);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
Object[] tempRow = new Object[]{"test1", "test2", "test 3", "test 4", "test5"};
verlofTabel test = new verlofTabel();
test.addRow(tempRow);
this.setVisible(false);
}
}
}
Code :
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class verlofTabel extends JPanel {
private Object[][] databaseInfo;
private JScrollPane pane;
public Object[][] getDatabaseInfo() {
return databaseInfo;
}
public void setDatabaseInfo(Object[][] databaseInfo) {
this.databaseInfo = databaseInfo;
}
private String[] columns = {"Begin datum", "Eind datum", "Uren berekend", "Werkelijke uren", "Opmerkingen"};
private MyTableModel dTable = new MyTableModel(databaseInfo, columns);
public verlofTabel()
{
JTable tabelVerlof = new JTable(dTable);
this.add(new JScrollPane(tabelVerlof));
}
public void addRow(Object[] row)
{
dTable.addRow(row);
System.out.println("row added");
}
}
[CODE]
import javax.swing.table.DefaultTableModel;
public class MyTableModel extends DefaultTableModel {
public MyTableModel(Object[][] data, String[] columnNames)
{
setDataVector(data, columnNames);
}
public boolean isCellEditable(int row, int column)
{
return false;
}
}
[/CODE]
Re: data won't show up in jtable
Re: data won't show up in jtable
Are you adding the data to the component that is visible in the GUI?
Re: data won't show up in jtable
Quote:
Originally Posted by
Motion
You must use Vectors
Incorrect. And I'm not sure what - if any - bearing this has on data in the model not being visible in the view.
Re: data won't show up in jtable
You must understand that coding is unique but one result is achieved at the end. DO NOT say incorrect for what u never tested.
That's what i normally use genius...
Re: data won't show up in jtable
Quote:
Originally Posted by
Motion
You must understand that coding is unique but one result is achieved at the end. DO NOT say incorrect for what u never tested.
That's what i normally use genius...
Your words: "you must use vectors." Read the API - this is incorrect because you can use arrays, and you could also use a List, or a Map, or a Set for that matter, presuming you extend the appropriate class and implement the appropriate methods.
Re: data won't show up in jtable
Quote:
Originally Posted by
copeg
Your words: "you must use vectors." Read the API - this is incorrect because you can use arrays, and you could also use a List, or a Map, or a Set for that matter, presuming you extend the appropriate class and implement the appropriate methods.
Whatever... you are a moderator..huh? act like one!
Re: data won't show up in jtable
Quote:
Whatever... you are a moderator..huh? act like one!
@Motion why the sassy backtalk?
You mislead the OP by saying: You must use Vectors
which isn't true. The API doc shows there are other choices. Besides, That is not the problem with the posted code.
Re: data won't show up in jtable
Quote:
Originally Posted by
Motion
Whatever... you are a moderator..huh? act like one!
This is a technical forum, not a kindergarten class. You were wrong, and copeg pointed that out so that anybody reading this forum doesn't get the wrong idea from you. It happens. Apologize, move passed it, and use it as a learning opportunity. Further bickering (and further posting of wrong advice as facts) will be cause for moderator action, so you'll get your wish.