With the above code i want to close the popup right after click on one of the radio buttons but the popup stays open.
Is there something build-in in swing or must i call actionPeformed and close the popup explicity ?
public class NewClass extends JFrame {
NewClass() {
setSize(100,100);
JPopupMenu pop = new JPopupMenu();
JRadioButton log1 = new JRadioButton("Level 1");
pop.add(log1);
JRadioButton log2 = new JRadioButton("Level 2");
pop.add(log2);
JPanel p = new JPanel();
add(p);
p.setComponentPopupMenu(pop);
}
public static void main(String[] args) {
new NewClass().setVisible(true);
}
}
look at JRadioButton#addItemListener() and check if ButtonGroup is usefull in your case
EDIT:
Is there something build-in in swing or must i call actionPeformed and
close the popup explicity ?
I don't know..., good question, but if you adds JRadioButton to the JMenuItem then is build-in look here
I would have added actionlisteners on the buttons so when you press a button you then use:
public void actionPerformed(ActionEvent e){
pop.setVisible(false);
pop.dispose();
}
Related
I wrote the following code to have a JPopupMenu that allows multiple selection of different items.
The problem is that, as soon as the mouse enters one of the displayed JCheckboxMenuItems, the JPopupMenu gets closed. This issue doesn't occur if I replace JCheckboxMenuItem with, for example, JLabel but, for sure, JLabel doesn't work for my purpose.
Any idea of what could trigger this issue? Any idea of how this problem can be resolved in a better way? I apologize for the newbie question but I'm not a java developer. Thanks in advance for any help.
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedborder(),"Select Layers");
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
for (MyAction layer : layers) {
JCheckBoxMenuItem box = new JCheckBoxMenuItem(layer);
box.setIcon(new SquareIcon(myColor));
panel.add(box);
}
JPopup popup = new JidePopup();
popup.add(panel)
JButton button = new JButton("Layers");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
popup.show(button,0,button.getHeight())
}
});
Thats in the nature of JPopupMenus. They disappear when the invoker component loses the focus. But I found a little trick here.
Create your own class and extend it from JPopupMenu. Then override the setVisible method that it will only forward true to the super class and create an own method that will setVisible of the super class to false.
public class StayOpenPopup extends JPopupMenu{
public void setVisible(boolean visible){
if(visible == true)
super.setVisible(visible);
}
public void disappear() {
super.setVisible(false);
}
}
Then use it like this in your code
[...]
StayOpenPopup popup = new StayOpenPopup();
popup.add(panel);
[...]
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if(popup.isVisible())
popup.disappear();
else popup.show(button,0,button.getHeight());
}
});
Now one click on button will show it. And it will stay visible until next click on Button.
I am new to Java; but I'm having a blast. I feel like I'm missing something really simple; but I can't figure it out.
I want the RadioButtons to be displayed inside the JFrame."
public class HelloWorldFrame extends JFrame
{
private TitledBorder title;
public HelloWorldFrame()
{
super("Hello World! ");
JFrame helloWorld = new JFrame();
JLabel label = new JLabel();
title = BorderFactory.createTitledBorder("Language");
title.setTitleJustification(TitledBorder.LEFT);
label.setBorder(title);
add(label);
setSize(300, 200);
JRadioButton button1 = new JRadioButton("English");
JRadioButton button2 = new JRadioButton("French");
JRadioButton button3 = new JRadioButton("Spanish");
ButtonGroup bG = new ButtonGroup();
bG.add(button1);
bG.add(button2);
bG.add(button3);
label.add(button1);
label.add(button2);
label.add(button3);
helloWorld.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//The main class starts here
public class HelloWorldApp
{
public static void main(String[] args)
{
JFrame helloWorld = new HelloWorldFrame();
helloWorld.setVisible(true);
}
}
The first question is why? Why do you want to add the radio buttons to a JLabel?
Having said that, you can set the labels layout manager to something other then it's default value of null...
label.setLayout(new FlowLayout());
label.add(button1);
label.add(button2);
label.add(button3);
Next...your class extends from JFrame, but in your constructor, you are creating another JFrame, this means that when you do...
JFrame helloWorld = new HelloWorldFrame();
helloWorld.setVisible(true);
Nothing will be displayed, because nothing has been added to the frame.
Instead, make your class extend from something like JFrame and then add that to your JFrame in main
Updated
I just did some testing and doing this (adding buttons to a label) won't work well, as the JLabel calculates it's preferred size based on the text and icon, not it's contents (like something like JPanel would)...just saying...
I'm currently writing a Desktop-Application with Swing that consists of a MainFrame in which several InternalFrames will be running. I want to write the InternalFrames in separate classes for each to avoid having one huge MainFrame-class.
Now my problem is that I want to add an ActionListener to a MenuItem in a PopupMenu in the MenuInternalFrame that opens another InternalFrame in the MainFrame. The MenuInternalFrame appears, however when I click on the MenuItem nothing happens. When I put the code outside of the ActionListener the InternalFrame appears.
The problem must be something with the ActionListener. Could it be my workaround with the local class and a final instance to access the InternalFrame from within the mouseClicked-Method?
Here are the relevant parts of the code from the MainFrame-Constructor:
class InternalFrames {
TestInternalFrame test = new TestInternalFrame();
JDesktopPane desktopPane = new JDesktopPane();
}
final InternalFrames internalFrames = new InternalFrames();
internalFrames.desktopPane.setBackground(Color.WHITE);
Menu menu = new Menu();
menu.getMntmTestMenuItem().addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
internalFrames.desktopPane.add(internalFrames.test);
internalFrames.test.setVisible(true);
}
});
internalFrames.desktopPane.add(menu);
menu.setVisible(true);
Any ideas what could be the problem? Thanks in advance.
Dont use MouseAdapter on a JMenuItem, use ActionListener and add it via JMenuItem#addActionListener(...) instead:
Menu menu = new Menu();
menu.getMntmTestMenuItem().addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent ae) {
internalFrames.desktopPane.add(internalFrames.test);
internalFrames.test.setVisible(true);
}
});
Let's say I have a JMenuItem with a text inside "Exit", and a JButton with the text "Exit",
the command which JButton will use is System.exit(0), of course using Action Listener, Ok i Know, I can put the same codes when clicking on the JMenuItem, but isn't there a way, that when I click on the JMenuItem, the JButton is clicked so then the following commands are executed (JButton commands)?
What you can do is create an Action object, and use that for both your JButton and your JMenuItem.
Action exit = new AbstractAction() {
private static final long serialVersionUID = -2581717261367873054L;
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
};
exit.putValue(Action.NAME, "Exit");
exit.putValue(Action.MNEMONIC_KEY, KeyEvent.VK_X);
JButton exitButton = new JButton(exit);
JMenuItem exitItem = new JMenuItem(exit);
A good way to do this is to set the same ActionListener to both components. Like this:
JButton button = new JButton ("Exit");
JMenuItem item = new JMenuItem ("Exit");
ActionListener exitaction = new ActionListener ()
{
public void actionPerformed (ActionEvent e)
{
System.exit (0);
}
};
button.addActionListener (exitaction);
item.addActionListener (exitaction);
However, I would recommend against using System.exit (0). The better way of closing the program (which I assume is basically a JFrame) is by setting
frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE)
(where frame is the window of the program)
and calling frame.dispose () in the ActionListener.
I consider the best way to do this is to register the same ActionListener instance in the event listeners of both JMenuItem and JButton, it's like using the old Command design pattern.
I would not recommend trying to trick the events 'engine' like making the JMenuItem fire the event related to the pressing of the JButton, since that is not what's happening but what you seem to want is to reuse both actions to 2 distinct events.
You can try to save the button as a class field
private JButton button;
and insert in the click event handler of the menu item the code
button.doClick();
but the solution of SoboLAN is more elegant.
I have two JFrames. The first one is defined as public firstJframe and the second one is defined as public static final jFrame. I want to open the second JFrame on clicking a button on the first JFrame. How can I do this?
.setVisible does not work for this. I really don't know how to proceed with this.
Try calling revalidate() for the object you want to update(in your case the second frame).
Example:
JButton myButton = new JButton("Open new window");
JFrame newFrame = new JFrame("New Window");
myButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newFrame.pack();
newFrame.setVisible(true);
newFrame.revalidate();
}
});
Update
If that does not work, try calling this:
newFrame.invalidate();
newFrame.validate();
I propose:
under the button type:
this.dispose
new public static final jFrame.setvisible(true);