JMenuItems don't show in JDialog - swing

This is probably a dumb question, but I just can't see it! I have Swing app that uses a popup menu. It works fine, but I want to make the menu persistent (i.e. until I close it). I have basically changed the JPopupMenu to JDialog, and I am getting the JDialog panel, but the menu items are invisible! It's probably something very obvious, so I'll probably be embarrassed! Here is part of the code:
JDialog buildNewItemMenu(DrawFBP base) {
JDialog jd = new JDialog();
jd.setSize(200, 300);
JMenuItem menuItem = null;
JLabel label2 = new JLabel();
label2.setForeground(Color.BLUE);
JMenu menu = new JMenu();
jd.add(menu);
jd.setVisible(true);
menu.setVisible(true);
menu.add(label2);
menu.addSeparator();
menuItem = new JMenuItem("Component");
menuItem.addActionListener(base);
menu.add(menuItem);
....
menu.addSeparator();
menuItem = new JMenuItem("Enclosure");
menuItem.addActionListener(base);
menu.add(menuItem);
return jd;

I think I will close this - as I said in the comment, changing the JMenu to a JPanel and adding
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
fixed the problem, but I plan to tackle the problem a different way. Thanks anyway!

Related

Minimize a resizable JDialog

I'm working on an swing application with a main window (which extends JFrame) from which several child windows can be opened (more than 1 contemporarily).
These windows are all non-modal and resizable.
So far, I implemented these 'child' windows as a JFrame. However, I get a new icon on my Windows taskbar for each opened Window.
I therefore tried to implement these windows as a JDialog with type ModalityType.MODELESS.
Looks OK except that a JDialog has no minimize button.
Is there a way to resolve this?
I.e., I need to create non-modal and resizable child windows that can be minimized.
JInternalFrame is not an option since the main frame is not just a container with a JDesktopPane and child windows should be able to cross the borders of the main window.
For those interested:
Child windows register and unregister themselves on the main window when being opened/closed.
The main window has a menu with a 'Windows' item and child windows are added/removed from that menu upon registration/unregistration.
The user can switch between the various windows by selecting an item within this menu.
I am offering two suggestions.
A. Don't use the close button to get rid of the contents.
B. Set the type of child jframes to be utility.
I think having the JDialog close button destroy data is setting your users up for data loss. I would instead use the close to just hide the window, and then have controls inside of the dialog to cancel/finish/restart.
import java.awt.*;
public class DiFrame{
static JDialog log;
static JFrame ame;
public static void main(String[] args){
JFrame frame = new JFrame("Father of two");
JButton one = new JButton("dialog");
one.addActionListener( evt->{
if(log==null){
log = new JDialog(frame, "dialog child", false);
log.add(new JTextArea("fresh start"));
log.pack();
log.setVisible(true);
} else{
log.setVisible(true);
}
});
JButton two = new JButton("frame");
two.addActionListener( evt->{
if(ame==null){
ame = new JFrame("frame child");
ame.add( new JTextArea("fresh start") );
ame.setType(Window.Type.UTILITY);
ame.pack();
ame.setVisible(true);
} else{
ame.setVisible(true);
}
});
frame.add(one, BorderLayout.NORTH);
frame.add(two, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Click the dialog button and it shows the dialog. Then the text area can be modified. When the dialog is closed it can be re-opened.
Click the frame button and a jframe is shown. ( I actually cannot check if this shows up as a new application because it doesn't on my computer anyways. )

Libgdx stage 'frozen"

In my game there is a menu, in this menu, there are 4 tabs and each tab have its own stage with tables scrollpanes and buttons (is it a good idea ?)
My problem is that every stage seems "frozen", buttons are not responding and scrollpanes don't scroll
My menu structure :
Menu class
-> render a tab (render selectedTab, selectedTab is a Tab object (custom class) that is asigned with a specific tab (Ex: sele shopTab (extend tab class))
// menu class
private Tab selectedTab;
private Tab RecipeTab, SellTab, UpgradeTab, ShopTab;
// menu constructor
RecipeTab = new RecipeTab(viewport, sb, itemsdata);
SellTab = new SellTab(viewport, sb, hud);
UpgradeTab = new UpgradeTab(viewport, sb, itemsdata);
ShopTab = new ShopTab(viewport, sb);
selectedTab = RecipeTab;
// render
selectedTab.render(sr, delta);
// on tab change
public void setSelectedTab(Tab newTab) {selectedTab = newTab;}
setSelectedTab(ShopTab);
-> menu contain a navbar to switch tabs (the selected tab is asigned with another tab object)
I don't know if the issue comes from the stages or the actors. even a simple textbutton doesn't work
// how my stages are made
// constructor
this.stage = new Stage(viewport, sb); // ExtendViewport (same everywhere), Spritebatch
Gdx.input.setInputProcessor(this.stage);
// render
this.stage.draw();
this.stage.act(delta);
I figured it out.
The answer was simple, there was a conflict with the inputProcessor
i was seting the inputProcessoron each tabs
so i added a setInputProcessor() method to the tabs a called it on tab change

Vaadin Spring Navbar

I am trying to add a navbar (just like bootstrap's) for a Spring application but am not getting the nav menu to appear on the web page!
Can anyone please tell me what's wrong here?
Below is my code:
private Panel viewContainer;
private HorizontalLayout navbar;
private Button btnHome;
private Button btnNested;
private Button createNavigationButton(String caption, final String viewName) {
Button button = new Button(caption);
button.addStyleName(ValoTheme.BUTTON_SMALL);
// If you didn't choose Java 8 when creating the project, convert this
// to an anonymous listener class
button.addClickListener(event -> getUI().getNavigator().navigateTo(
viewName));
return button;
}
#Override
protected void init(VaadinRequest request) {
final VerticalLayout root = new VerticalLayout();
root.setSizeFull();
navbar = new HorizontalLayout();
navbar.setWidth("100%");
navbar.setDefaultComponentAlignment(Alignment.MIDDLE_RIGHT);
root.addComponent(navbar);
final Label brand = new Label("Nested demo");
brand.addStyleName(ValoTheme.LABEL_H1);
brand.addStyleName(ValoTheme.LABEL_NO_MARGIN);
navbar.addComponent(brand);
navbar.setComponentAlignment(brand, Alignment.MIDDLE_LEFT);
navbar.setExpandRatio(brand, 1);
btnHome = new Button("Home", FontAwesome.HOME);
btnHome.addStyleName(ValoTheme.BUTTON_BORDERLESS);
navbar.addComponent(btnHome);
btnNested = new Button("nested", FontAwesome.COFFEE);
btnNested.addStyleName(ValoTheme.BUTTON_BORDERLESS);
navbar.addComponent(btnNested);
viewContainer = new Panel();
viewContainer.setSizeFull();
root.addComponent(viewContainer);
root.setExpandRatio(viewContainer, 1);
}
Any hint is much appreciated.
Thanks
Henri's comment is almost certainly the correct answer.
Judging by the init(VaadinRequest) code you're using a UI class.
Without setContent(some components with visible stuff in them);
you won't see anything.
'you won't see anything' happens a lot in Vaadin when you're attempting new things, or doing proof of concept stuff. IMHO it's always a good practice to start with really dumb UI stuff e.g. setContent(new Label("TODO - implement this content-xxx"));
Using browser dev-tools is also a great idea. A quick select-element should show you that the UI div is empty, and allow you to start diagnosing.
TL;DR :-
UI is a ComponentContainer, so you need to - either
setContent(myLayoutWithStuff);
or
getContent().addComponent(myStuff);
to show someStuff.

Force JScrollPane and JPanel to repaint

I have a JScrollPane that holds a JPanel. The layout on the JPanel is a GridBagLayout. On that JPanel, I add a number of custom components - each is a JPanel with 3 JLabels.
The first time in the program I lay all of this out, it works fine. When I invoke the code to add another custom component to the JPanel, the panel appears empty, but I can determine by examining the contents of the JPanel that my components are actually there. If I resize the JDialog in which this all sites, the JPanel will paint properly. It also works if I scroll the JScrollPane horizontally even a tiny bit.
I use the same method for the initial layout as I do when adding an item.
I've tried various combinations of repaint(), invalidate() and doLayout() but nothing seems to work all the time. I've run into this situation before and have never been able to fully solve it. Any suggestions?
Running under OpenJDK 7u25. Below is the code that lays out the scroll pane and panel.
private void displayRelatedBug(ArrayList<Bug> a_bugs) {
// sort the bugs by ID
ArrayList<Bug> l_sorted = new ArrayList<>(a_bugs);
Collections.sort(l_sorted);
pnlRelatedBugs.removeAll();
pnlRelatedBugs.setLayout(new GridBagLayout());
GridBagConstraints l_gbc = new GridBagConstraints();
l_gbc.gridx = 0;
l_gbc.gridy = 0;
l_gbc.gridwidth = 1;
l_gbc.gridheight = 1;
l_gbc.anchor = GridBagConstraints.NORTHWEST;
l_gbc.fill = GridBagConstraints.NONE;
l_gbc.insets = new Insets(3, 4, 0, 0);
for (Bug r : l_sorted) {
pnlRelatedBugs.add(new RelatedBugDisplay(r, this), l_gbc);
l_gbc.gridy++;
}
// add a filler at the bottom to push it up
l_gbc.weighty = 1.0;
pnlRelatedBugs.add(new MMPanel(), l_gbc);
// add a filler on the right to push them left
l_gbc.weighty = 0.0;
l_gbc.weightx = 1.0;
l_gbc.gridx++;
pnlRelatedBugs.add(new MMPanel(), l_gbc);
// try in vain to make it show up!!!
pnlRelatedBugs.invalidate();
pnlRelatedBugs.doLayout();
pnlRelatedBugs.repaint();
scrollerRelatedBugs.doLayout();
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
pnlRelatedBugs.repaint();
scrollerRelatedBugs.repaint();
// this seems to help if the scroll bar is showing
scrollerRelatedBugs.getHorizontalScrollBar().setValue(1);
scrollerRelatedBugs.getHorizontalScrollBar().setValue(0);
}
});
}
Whenever you add/remove components from a visible panel, the basic code is:
panel.remove(...);
panel.add(...);
panel.revalidate();
panel.repaint();
Without a proper SSCCE we can't really tell what your code is doing.
If you do add/remove/replace/others actions with components on showing container, you must to revalidate and repaint your container, to which you add components for proper displaying.

change popup height

I have a Java swing popupmenu with a couple of menuItems.
Is there any way to increase the size of the popup keeping the same number of menuItems? For example, add 10px before the 1st menuItem and 10px after the last menuItem.
How can I do this? Can someone give me an hint?
Thanks
This is pretty simple. Since JPopupMenu is a Container the following code will produce the effect you desire
JPanel p1 = new JPanel();
p1.setPreferredSize( new Dimension(100,10));
JPanel p2 = new JPanel();
p2.setPreferredSize( new Dimension(100,10));
menu.add(p1);
menu.add(new JMenuItem("Item 1"));
menu.add(new JMenuItem("Item 2"));
menu.add(new JMenuItem("Item 3"));
menu.add(p2);