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.
Related
I want to have a calendar with entries overlapping a JTable.
That JTable is inside a scrollpane which again is inside a JLayeredPane.
setLayout(new MigLayout("", "[:20%:200px,grow][26%,grow][26%,grow][26%]", "[:15%:80px,grow][85%,grow]"));
add(layeredPane, "cell 1 1 3 1,grow");
layeredPane.setLayout(new MigLayout("", "[100%,grow]", "[100%,grow]"));
layeredPane.setBackground(Color.BLACK);
table = new JTable();
JScrollPane scrollPane = new JScrollPane(table);
layeredPane.add(scrollPane, "cell 0 0,grow",0);
I'm now calculating the bounds for my panel which is supposed to be an entry. When adding that entry to the JLayeredPane however, the whole layout is disrupted and it seems to be on the same layer. Even if my calculated values are wrong, that wrong result is still supposed to show up on a different layer.
JPanel panel = new JPanel();
panel.setBounds((int)position.getX(), (int)position.getY(), width, height);
panel.setBackground(Color.BLACK);
pane.add(panel, 300); // This is the JLayeredPane
When I go into fullscreen however, this happens:
Why is the pane not showing the panel on a different layer and why is my layout corrupted by it?
pane.add(panel, 300);
Well, that appears to be the code where you add your panel to the layered pane. Although elsewhere in your code you also have a variable called "layeredPane", so I'm not sure.
If that is referring to the layered pane then that is not how you add a component to the layer:
why would you use 300 for the layer? that is not a pixel location
The layer is specified by an Integer value, not an int.
Read the section from the Swing tutorial on How to Use Layered Panes for more information and a working example.
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.
It has been identified as a possible duplicate of the question:
JPanel Window not scaling when resize the main frame
I do not agree, because at that topic the problem is that the JPanel does not resize. In my case, the JPanel has no problems, but the JScrollPane is not getting resized!
I have a JFrame, there is a JPanel inside, containing a JScrollpane, which contains a JTable.
The problem is, that if the JFrame is resized, the JScrollpane stays the same, doesn't matter if the JFrame is made really small, so that the scroll pane does not even fit in the window or really big.
I want to make the JScrollpane adjust itself according to its JPanel, which gets adjusted by resizing of the window.
If the same thing is done by using a JTextArea inside the JScrollPane, everything works just fine -- resizing the JPanel resizes the scroll pane.
Do you have any idea why the JTable ruins the automatic resizing of the JScrollPane, and how to get around it? It seems, that the LayoutManager somehow is not able to handle it properly... (I also tried to add images showing what's happening, but I still don't have enough of reputation...)
Here is the code:
JFrame tableFrame = new JFrame("Test Table");
editPanel vadEditPane;
vadEditPane = new editPanel(vadData);
vadEditPane.setOpaque(true); //content panes must be opaque
tableFrame.getContentPane().add(vadEditPane, BorderLayout.CENTER);
public class editPanel extends JPanel {
public editPanel(dataConv vadData){
JTable vadTable;
String[] columnNames = {---SOME COLUMN NAMES---};
Object[][] data = {---SOME DATA---};
vadTable = new JTable(data, columnNames);
vadTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
vadTable.setFillsViewportHeight(true);
vadTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(vadTable);
scrollPane.setBackground(Color.RED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
add(scrollPane);
setBackground(Color.ORANGE);
}
}
I do not agree, because at that topic the problem is that the JPanel does not resize. In my case, the JPanel has no problems, but the JScrollPane is not getting resized!
If you read the comment in the first answer and take the time to understand the suggestion you will indeed find that the solution is the same.
The answer there states that a JPanel uses a FlowLayout (by default). The FlowLayout respects the preferred size of any component added to it. So the size of the table will never change even as the frame size changes.
So the solution suggests to change the layout manager of the panel so that the child components can be resized as the size of the panel changes. I would suggest using a BorderLayout.
I have a panel inside a JScrollPane and I dynamically populate the panel with components as the data gets received from the service. The panel uses GridBagLayout (but that should be irrelevant). For each record that comes back from the service, I create several components dynamically and append them to the bottom of the panel. Everything works fine, but the problem is that JTextArea that gets created for each record forces the main JScrollPane to scroll down and show the last added JTextArea, as shown here:
I tried to disable everything I could think of to dumb down the JTextArea, but still doesn't help
JTextArea descriptionArea = new JTextArea(project.getDescription().replace("<br>", "\n"));
descriptionArea.setEditable(false);
descriptionArea.setFont(thumbnailLabel.getFont());
descriptionArea.setLineWrap(true);
descriptionArea.setWrapStyleWord(true);
descriptionArea.setFocusable(false);
descriptionArea.setRequestFocusEnabled(false);
DefaultCaret caret = (DefaultCaret) descriptionArea.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
How can I prevent it from moving the scrollbar? I tried replacing JTextArea with JLabel and that works, but I can't get the JLabel to word wrap the text as good. Any ideas would be highly appreciated.
You could do something like:
Point p = srcollPane.getViewport().getViewPostition();
// add the components to the panel in the viewport of the scrollpane
scrollpane.getViewport().setViewPosition( p );
Now the scrollpane should be reset to its original position before you added the components.
I created a form. Actually it is 10 JLabels with each JLabel having a text field next to it.
consider,
JLabel_called_Name JTextField_to_obtain_name
JLabel_called_Phone JTextField_to_obtain_phone_number
and so on..
I usually position this in a JPanel and display it in a frame. But my panel and frame have height smaller than the size required to hold 10 of these Labels and Textfields.
So I wish to add them to a JScrollPane.
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box,
or adding JLabels to a JList.
However I would like to represent it in the format I showed above. A Jlabel beside a JTextField.
How can one acheive this?
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box, or adding JLabels to a JList.
You can add any component to a JScrollPane:
JPanel = new JPanel();
panel.add( label1 );
panel.add( textField1 );
JScrollPane scrollPane = new JScrollPane( panel );
The trick is choosing the correct layout manager for you panel. Read the Swing tutorial on Layout Managers to help you decide how to design the panel. You can also nest panels to get your desired layout.