Change JPanel at runtime with a button - java

Hello I have a main layout made in this way:
| |
Main pane |Menu pane|
| |
Now, menu pane is made just of button: clicking on a button switches main panel with another panel.
On click event is made this way:
public void actionPerformed(ActionEvent evt){
mainPanel = new MyNewPanel();
this.revalidate();
}
But, for some reason, main panel does not change!

You are not setting the main component of your container. You need to add your new panel and call validate() on that container.
Note, depending on your the layout of your container you may need to remove the currently visible component first.
CardLayout could manage all this for you.

Related

Vaadin HybridMenu shift with scrollpanel

I want to add scrollbar to my web app so I can see every components on my browser.
Red rectangle in picture below is Grid. Grid is placed inside class that extends VerticalLayout and implements View.
Green color shows my scrollbar which is added to HybridMenu instance.
HybridMenu root = HybridMenuBuilder.get()
.setContent(new VerticalLayout())
.setMenuComponent(EMenuComponents.LEFT_WITH_TOP)
.setConfig(menuConfig)
.build();
root.addStyleName("scrollpanel");
root.setSizeFull();
super.setContent(root); //class extends UI
scrollpanel definition in css:
.scrollpanel > div {
overflow: auto !important;
}
When I reduce height of browser window, menu bar is also being scrolled:
I tried to set scrollbar to VerticalLayout, but it seems like id didn't capture that grid was partly hidden. (I've setted everything in VerticalLayout to full size - How to add scrollbar to Vaadin layout)
//extends VerticalLayout implements View
private void init()
{
this.addStyleName("scrollpanel");
this.setSizeFull();
this.setMargin(false);
grid.setSizeFull();
this.addComponentsAndExpand(grid);
this.setExpandRatio(grid, 0.7f);
this.setComponentAlignment(grid, Alignment.TOP_LEFT);
}
Generally I found some not expected (at least for me) behaviour of components in VerticalLayout. They acts differently according to Layout root
(Example with VerticalLayout as root).
Questions:
How can I avoid hiding part of hybrid menu when scrolling?
Am I missing something about adding scroll panel to this VerticalLayout?
#Edit:
I found that this problem doesn't occurs if HybridMenu content is not fullSized or content is not set.
On the other hand scrollbars are not available without it.
So, creating HybridMenu the way that code below shows, solves problem with scrollpanels, but restores previous problem (layout type doesn't change anything).
AbsoluteLayout test = new AbsoluteLayout();
test.setSizeFull();
HybridMenu root = HybridMenuBuilder.get()
.setContent(test)
.setMenuComponent(EMenuComponents.LEFT_WITH_TOP)
.setConfig(menuConfig)
.build();

How to put two components at the same place

I am currently building a game and I don't know how to make a main menu. I don't know how to put the text on the GIF background. Now the thing is when I try to put them on I try this code:
private void initializeDisplay() {
playBtn.setBorderPainted(false);
playBtn.setFocusPainted(false);
playBtn.setContentAreaFilled(false);
playBtn.setText("PLAY");
playBtn.setFont(new Font("Poiret One", Font.TRUETYPE_FONT, 27));
playBtn.setForeground(Color.WHITE);
mainPanel.add(playBtn);
mainPanel.add(bgImg);
mainPanel.setLayout(new GridBagLayout());
bgImg.setLocation(new Point(200, 200));
mainPanel.setOpaque(true);
mainPanel.addKeyListener(new Keyboard());
mainPanel.addMouseListener(new Mouse());
mainPanel.setFocusable(true);
}
Also when I do that I use a GridBagLayout for the layout type but it seems that I'm not get the result I want, the problem is the button not the image in the background because that's appearing but the button isn't appearing.
You could also add an image to a jlabel's icon field, and then add the label to your panel's content pane.
This post explains it more:
How to set a background picture in JPanel

Using a JScrollPane in a JEditorPane

Hi so I'm struggling to apply a JScrollPanel within a JEditorPane.
I have a main content window (which has a scroll bar) and within that a JEditorPane that I would like to scroll slide horizontal and vertical. So far I have
JEditorPane monApp = new JEditorPane();
and have tried
ScrollablePanel panel = new ScrollablePanel();
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );
along with a few other things but couldn't seem to get any of them working.

Vaadin ActionHandler on Panel doesn't work

I use Vaadin 6.8.8. I'm trying to add action handler to panel (to get context menu). The panel takes all the place of the window, but I caused the browser context menu when i do mouse right button click.
public class MyPanel extends Panel {
public MyPanel() {
...
addActionHandler(new Action.Handler(){
...
});
setSizeFull();
}
}
What is wrong?
Those action related methods in Panel are for keyboard shorcuts. Only Tree, Table and TreeTable are the core components supporting context menu. To add a context menu to your panel, take a look at the ContextMenu add-on.

How do I add a double-click event listener to tray icon using SWT?

I have an SWT tray icon which I've created with the following snippet of code:
itmTrayItem = new TrayItem(trySysTray, SWT.NONE);
itmTrayItem.setToolTipText("My App");
itmTrayItem.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event) {
mnuPopup.setVisible(true);
}
});
Right clicking on the tray icon brings up a context menu. I'd like to add a double-click event to the tray icon so that when the icon is double-clicked I perform some action. How can I do this?
I haven't understood how I was use the mouse listener as I've been finding some parts of the SWT docs lacking in examples.
Thanks
try the SWT.DefaultSelection event, it may do the trick

Categories

Resources