Jtable Not Showing Up on Jframe (Java)

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 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 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

Java JTable not showing the records

while (res.next()) 
{
System.out.println(res.getString(1) + "\t" + res.getString(2) + "\t" + res.getString(3));
}

After doing the query you read all the data and display it on the console.

while (res.next()) {              
row = new Vector(c);
for(int i=1; i<=c; i++){
row.add(res.getString(i));
}
data.add(row);
}

Then later you try to read the data into the Vectors. Problem is that there is no data in the ResultSet because you have already read all the data.

Get rid of the first loop.

If you want to see the values of the ResultSet then put the System.out.println(...) statement in the second loop.

JTable not showing in JPanel

After adding the new content to the panel, you need to repaint the panel to the screen:

fileTablePanel.revalidate();
fileTablePanel.repaint();

It works when you popup a new JFrame since it's the first time the JFrame is painted to the screen.



Related Topics



Leave a reply



Submit