retrieve changed contents of a jtable contained in a jscrollpane - java

I have a Jtabbedpane that holds a JscrollPane which in turn holds a Jtable.
I would like to access the dynamically changed contents of the JTable via the JScrollPane. I have tried the following but I am stuck up at this point.
JScrollPane c = (JScrollPane)MyTabbedPane.getSelectedComponent()
Now c, the scrollPane has the changed table.
I saw that c.getComponents is returning two viewports and two scrollbars but not the Jtable.
Please tell me how can I access the JTable that the JScrollPane holds.

JViewport viewport = scrollPane.getViewport();
JTable table = (JTable)viewport.getView();

try this,to get table in JScrollPane...
jScrollPane.getViewport().getComponents()
Hope this helps...

Related

Scrollbar not appearing in the scrollpane when added to the panel

I have 2 tables each are added for a scrollpane however the scrollbar only apperas in one of them
JScrollPane pane = new JScrollPane(table);
JScrollPane pane_2 = new JScrollPane(table_1);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table_1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
panel.add(pane_2);
panel.add(pane);
the scrollbar only appears in pane_2 and not in pane
Any ideas how to fix this ?
Possible reason of such behavior could be different size of the tables. According to my experience JScrollPane shows the scrollbar only in case that its content is bigger and does not fit within the pane.
You can try to experiment with following JScrollPane properties:
horizontalScrollBarPolicy
verticalScrollBarPolicy
They can contain following values (e.g. in vertical case):
ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
They should control the behavior you ask about. The names are self explanatory I think.

JTable not visible in JPanel

As I am a beginner with java swing (I'm using netbeans), I am having trouble integrating tables in my gui.
I have a tab with a panel with research options in a db, and I want to add beneath this panel the produced table with the research results.
With swing, i have put a JPanel 'researchPanel' within my tab and what I do is the following:
table = my_research_function(options);
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
JScrollPane pane = new JScrollPane(table);
pane.setViewportView(table);
//resultsPanel.setLayout(new BorderLayout());
resultsPanel.add(pane);
// I'm just using this to ensure i get a table
// with correct results - works fine
JOptionPane.showMessageDialog(null, new JScrollPane(table));
As I saw in the produced code by swing, resultsPanel already has a layout. Just in case, i did a resultsPanel.setLayout(new BorderLayout()) and resultsPanel.add(pane, BorderLayout.CENTER) and I saw some shades in my panel but still no data.
Furthermore, in the Design tab of netbeans, I've set the panel to Auto-Resize both Vertical and Horizontal as the results table can be quite big.
What am I missing?
Swing component can have only one parent component.
You can't see your JTable in resultsPanel because of the following reasons
At first you create JScrollPane pane = new JScrollPane(table);
which add to resultsPanel,
but then you override parent component of your table here JOptionPane.showMessageDialog(null, new JScrollPane(table));.
So remove last line and your JTable will be visible.

Disable horizontal autoscroll in JTable

I have a JTable that is filled dynamically.
It has only two columns, the first is a boolean value and the second is a string.
The problem is that when I select a row in the string column, the table autoscrolls to the right hiding the first column.
How do I change this behaviour?
If I disable autoscroll, the user is not able to scroll to the bottom through the arrow keys.
Any ideas?
Thanks in advance.
Call:
jscrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
which will make sure no horizontal JScrollBar is created for the JScrollPane.
Perhaps also need to ensure the JScrollPane fits the JTable horizontally via:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane jscrollpane = new JScrollPane(table);

How to set JScrollPane Layout to be the same as JTable?

Tabel1.setModel(new DefaultTableModel(x,y));
JScrollPane pane = new JScrollPane(Tabel1);
When I run the program the scrollpane appears bigger than the table. How can I make the scrollpane layout the same as the table?
how i can make scrollpane layout the same of table??
if your JTable contains only a few rows
table.setPreferredScrollableViewportSize(table.getPreferredSize());
Tabel1.setModel(new DefaultTableModel(x,y));
use Java Naming Conventions correctly, then not Tabel1 but tabel1

JScrollPane Movement Restriction Query?

I have JTable of order nxn with the JScrollPane size being
JScrollPane jsc = new JScrollPane(table);
jsc.setPreferredSize(new Dimension(500,700));
The Scenario is this:: whenever I edit the last cell (it contains a JTextField) of the JTable by dragging down the ScrollBar and come out of Focus, the ScrollBar slides back the original position, i.e the beginning position. I would like the ScrollBar be in the place where it was set by the user before and after editing.... how can this be achieved?
Thank You In Advance....
you can able to determine scrollToVisible(JTable table, int rowIndex, int vColIndex), example here
Because JTable is a Scrolling-Savvy Client, it implements the Scrollable interface. You may get better results using the method setPreferredScrollableViewportSize() instead of calling setPreferredSize() directly on the scroll pane.

Categories