NetBeans JTable without Scroll Pane, Keep Header - java

I'm attempting to add a JTable with NetBeans GUI builder. The table is inside a panel which already has a scroll bar. Netbeans automatically creates all JTables inside of a JScrollPane.
However, I want the table to scroll as part of a larger page. I do not need two scroll bars.
My problem is: if I get rid of the scroll pane, I lose the header.
Is there a way to have a table with a header inside the Netbeans GUI builder?

My problem is: if I get rid of the scroll pane, I lose the header.
JTableHeader is (automatically) visible in the case that JTable is inside JScrollPane
you have to get JTableHeader from JTable and place this Object programatically by using LayoutManager to the container, I'm strongly recommend to use BorderLayout or GridBagLayout for this container

If you add JTabel directly to container(not to JScrollPane) you need to add JTableHeader by yourself(programatically ), try next example:
public static void main(String[] args) {
JTable t = new JTable(new Object[][]{{1,2,3}},new Object[]{"1","2","3"});
JFrame frame = new JFrame();
frame.add(t.getTableHeader(),BorderLayout.NORTH);
frame.add(t);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

Related

Frame and components in java

I know frame is act as container object that we can add components , there are many of components in swing package ( like button , menu and table etc...)
I have tried to add table to frame but I could not , why ??
unless I have add scroll pane object to frame then add table to scroll pane so what is difference between frame and scroll pane ? why I cannot add table directly to frame and what are real benefits of scroll pane for table
in this code , the table is shown with very small size and in corner and when add table directly to frame I cannot see any thing
Test t1=new Test(Test.Accounts());
ScrollPane scroll=new ScrollPane();
scroll.add(t1);
add(scroll);
note Test class is act as table class because I have extend it with JTable class and this code inside JFrame constructor
You have to tell Java how large you want the Table to be. You can do so by either set the size manually with t1.setBounds(x, y, width, height), or by using a layout. If you want to set the bounds manually you have to check that no layout is being used. You can do so by setting the layout to null: frame.setLayout(null);.
You can set the container's layout by frame.setLayout(new BorderLayout()); where in this case I'm using a BorderLayout (https://docs.oracle.com/javase/tutorial/uiswing/layout/border.html). Now you can add the table and tell Java to fill the whole frame by typing frame.add(t1, BorderLayout.CENTER). CENTER can be replaced by NORTH, EAST, SOUTH, WEST depending on where you want the table to be.

How to Set Bounds of JTable while Set Layout is Null?

In my
public UI(){
I have JTextField,JButtons and labels
I have also setLayout(null);
}
Now I'm Trying to create a JTable
JTable table = new JTable(data, headers);
But it wont display until I take out setLayout
For all the buttons,text boxes and labels I have setBounds();
How can I display the JTable while the setLayout is Null?
Don't use null Layout, instead use LayoutManagers. Java created for you amazing layouts, you just have to use it, and let the layout do your job instead of hard working of calculating the position and size.
That's my answer, but if you insist of using it, just call setBounds(x,y,width,height) method for the JTable.
But again, don't use absolute positioning(null Layout).
//....
JScrollPane scrol = new JScrollPane(table);
scrol.setBounds(table.getBounds());
//....

Removing JTable from content panel

I'm using a JTable to graphically display search results for an application I'm developing. I would like the ability to remove a table once it's not longer needed, and then replace it with a newly created table. Here is how I'm currently adding the table to my JFrame:
userLibrary = new CustomLibrary(users, LIBRARY_WIDTH, LIBRARY_HEIGHT);
userLibrary.setOpaque(true);
userLibrary.setBounds(LIBRARY_START_X, LIBRARY_START_Y, LIBRARY_WIDTH, LIBRARY_HEIGHT);
getContentPane().add(userLibrary);
My custom Library (Which extends JPanel) does the following:
public CustomLibrary(LinkedList<User> usernames, int width, int height) {
CustomTable table = new CustomTable(userRows,columnNames);
table.setPreferredScrollableViewportSize(new Dimension(width, height));
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
// Add the scroll pane to this panel.
add(scrollPane);
}
now this all works fine and displays my table, but I can't figure out how to completely remove the table from my content pane. I've tried calling
getContentPane().remove(userLibrary);
But this appears to do nothing.
So my general question is. How do I completely remove a table from my JFrame once I've already created it and drawn it?
I would like the ability to remove a table once it's not longer needed, and then replace it with a newly created table.
The easiest way is to just replace the TableModel of the JTable:
table.setModel( yourNewlyCreatedTableModel );
No need to create a JTable or a JScrollPane.
To remove and replace it with another component:
contentPanel.remove(table);
contentPanel.add(component, BorderLayout.CENTER);
After adding/removing components you should do:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
Usually a JTable is displayed in a JScrollPane. So maybe a better solution is to use:
scrollPane.setViewportView( anotherComponent );

make a jscrollpane (containing a jtable) fill its container

I have a JTable with autoResizeMode set to AUTO_RESIZE_LAST_COLUMN. I have added it to a panel by creating a JScrollPane with the JTable as its child widget, and then adding the JScrollPane to the panel.
I would like to set the size of the JScrollPane viewport to that of the parent JPanel, and have the JTable resize its last column dynamically.
JPanel have got implemented FlowLayout by defaut, you can place JScrollPane to the BorderLayout.CENTER
I think if you make the JPanel use GridLayout(1,1) and add the JScrollPane to it then you will get the desired result.
The first answer on this link How to make a JTable fill entire available space? worked perfectly for me.
I did the following
myPanel = new JPanel(new GridLayout());
myJtable = new JTable(MyTableModel);
myPanel.add(new JScrollPane(myJtable));

How does one make an applet scroll?

I have been trying to use JScrollPane with my applet, but it doesn't work. I have a JPanel to which I add 20 buttons, and I want to be able to scroll up and down this JPanel. Instead, the scrollbars do not appear. When I use setPreferredSize they still did not appear even though only about 3 of the buttons are being displayed and the rest are cut off. If I do not use setPreferredSize, there might as well not be any scrollbars because I have to make the window big enough to see all of the buttons. If I try to make the scrollbars always visible, they appear but do nothing. I tried the exact same code with JFrame instead of Applet, and it works fine, but I need it to be an applet. Is JScrollPane incompatible with applets? (Note: I tried to use an outer JPanel and add the scrollable panel to it, but it changed nothing). Changing the layouts also doesn't fix the problem. I have attached a simplified version of my code, but it displays the same errors.
Here is the code I have:
JPanel scrollPanel = new JPanel();
scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
JScrollPane scroll = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
for (int i = 0; i < 20; i++) scrollPanel.add(new JButton("Button " + i));
add(scrollPanel);
validate();
You never all the panel to the scroll pane
You never add the scroll pane to the applet
The basic code should be:
JScrollPane scrollPane = new JScrollPane(...);
scrollPane.setViewportView( scrollPanel );
add( scrollPane );
You are adding components to a Panel so you shouldn't expect to see a scroll pane wihout showing the scrollpane. What you want to do is then add that panel to a scrollpane which would be added to ur main container.
From your code, i think your problem is
add(scrollPanel);
your should be doing this
add(scroll);`
This is because you only added the panel to the frame which does not contain any scrollpane. Since you have added the panel unto the scrollpane, you should add the scrollpane and not the panel to the main container.
It sounds like you are using Swing components (JScrollPane, JPanel, ...) in an AWT container (Applet). Try using JApplet instead.

Categories

Resources