问题
i write this code for showing strings in a table.
but it doesnt shown and has no effect.
what is problrem?
public pamnel() {
initComponents();
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
jTable1 = new JTable(data, columnNames);
}
Edit: I add Jtable on a panel.
in the main add panel to a jframe.
JFrame frame = new JFrame();
frame.add(new pamnel());
frame.setVisible(true);
frame.setSize(600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The table is shown but data isn't show. the row and column of table is empty!
回答1:
You have to add the JTable to its parent-component and setVisible().
回答2:
Try something like this, just to make it run. Notice you drop the voidin the pammel method and a main was added.
import javax.swing.*;
class TableDemo {
JTable jTable1;
public static void main( String ... args ) {
TableDemo tableDemo = new TableDemo();
tableDemo.pamnel();
JFrame frame = new JFrame();
frame.add( new JScrollPane(tableDemo.jTable1) );
frame.pack();
frame.setVisible( true );
}
public void initComponents(){
}
public void pamnel() {
initComponents();
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
jTable1 = new JTable(data, columnNames);
}
}
回答3:
Did you really write the code yourself? I remember this from an oracle swing tutorial. Anyway, you've not added the JTable to a component. Observe below source
package components;
/*
* SimpleTableDemo.java requires no other files.
*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;
public SimpleTableDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
The JTable is added to the JPanel in the SimpleTableDemo constructor. Then the JPanel is set as the content pane in the for the main JFrame, which is made as frame.setVisible(true). This happens in the createAndShowGUI method. The reason your code doesn't display the JTable is because a JTable is an abstract widget. You need to add the abstract widget to a component, such as a JFrame (in the above case) for it to be displayed.
回答4:
It seams like the new JTable is not being added to pamnel.
Probably another instance, not initialized with data, is being added in initComponents()
private JTable jTable1 = new JTable(); // empty table
public pamnel() {
initComponents();
String[] columnNames = ...
Object[][] data = ...
jTable1 = new JTable(data, columnNames); // new instance created here
// nothing happens with that new instance
}
private void initComponents() {
...
add(jTable1); // added empty table, not the one created above (constructor)
...
}
The add method does not add a reference to the variable to the component, it only adds the value of the variable. So, if the variable is changed, the component still contains the previous (old) value.
Change the order of create-add:
private JTable jTable1;
public pamnel() {
initComponents();
}
private void initComponents() {
...
createTable();
add(jTable1);
...
}
private void createTable() {
String[] columnNames = ...
Object[][] data = ...
jTable1 = new JTable(data, columnNames);
}
来源:https://stackoverflow.com/questions/5106889/how-to-insert-data-into-jtable