Difference between JScrollPane.setviewportview vs JScrollPane.add - java

I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview() method? What does add() method do and what does setViewProtView() do?

Basically, you should not use JScrollPane#add.
JScrollPane has a single component already attached to it, a JViewport, this is what the JScrollPane uses to display any component added to the view port.
setViewportView is a convenience method for for JScrollPane#getViewport#setView
The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add doesn't actually make any sense for it. The method is a consequence of extending from JComponent -> Container

Related

Dynamically adding content to JScrollPane in intelliJ GUI form [duplicate]

I faced this new thing today, and I didn't know why. When I want to show something in panel for example, I just add it to panel; but why I cannot add a table to scroll pane directly, and why I have to call the setviewportview() method? What does add() method do and what does setViewProtView() do?
Basically, you should not use JScrollPane#add.
JScrollPane has a single component already attached to it, a JViewport, this is what the JScrollPane uses to display any component added to the view port.
setViewportView is a convenience method for for JScrollPane#getViewport#setView
The basic concept comes down to the fact that from the scroll panes point of view, it will only show a single component, so add doesn't actually make any sense for it. The method is a consequence of extending from JComponent -> Container

JPanel data getting changed on mouse click

I am using three JButtons in my swing application. When I click on each button, the corresponding data (formatted in JTable with JScrollPane) will display on JPanel.
Problem: when I resize the JFrame, the JPanel is replacing with default button (the button which i was clicked first) information instead of current JButton information.
My sample code:
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
public void actioPerformed(ActionEvent e){
if(e.getActionCommand.equals("button1"))
JPanel.add(table1);
}
if(e.getActionCommand.equals("button2"))
JPanel.add(table1);
}.......
Resizing the JPanel will not suddenly replace components or add other components to the panel.
My best guess (and it is a guess due to the limited information in the question) is that none of your buttons actually work and just show the wrong information.
The code you posted only contains an add without any revalidation of the layout. Consult the javadoc of the Container#add method. When you resize, the layout gets revalidated and you see what is actually contained in the JPanel.
Possible solutions:
Call invalidate and repaint on your panel as well in your ActionListener
Use a CardLayout to switch between the different components
I personally prefer the CardLayout option, but it might depend a bit on the situation.
Note that in the code you posted, you add table1 for both buttons. Might be a copy-paste problem, or a problem with your actual code.
I was unable express problem clearly.Sorry for your inconvenience.
JPanel.removeAll() method has fixed my problem.
I added this method before adding any new component to JPanel. That fixes JPanel unexpected behavior.

addMouseListener() won't work with JPanel under JScrollPane

I add my customized panel onto JScrollPane. I also have another set of "tool" that will attach itself to my custom panel. The problem is, in these tool, it invokes attached.addMouseListener(this). Normally, everything would function well, but when I have it inside JScrollPane, it won't function at all. My deduction is JScrollPane never gives window focus to its child component. Is there a way to get this around without having to change my attachment procedure? I want my 'tool' to specifically attach to my custom panel, not the scrollpane.
I hope I have understood the problem.
What about adding a MouseListener to the JScrollPane and then dispatch the event to the JPanel?
Somenthing like this:
//JScrollPane Listener
public void mousePressed(MouseEvent me) {
jpanel.dispatchEvent(me);
}
It's a little bit tricky, but so you don't have to change the JPanel Listener.

JLabels dont show

So I have a panel and depending upon users entry they are populated with an x number of jlabels. Now the problem is, when the user entered information the labels successfully populate but they do not display properly in the panel; they don't even show.
Only when I resize the frame they appear?
It's been a while since I did Swing programming and I am trying to remember the method which you are supposed on a container after you add components. I think it's revalidate().
usually you have to call:
JPanel yourPanel = new JPanel();
yourPanel.repaint();
yourPanel.validate();
invalidate marks a component as needing to be relaid out soon because the component or one of its children has been resized or become visible or invisible. invalidate is called on a component automatically when children components are added/removed.
validate checks that a container is valid and if not, calls doLayout or invalidateTree to calculate positions and sizes of children components. validate effectively redoes the layouts if necessary, deciding on new sizes and locations of all components in the container.
After adding/removing components from a container, validate must be called on the parent to let the LayoutManager redo the layout. Calling validate does not schedule a repaint, so you may need to call repaint after the validate.

Swing add new Component

I need to add dynamicaly Components to JPanel, but if i make just add(Component) then component doesn't appears, if i make then JPanel.revalidate(); then it appears, but JPanel blinks, can I make it more fine, without blinking?
Hm, i have found solution,just after add(component); i have write component.repaint(); and it works, but now there is another Problem with Window resizing, if i resize window then all my added components disapeard!!!
This is basic, but you should make sure each component is
1) added from the EDT (see SwingUtilities.invokeLater())
2) added only once per instance
Might be a better idea to add the components on initialization and hide them, making them visible when needed.
Use the method Component.setVisible(boolean b) so show and hide components.
Edit:
I just tried a simple test class where I added random components to the main JFrame and it worked fine.
Try calling JFrame.pack() following JPanel.revalidate().
If this does not make a difference could you post some of your code where you add the dynamic components?
Another Edit:
Make your main component Implement the ComponentListener interface and implement the componentResized(ComponentEvent e) method to call JFrame.pack().
if you add a new component you have to call revalidate.
Example:
panel.add(new JButton(...), ...);
panel.revalidate();
Make sure you're calling this from within the EDT.
If it still flickers have a look at panel.setDoubleBuffered.
Hope that helps, even though example code from your side would be nice to actually see the effect you are describing.

Categories

Resources