I use JTable with horizontal and vertical scrollbars. My JTable has empty space after rows with data.
When I open panel that is situated down of my table it hides a part of JTable and scrolling appears on JTable. This is normal behavior, but then I close that panel, empty space without data becomes grey instead of white color.
This only happens when I have horizontal scrollbar on my JTable. I suppose I must force JTable to repaint, I tried resizeAndRepaint() on TableHeader and JTable but it didn't work.
Please help! Thanks!
Here is resize() code invoked if any resize action was performed on table
for (int i = 0; i < columsNum; i++){
TableColumn column = this.getColumnModel().getColumn(i);
int preferedSize = //current size
int minimumSize = // min size
if (minimumSize != ColumnSizeCalculator.UNDEFINED_WIDTH)
column.setMinWidth(minimumSize);
column.setPreferredWidth(preferedSize);
}
this.revalidate();
this.repaint();
By default, a JTable does not fill the viewport of a scrollpane in the vertical direction. Try to call
table.setFillsViewportHeight(true);
on your table, then repainting the table should work.
Related
I am having a problem on adjusting the height of a jtable whenever I insert rows. I have tried using setsize() and setPreferredScrollableViewportsize() for both table and scrollpane of the table. Could it be a problem in layout manager?
I also tried increasing the size of jpanel too upon inserting each row. BTW, the table lies in a panel and that panel lies in a jDialog. I am using free design in NetBeans for UI building.
Try the following... But make sure you editable option is false!
DefaultTableModel model = (DefaultTableModel)table.getModel();
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
final TableColumnModel colModel = table.getColumnModel();
for(int column=0; column<table.getColumnCount();column++){
int width = 15;
for(int row=0; row<table.getRowCount();row++){
TableCellRenderer render = table.getCellRenderer(row, column);
Component component = table.prepareRenderer(render, row, column);
width = Math.max(component.getPreferredSize().width+1, width);
}
if(width>300){
width = 300;
colModel.getColumn(column).setPreferredWidth(width);
}
}
I have a table right now that's displaying a list of classes and everything works fine except the formatting of it inside of the scroll pane. Here's what I have right now for it.
String[] columns = {"Class","Period","Size", "Honors/AP"};
JTable classTable = new JTable(new DefaultTableModel(new Object[][] {}, columns));
DefaultTableModel model = (DefaultTableModel)classTable.getModel();
//populating the table based on the number of classes that the teacher has
for (Class c: t.getClasses()) {
model.addRow(new Object[]{c.getClassName(),c.getPeriod(),c.getSize(),c.isHonorsAP()});
}
if (classTable.getRowCount()==0)
model.addRow(new Object[]{});
JScrollPane scrollPane = new JScrollPane(classTable);
classTable.setPreferredSize(new Dimension(700,100));
classTable.setPreferredScrollableViewportSize(classTable.getPreferredSize());
classTable.setFillsViewportHeight(true);
if (classTable.getPreferredSize().getHeight() < scrollPane.getPreferredSize().getHeight())
scrollPane.setPreferredSize(classTable.getPreferredSize());
So the problem on one end is that when there aren't enough rows to fill the preferred size of the table, it leaves extra space at the bottom and I'm trying to get that removed.
And then on the other end, when there are more rows that can fit, I need the size to stay but add in the scroll bar.
I'm really confused right now. Any help would be greatly appreciated
Im trying to learn Java and using the GUI as an upgrade to the "booring" C\C++
I had two panels, one of them with an input form, and one of them with a table which suppose to show the things I load from the input form.
But no idea why, the table expands and has a border line, moreover, it has a fixed size and whenever I expand the window, the table stays the same.
The "mainFrame" (the file that calls the c'tor) just initiate the table and add it to the layout so im sure its not happening there...
I've added the JTable class code and a picture showing whats happening
The link if it shows too small : http://oi62.tinypic.com/ok4hs5.jpg
And the code :
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class TableOutput extends JPanel {
private static final long serialVersionUID = 1L;
private JTable jt;
public TableOutput() {
String[] columns = {"Product ID" , "Date" , "Name" , "Address" , "Status"};
String[][] data = {};
jt = new JTable(data,columns);
jt.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
JScrollPane jsp = new JScrollPane(jt, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
add(jsp);
}
But no idea why, the table expands and has a border line, moreover, it has a fixed size
size - Do System.out.println(table.getPreferredScrollableViewportSize()). This is the method the scroll pane calls to get the preferred size for the table view. You will see that the default it 450x400. You can change it by overriding getPreferredScrollableViewportSize() on the table.
JTable table = new JTable() {
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(300, 300);
}
};
border - that's not the table border. It's actually the scroll pane border.
whenever I expand the window, the table stays the same.
JPanel has a default FlowLayout, which will always respect the preferred size of the contained component. If you want the table/scrollpane to stretch with the panel, use a layout manager like BorderLayout which will disregard the preferred size and stretch to fit. Keep in mind that if the panel is contained by a container with the FlowLayout, you will still get your current result. If the panel is just being added the frame with its default BorderLayout, then you should be fine. See more at How to use Layout Managers
Note: You may find out though that if you want to set the layout the BorderLayout, the preferred size of the scroll pane will have no effect. This will leave you will the same result - the table will big with empty rows and the border.
This is my issue. I have the code below. What it does is when I drag a column over the edge of the panel, my panel scrolls by itself.
However, the problem is, with this feature, every time I resize the column width, the panel also scrolls to the top.
For example, I have 1000 rows and I highlight the 999th row at the bottom. When I resize the column width, the panel scrolls to the top. How do I make the panel to stay at wherever I was?
I want my table to autoscroll horizontally, but not vertically.
table.getTableHeader().addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e)
{
Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
((JTableHeader)e.getSource()).scrollRectToVisible(r);
table.scrollRectToVisible(r);
}
});
You are resetting JTable's scroll bar position by this statement:
table.scrollRectToVisible(r);
Here x position of rectangle is correct, as it is same for JTableHeader and JTable. But y position should be different for both. But you are using y position from JTableHeader for JTable also.
To make it work correctly, get y position of JViewPort of JScrollPane used by JTable.
Use this y position for the rectangle used in above statement. This should correct the issue.
This code puts a JTable into a JFrame (sole component of the whole UI):
JFrame frame = new JFrame( "Title");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTable table = appender.createTable();
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
I also have some code to set the preferred size of the columns.
Nothing fancy. When the UI opens, the table takes up the whole view and I have no scroll bars. The combined preferred widths of the columns need more horizontal space than the windows is wide. Despite of that, there is no horizontal scroll bar and the columns are too narrow.
What do I have to do that
The columns are still resizable by the user
The current width is respected? I.e. when I change the width of one column, the width of the other columns should not change.
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
You can also add the scrollpane containing the table to a JPanel and then add the panel to the frame. That way the panel will change in size, not the scrollpane.