JTable hidden under JScrollPane - java

I have a problem with JScrollPane - it looks like it lays over my JTable.
I have made a JTable without JScrollPane and everything seems to be fine - everything gets loaded as I wish and so on - but when I try to add JScrollPane everything goes wrong - only the column names are showing.
This is my code...
projectTable = new javax.swing.JTable();
ProjectTableModel ptm = new ProjectTableModel();
projectTable.setModel(ptm);
jScrollPane1.setViewportView(projectTable);
Is it a problem with positioning components? Could someone please help me with this problem.

The Oracle JTable tutorial contains a section about 'Adding a table to a container', where the container in the example is a JScrollPane

Related

How can I add multiple checkboxes to jScrollPane?

I have a Java connection with MySQL database. I inserted 30 variable to a MySQL table. I would like to create a graphical interface with Jframe where I can select the variables from the table using a checkbox.
I plan to do something similar as Java Swing Group of checkbox multiple selection handler
But I do not know how I can use it with NetBeans? I tried to add a newJFrame where I add a jScrollPane and I wanted to add multiple jCheckBoxes but I can add only one to jScrollPane.
Could someone let me know how I should add a group of checkboxes to jScrollPane?
You have to add a JPanel to your JScrollPane, then add checkboxes to that JPanel.
JPanel panel = new JPanel(layout);
panel.add(checkbox1);
panel.add(checkbox2);
JScrollPane scrollPane = new JScrollPane(panel);

Jtable columns not horizontally scrollable in NetBeans GUI builder?

I have the following JTable in my program which is placed inside a JScrollPane. I made it using the GUI builder provided by Netbeans.
The problem is some of the columns in the table is not visible and there is no horizontal scroll bar. I even set the horizontalScrollBarPolicy to ALWAYS and it didnt help. So how do I make the table scrollable?
As #Andrew Thompson mentiond you should provide some code that addresses the problem. Without seeing your code it's hard to find out what's the problem. But a common workaround about that is as the following:
If you have correctly added your JTable to the ViewPort of a JScrollPane like this:
JTable jtable = new JTable();
//...
JScrollPane sc = new JScrollPane(jtable);
//sc.setViewportView(jtable); <- This way is correct too
//
getContentPane().add(sc);
Then the most probable problem is about the AutoResizeMode of your JTable. Try this:
jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
Hope this would help you.

Move up and down in Jframe, how to make a lift?

I am trying to make a small program..When I execute it, I insert rows and columns and then when I click a button a matrix is appeared on the screen. My problem is that when the number of rows is bigger , I cant see all the matrix and other things that are below, and I can not move down in the JFrame. How can I do this? how can I add a lift, or elevator, to make possible to move up and down? I am not sure if it is called a lift ( the object at the right of the window that helps us to move up and down). Please help me...Thank you in advance.
"how can I add a lift, or elevator, to make possible to move up and down? I am not sure if it is called a lift ( the object at the right of the window that helps us to move up and down)."
Wrap the JTable in a JScrollPane
JTable table = new JTable();
JScrollPane scroll = new JScrollPane(table);
container.add(scroll);
Make sure you remove the table from the container, as a component can only have one parent container
See How to Use ScrollPanes
add(new JScrollPane(tableName));
you should put it in your constructor.
it worked for me, good luck :)

Java AbstractTableModel repaint();

I have a problem in my table model to update the data I print in it.
I have the class AgendaTableModel who is the table model, and the class Screen that is the main UI, when I press the button 'Listar Contatos' the table model should appear on JScrollPane in the center of the JFrame, but it continues blank.
What should be the problem ?
You should really post the code or better an SSCCE.
Here's the Oracle's tutorial on JTable.
I'll give you some hints:
Each JTable has a TableModel associated
You don't display the TableModel but the JTable, that is a view of your model
When you add components dynamically you should revalidate the parent component, so if you are adding a JTable somewhere, try to revalidate its container.
If you are trying to add the JTable to an already existing JScrollPane (empty or containing something else), consider to instantiate a new JScrollPane rather than updating its content.

JScrollPane layout

I want to add table2 into the scrollpanel (called feedback) which already has table1 in there. But only one table shows up. If I use feedback.add(table2), only the 1st table shows (I guess the 2nd table is behind the first one, but I don't know how to make the second one below the first one). if I use feedback.getViewport().add(table2, null), only the 2nd table shows. Do I need to use some layout manager here? i tried to search online about scrollpanel layout but didn't get any solutions. Could anyone tell me what's the problem or give me some related example links? Thanks a lot. The relative code are:
content = getContentPane();
content.setLayout(new FlowLayout());
scrollPane = new JScrollPane(tree);
feedback = new JScrollPane(table1);
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,scrollPane, feedback);
content.add(splitPane);
.
.
.
.
feedback.add(table2);
//i add this, but still doesn't work
content.add(table2);
Only a single component can be added to the "viewport" of a JScrollPane. This is done by using:
JScrollPane scrollPane = new JScrollPane( table );
or
scrollPane.setViewportView( table );
If you want multiple component to appear in a scrollPane then add the component to a panel first and add the panel to the viewport.
Read the JScrollPane API for more information and follow the link to the Swing tutorial as well for examples.

Categories

Resources