updating view only after changing the size of JFrame - java

So I have a JFrame in which inside of it I have a JScrollPane and inside the JScrollPane I have a JPanel. I have a button click mouse listener that modifies the JPanel inside the JScrollPane. The code in the mouse listener is:
#Override
public void mouseClicked(MouseEvent arg0) {
searchResult.updateInfo();
}
The method updateInfo is adding a bunch of JPanel into the searchResult JPanel. After I click the button associated with this listener, nothing happens, but when I resize the JFrame.. it updates the view.. why is this?
I tried repaint the JFrame, but it did not solve my problem

After adding/removing components from a visible GUI you need to tell the panel to layout the components so the preferred size can be recalculated. You need to add:
searchResult.revalidate();
searchResult.repaint(); // sometimes needed
Then if the preferred size is greater than the size of the scrollpane scrollbars will appear.

make
searchResult.removeAll();
searchResult.updateInfo();
searchResult.validate();
if it doesn't work make like this
searchResult.removeAll();
searchResult.updateInfo();
searchResult.validate();
searchResult.repaint();

Related

GUI components disappearing when resizing

I'm writing a program that trades out the main component of the GUI when a button is pressed. To do this, I have multiple classes extending a component (JScrollPane) that can then be placed into my main class, which extends JFrame.
JScrollPane menu;
public MenuSystem()
{
menu = new OpeningMenu(this);
setSize(500,500);
setLocationRelativeTo(null);
setTitle("Menu system");
setDefaultCloseOperation(EXIT_ON_CLOSE);
loadInfo();
pane = getContentPane();
pane.setLayout(null);
pane.add(menu);
setVisible(true);
}
public void changeMenuTo(JScrollPane x)
{
pane.remove(menu);
menu = x;
pane.add(menu);
pane.repaint();
}
These bits of code control which JScrollPane is displayed on the GUI. I face two problems that I believe are related.
When I start the program, some of the components in the initial JScrollPane do not appear. I have several buttons and a JLabel in this pane, but only the first button appears. The rest of the buttons will appear when my mouse passes over them, but until then, they are invisible. This seems strange, because it suggests that my constructor is somehow at fault. when bringing up the same pane with the changeMenuTo() method, it appears perfectly.
Whenever I manually resize the screen, all components in the current pane disappear. It happens almost exactly like when I first start the program, buttons are invisible until my mouse passes over them. In this case the first button is also invisible.
Changing the layout has fixed the issue. Thanks for the suggestions.

Preventing components below panel to get hovered when panel is transparent

I have a JPanel, set to be transparent:
public SomePanel() {
setOpaque(false);
[...]
}
I have other JComponent instances under it (at the same location, but below it).
If I draw on the panel using paintComponent(g), putting my mouse on the panel still triggers mouseEntered and mouseExited events for other components below it.
How can I prevent components below the panel to fire mouse events if the non-opaque panel is visible? I am using setOpaque(false) because I need a transparent background, perhaps there is another way to achieve this?
One possible solution: give the covering JPanel its own MouseListener, one that is there to simply swallow the mouse events and prevent them from being transmitted. The code could be as simple as:
myPanel.addMouseListener(new MouseAdapter() {});

JScrollPane with transparent background and content

In my app, I show a popup dialog to show a large list of cards. I display them as images in many JLabel components in a JPanel subclass. I then put that object in a JScrollPane to allow for horizontal scrolling through the cards.
I want the unused space to be transparent with a dark background to show that what's behind it is disabled. I used setBackground(new Color(50, 50, 50, 200)) to achieve the look I want, but the content behind it does not redraw, so I get artifacting.
Here's what it looks like:
How would I go about fixing this? How do I get the content behind it to redraw when I scroll?
Thanks in advance.
Taking the window out of the equation for the momement.
The JScrollPane contains a JViewport which then contains you content. So you need to set your content pane to transparent, the viewport to transparent and then the scroll pane to transparent.
You can achieve this by using setOpaque(false) on each of these containers.
This will ensure that the repaint manager will now paint through the background.
The next problem is, Swing doesn't actually support "semi-transparent" components (that is, either it's opaque or transparent).
You can implement this by overriding the paintComponent method of the main component (the one on the viewport is probably sufficient)
Try the following...might give you some relief during scrolling.
You likely also have a problem when the main frame is maximized
or restored. You will need a listener for those events and a
similar fix.
jScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
#Override
public void adjustmentValueChanged(final AdjustmentEvent e) {
sevenWondersframe.repaint();
}
});
jScrollPane.getHorizontalScrollBar().addAdjustmentListener(new AdjustmentListener() {
#Override
public void adjustmentValueChanged(final AdjustmentEvent e) {
sevenWondersframe.repaint();
}
});

JScrollPane does not update scrollbar

I am trying to create a JScrollPane that contains a JPanel that will be increasing and decreasing in height. When it becomes larger than the size of the JScrollPane, it should create a vertical scroll bar which will allow me to scroll through the entire JPanel. However, I am having difficulty achieving this. Yes, I know I am not using LayoutManagers. No, I will not be using them, and I need a solution that does not involve their usage.
Here are the two button's AbstractActions that add and subtract from the JPanel:
class AddACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.setSize(420,info.getHeight() + 40);
info.add(new SubPanel); // Adds another JPanel into the main JPanel (for content input)
gui.repaint();
infoS.validate();
}
}
class RemoveACT extends AbstractAction
{
public void actionPerformed(ActionEvent e)
{
info.remove(subPanel()); // This would remove the last JPanel added to the main JPanel
info.setSize(420,info.getHeight() - 40);
gui.repaint();
infoS.validate();
}
And here is the code for the main JPanel and the JScrollPane:
final JPanel info = new JPanel();
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
info.setLayout(null);
info.setSize(420,600);
infoS.setLocation(10,80);
infoS.setSize(420,490);
gui.add(infoS); // gui is the frame's content pane (the overall JPanel)
This is the second project I've been trying to learn GUI by doing. I am a complete novice in Swing and am only intermediate in Java. Sorry if I am making a blindingly obvious mistake.
1) Use LayoutManagers (+1 to #kleopatra and #GagandeepBali comments)
The absence of LayoutManagers only guarantees your GUI's will look very trashy (especially when run on other OSes/builds) and being a Novice you should rather learn the correct way than learn the wrong way and get into bad habits like calling setSize() etc.
Have a read on these links to get you started:
A Visual Guide to Layout Managers
Concurrency in Swing
2) See this example for how to use a JScrollPane, it simply adds a JPanel with buttons to a JScrollPane which in-turn is added to the JFrame.
3) Also see this example for how to make the JScrollPane vertically scroll-able only.
4) For more on JScrollPanes have a look here: How to Use Scroll Panes.
5) As for how it interacts with LayoutManager, if you do not explicitly set its size via setPreferredSize(Dimension d) the scroll pane computes it based on the preferred size of its nine components (the viewport, and, if present, the two scroll bars, the row and column headers, and the four corners)
6) On your usage of validate():
validate() is used when new JComponents are added to a visible component
revalidate() is used when JComponent is removed/added from a visible component
revalidate() covers validate() too
Thus always use this:
//add or remove component(s)
revalidate();
repaint();
References:
http://www.daniweb.com/software-development/java/threads/405568/validate-vs-revalidate
LayoutManager is not required to solve the problem. The problem in Thrfoot's example is in these lines:
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
info.setLayout(null);
info.setSize(420,600);
The program appears to recognize there is a need for scroll bars (it would show the scroll bar if your setting was VERTICAL_SCROLLBAR_AS_NEEDED), but the actual scrolling does not work (the scroll bar slider is not there).
To fix this, first set the preferred size of info, then construct the infoS.
Example:
info.setPreferredSize(420,600);
final JScrollPane infoS = new JScrollPane(info, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
The idea is to set the preferred size of the info panel before it is used for the scroll pane. This is the same reason to set the size and location of infoS before adding to the gui:
infoS.setLocation(10,80);
infoS.setSize(420,490);
gui.add(infoS); // gui is the frame's content pane (the overall JPanel)

Why do the JList and TextArea components not resize using a GridBagLayout?

http://pastebin.com/VaaTRsuf
I would like to have the JList and JTextArea resize with the window, but the JPanel stays in the center.
Your LogView class extends JPanel and thus unless you change it, it uses JPanel's default layout, FlowLayout. Components held in a FlowLayout-using container do not change size when the container changes size, and so if you want this behavior, you don't need a component Listener -- you just need to change the layout manager for the LogView JPanel to BorderLayout or something similar that allows its held component to expand, that's it. One line of code:
public LogView(final JFrame contentPane) {
// .......
setLayout(new BorderLayout()); // add this, that's it
add(mainPanel);
}
Another option is to get rid of mainPanel as it doesn't appear to be necessary at all, to set the layout of your LogView object to be GridBagLayout and to add your components directly to the LogView object.

Categories

Resources