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());
//....
Related
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);
}
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 );
ive got a JPane within a JScrolledPane. When i add content to JPane , JScrollPane doesnt show scrollbar. I tried repaint() and revalidate() but it didnt help.
static void ladowaniePaneli()
{
int b;
for(b=0;b<o;b++)
{
bgPanel[b] = new JBackgroundPanel();
nowyPanel[b] = new JPanel();
((FlowLayout)bgPanel[b].getLayout()).setVgap(0);
nowyPanel[b].setPreferredSize(new Dimension(790,518));
nowyPanel[b].setOpaque(false);
vertical[b] = new JScrollPane(nowyPanel[b]);
vertical[b].setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
vertical[b].setPreferredSize(new Dimension(789,517));
vertical[b].setOpaque(false);
vertical[b].getViewport().setOpaque(false);
bgPanel[b].add(vertical[b]);
}
}
It makes sense that scrollbars are never seen since you restrict the size of the contained component so that it's always trivially larger than the scrollopane's viewport:
nowyPanel[b].setPreferredSize(new Dimension(790,518));
Solution: don't do that.
if i dont use setPreferredSize method components wont warp to another line
You can try the Wrap Layout.
pairs should be warped to new line if they exceed JScrollPane width
Components are layed out individually. I you want a group of components to wrap then you would need to add the components to a separate panel first. Then add the panel to the panel using the WrapLayout.
I need to display account names for my program and I want to do this using JTree inside a JScrollPane.
Here is my code:
public void loadAccounts() {
accountsRoot = new DefaultMutableTreeNode("Accounts"); //create root
accountsRoot.add(new DefaultMutableTreeNode("Fred")); //add one element
//for testing
accounts = new JTree(accountsRoot);
accountsPane = new JScrollPane(accounts);
accountsPane.add(accounts); //don't think this is necessary
canvas.add(accountsPane);
accounts.setBounds(0, 0, accountsPane.getWidth(), accountsPane.getHeight());
accountsPane.setBounds(460, 270, 240, 410);
accounts.setVisible(true);
accountsPane.setVisible(true);
}
Because I am not using a layout I set the bounds manually.
I can't seem to get it to show. I want to eventually end up loading the accounts from a while so I figure JTree would be pretty easy for that,
accountsPane = new JScrollPane(accounts);
accountsPane.add(accounts); //don't think this is necessary
Not only is that not necessary but it will mess things up as this in effect adds your accounts JTree to multiple containers -- to the JScrollPane's viewport (good) and to the JScrollPane itself (bad). Don't do that. Add it to the JScrollPane's viewport only either through the JScrollPane's constructor as shown on the first line above, or by calling setViewportView(...) on the JScrollPane object after creating it.
Edit: another problem is your use of setBounds(...). You shouldn't be doing this but rather should be using layout managers to allow the proper viewing of your components. You will also need to call revalidate() and repaint() on whatever container is accepting the JScrollPane.
I want to put objects coming out of a JTable, layered on top of it, so using a JLayeredPane seems natural. However, getting this to paint properly, do the headers properly etc is very hard. How do I do this so that:
The Row headers appear and match up when it scrolls
The column headers appear and match up when it scrolls
The table paints properly
resizing doesn't mess everything up
Note that because JDesktopPane extends JLayeredPane, the answers to this question also allow you to have a JTable (or any other component) behind a JDesktopPane.
Similar but not identical questions which help:
Java - Is it possible to put a JLayeredPane inside JScrollPane? and How to display animation in a JTable cell and Swing GUI design with JScrollPane and JLayeredPane.
To do this properly there are three separate issues to consider: Sizing, Headers and UI changes.
Sizing
To scroll and paint properly the JScrollPane needs to know the size and preferred size of the component inside it, in this case the JLayeredPane. But you want the size to be set by the table, as other Components will be floating on top of the table. In this case the easiest way is to make the JLayeredPane delegate size related properties to the JTable as follows.
final JTable table = new JTable();
JLayeredPane layers = new JLayeredPane() {
#Override
public Dimension getPreferredSize() {
return table.getPreferredSize();
}
#Override
public void setSize(int width, int height) {
super.setSize(width, height);
table.setSize(width, height);
}
#Override
public void setSize(Dimension d) {
super.setSize(d);
table.setSize(d);
}
};
// NB you must use new Integer() - the int version is a different method
layers.add(label, new Integer(JLayeredPane.PALETTE_LAYER), 0);
JScrollPane scrolling = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrolling.setViewportView(layers);
If you didn't want the JTable to be the thing which determines the size of the JLayeredPane then it needs to be determined in some other way, and so does the table's size. Both will need setPreferredSize() and setSize() called on them explicitly.
Headers
As the JTable is no longer the viewport, you'll need to link the headers yourself. The following code will work:
scrolling.setColumnHeaderView(table.getTableHeader());
scrolling.setRowHeaderView(rowHeader);
UI
Also note that JTable does some nasty code in configureEnclosingScrollPane() and configureEnclosingScrollPaneUI(). If you want to get UI style changes to work properly, then you'll have to override these methods, but I haven't worked out how to do this yet.