Jtable Not Showing

JTable not showing up on JFrame (Java)

I know thats not the problem tho because everything else shows up just fine

Oh... really? Not in my computer...

Let's have a picture of your actual GUI shown in my PC:

Sample Image

Does the GUI looks the same in your computer? I bet no.

But... why does it looks like that in my PC?

Well, as stated above in the comments by @MadProgrammer this is because of the setLayout(null); line. You might want to read Why is it frowned upon to use a null layout in Java Swing? for more information.

Now, that being said, you should also want to read and learn how to use the various layout managers that will let you create complex GUIs.

In your code you never set the location / bounds for scrollPane, and the size of it, so the component has a default size of 0, 0.

But... I think it's better to show you how you can get a really similar GUI (I'm in a hurry so I didn't make an even more similar GUI). You can copy-paste my code and see the same output (with slight differences because of the OS maybe) but text won't be cropped.

Sample Image

The code that produces the above image is this one:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class AccountCreator {

private JFrame frame;
private JPanel mainPane;
private JPanel topPane;
private JPanel tablePane;
private JPanel bottomPane;

private JLabel selectAccountLabel;
private JLabel userNameLabel;
private JLabel passwordLabel;
private JLabel homeWorldLabel;

private JTextField userNameField;
private JTextField homeWorldField;
private JPasswordField passwordField;

private JCheckBox membersBox;
private JCheckBox randomBox;

private JButton selectAccountButton;
private JButton addButton;
private JButton deleteButton;

private JTable table;

private JScrollPane scroll;

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new AccountCreator().createAndShowGui();
}
});
}

public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());

int rows = 30;
int cols = 3;

String[][] data = new String[rows][cols];

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
data[i][j] = i + "-" + j;
}
}

String[] columnNames = { "Column1", "Column2", "Column3" };

table = new JTable(data, columnNames);

scroll = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

table.setPreferredScrollableViewportSize(new Dimension(420, 250));
table.setFillsViewportHeight(true);

selectAccountLabel = new JLabel("Select Account");
userNameLabel = new JLabel("Username: ");
passwordLabel = new JLabel("Password: ");
homeWorldLabel = new JLabel("Home world");

selectAccountButton = new JButton("Select Account");
addButton = new JButton("Add");
deleteButton = new JButton("Del");

userNameField = new JTextField(10);
passwordField = new JPasswordField(10);
homeWorldField = new JTextField(3);

membersBox = new JCheckBox("Members");
randomBox = new JCheckBox("Random world");

topPane = new JPanel();
topPane.setLayout(new BorderLayout());

topPane.add(selectAccountLabel, BorderLayout.WEST);
topPane.add(selectAccountButton, BorderLayout.EAST);

tablePane = new JPanel();
tablePane.add(scroll);

bottomPane = new JPanel();
bottomPane.setLayout(new GridLayout(0, 5, 3, 3));

bottomPane.add(userNameLabel);
bottomPane.add(userNameField);
bottomPane.add(membersBox);
bottomPane.add(addButton);
bottomPane.add(deleteButton);
bottomPane.add(passwordLabel);
bottomPane.add(passwordField);
bottomPane.add(randomBox);
bottomPane.add(homeWorldLabel);
bottomPane.add(homeWorldField);

mainPane = new JPanel();
mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS));

frame.add(topPane, BorderLayout.NORTH);
frame.add(tablePane, BorderLayout.CENTER);
frame.add(bottomPane, BorderLayout.SOUTH);

frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

Also, you might have noticed that the main() method is different, well, the code inside it is placing the program on the Event Dispatch Thread (EDT).

So, be sure to include it in your future programs

JTable Not Showing Up In JPanel

I would suggest that the column's are being overridden by those reported back by the table model. You could instead use...

String[] colNames = {"Item", "Count"};
DefaultTableModel dtm = new DefaultTableModel(colNames, 0);

JPanel j = new JPanel(new BorderLayout());

JTable t = new JTable(dtm);
t.setBackground(Color.GREEN);

t.getTableHeader().setReorderingAllowed(false);
t.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

t.getColumnModel().getColumn(0).setPreferredWidth(113);

j.add(new JScrollPane(t), BorderLayout.CENTER);

Instead...

Without seeing the code you're using to put the table on the frame, it's difficult to comment further, however...

  • Avoid using setBounds, it's pointless in this context any way.
  • The background color will actually be defined more by the view port then the table or panel until the table is either configured to fill the empty space or has enough rows to fill the empty space

JTable not showing in java

frame.setLayout(null);

Don't use a null layout.

Because you don't use a layout manager the size of the scrollpane is (0, 0) so there is nothing to paint.

Swing was designed to be used with layout managers. Leave the default layout manager of the frame as the BorderLayout.

Also, a frame should be made visible AFTER all components have been added to the frame.

Read the section from the Swing tutorial on How to Use Tables for working examples to get you started. Use the structure of the code found in the tutorials and then modify it.

JTable not visible when added to Jpanel

can you try this one,

JTable jt=new JTable(rowsArray,columnNames);  
jt.setBounds(30,40,200,300); // you can put dimension as per your wish...

JScrollPane sp=new JScrollPane(jt);
jPanel2.add(sp);
// code fore set visible true to jpanel2...

JTable not showing

Invoking setModel() on the table should be sufficient, but you might call fireTableStructureChanged() on the model explicitly as a way to help sort things out.

Also, verify that you are working on the event dispatch thread.

Addendum: Here's an sscce that shows the basic approach.

Sample Image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/** @see http://stackoverflow.com/questions/8257148 */
public class SwapTableModel extends JPanel {

public SwapTableModel() {
final JTable table = new JTable(Model.Alpha.model);
table.setPreferredScrollableViewportSize(new Dimension(128, 32));
this.add(new JScrollPane(table));
final JComboBox combo = new JComboBox();
for (Model model : Model.values()) {
combo.addItem(model);
}
this.add(combo);
combo.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
Model model = (Model) combo.getSelectedItem();
table.setModel(model.model);
}
});
}

private enum Model {

Alpha(), Beta();
private DefaultTableModel model;

private Model() {
Object[] data = {this.toString()};
this.model = new DefaultTableModel(data, 1);
model.addRow(data);
}
}

private void display() {
JFrame f = new JFrame("SwapTableModel");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new SwapTableModel().display();
}
});
}
}

JTable header not showing

Start by removing

panel.add(table.getTableHeader());

The JTable is designed to add it's header to the JScrollPane. An instance of a component can only belong to a one parent/container, the above line is removing it from the scrollpane

Also, change this...

panel.add(table);

To

panel.add(scrollPane);


Related Topics



Leave a reply



Submit