Horizontal scroll bar wont show on a dynamic table - java

I am trying to make a dynamically growing JTable, specifically, growing columns. However, I've stumbled upon a problem where If my JTable grows too big, all the columns are compressed and the cell wont be visible any more. I initially based code from the best answer on this. Additionally, I tried to altered the code and placed it on the viewport of the scrollpane and it did not work. This was it.
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
tableModel = new DefaultTableModel(new Object[] { "Initial Column" }, 5);
JTable table = new JTable(tableModel);
scrollPane.setViewportView(table);
JFrame frame = new JFrame("Table Column Add");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 600, 300);
frame.add(scrollPane);
frame.setVisible(true);
Afterwards, I saw another solution on dynamic JTables. I tried running the best answer on this link and it's the same thing, when then columns get too big, it will be compressed.
However, if you keep adding rows, the scroll bar would appear and the row height is maintained and not compressed.

Related

Change JFrame size dynamically to JTable contents

I've got myself stuck again.
This time I have a JTable inside a JFrame.
I basically just want there to be no "white-space" below the table generated.
When resizing, the rows don't change height, but the columns change width for some reason. Is it at all possible to not have the white space below?
I'd prefer not to have a scrollbar if it at all possible, and just show the entire table with the white space removed, so even when resized, it doesn't show up.
Quick Update: i used the gridlayout layout and it kind of worked, but my header has a bigger font than the table, resulting in the cells to be shown properly, but the headers being cutoff and displayed as "Hea..."
Is it at all possible to not have the whitespace below?
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
frame.add(scrollPane);
frame.pack();
frame.setVisible( true );

JTable bug - First row and column cell misaligned until you click the table

I have this strange bug where the cell in the first row and column in my JTable is misaligned. You can see in the image how the first cell's text is drastically misaligned.
When you click the table, the problem fixes itself.
Does anyone know why this could be occurring, or how it can be fixed? I have never had this issue with JTables before.
My code looks like the following. Note I have done this before but never experienced this bug.
JTable table = new JTable(myTableModel);
TableColumnModel colModel=table.getColumnModel();
colModel.getColumn(0).setPreferredWidth(200);
colModel.getColumn(1).setPreferredWidth(150);
colModel.getColumn(2).setPreferredWidth(150);
colModel.getColumn(3).setPreferredWidth(100);
table.setRowHeight(26);
JScrollPane tableScrollPane = new JScrollPane(table);
tableScrollPane.setPreferredSize(new Dimension(600, 300));

NetBeans JTable without Scroll Pane, Keep Header

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);
}

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

Need help understanding some row constraints behaviour

In a Java project I am working on, my constraints were originally set to:
JPanel panel = new JPanel();
String layoutConstraints = "fill";
String columnConstraints = "[right]rel[grow]";
String rowConstraints = "[]10[]";
panel.setLayout(new MigLayout(layoutConstraints, columnConstraints, rowConstraints));
From what I understand of the documentation the last gap and constraint will be repeated for every row in excess of the specification. (cf. "If there are fewer rows in the format string than there are in the grid cells in that dimension the last gap and row constraint will be used for the extra rows.")
So for all I understand, this should mean the row constraints would in fact be []10[]10[]10[] and so on.
My problem comes when I have a JTextArea in a JScrollPane with rows set to 3:
JLabel label = new JLabel("Text area");
JTextArea text = new JTextArea("Test", 3, 40);
JScrollPane scroll = new JScrollPane(text);
panel.add(label);
panel.add(scroll, "grow,wrap");
When used with a row constraint of []10[] is only 1 row high, yet when I change
String rowConstraints = "[]10[]";
to
String rowConstraints = "[]";
it suddenly uses the full 3 rows height that I specified for the JTextArea.
Have I misunderstood how the row constraints work or is there something else at work here that I totally failed to see/understand?
In both cases you are using only one layout row (i.e. row managed by MigLayout) to which you put JLabel (first column) and JScrollPanel (containing JTextArea, second column).
You can easily visualize your MigLayout rows and columns structure by simply adding "debug" to layout constraints:
String layoutConstraints = "fill,debug";
And launching application. I really recommend using this setting while working on your layouts.
Rows property that you set for JTextArea have nothing to do with layout rows. This property only tells JTextArea component what size it will try to have, as defined in javadocs:
java.awt.TextArea has two properties rows and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane (...)
But JTextArea rows doesn't correspond by any means to rows in container's (JPanel in your case) layout.
Please, learn more carefully (and experiment!) what exactly JTextArea row property means.

Categories

Resources