I have Swing application which manipulate shapes. In my toolbar I have a zoom function that the user activate by clicking on a button, then the cursor of the mouse changes to a magnifier which is an image.
My problem is actually the cursor, for some raisons, when I set the cursor on the panel displaying the shapes, I can't save my model and I get the java.io.NotSerializableException: sun.awt.image.ToolkitImage exception.
My model
public class Document implements IDocObservable,Serializable{
...
public void updateCursor() {
Iterator<IDocObserver> iter = docObservers.iterator();
while (iter.hasNext()) {
iter.next().docCursorChanged();
}
}
...
}
The action
public class ZoomInAction extends AbstractAction {
public void actionPerformed(ActionEvent arg0) {
...
Application.getInstance().getActiveDocument().updateCursor();
}
}
The display Panel (note : if I comment the setCursor(..) line, I'am able to save )
public class Window extends JPanel implements IDocObserver{
...
public void paint(Graphics g){
//drawing the differents shapes
}
#Override
public void docCursorChanged() {
setCursor(Utile.getZoomInCursor();
}
}
}
The class that provide the cursor
public class Utile {
private static Image zoomIn = toolkit.getImage(Utile.class.getResource("/images/zoomin_mouse.png"));
...
public static Cursor getZoomInCursor() {
return toolkit.createCustomCursor(zoomIn, hotSpot, "");
}
}
The writing of the object is a standard Java methode with outStream.writeObject(doc);
thanks
You aren't just serializing a model, you are serializing a list of IDocObservers, which includes Window extends JPanel implements IDocObserver. IOW you are serializing a JPanel. Don't do that: see the warning at the top of the Javadoc. You don't need to save the observers along with the observable, surely: can't you make that list transient?
Related
I'm developing a dynamic menu using Primefaces and JSF 2.2.
The problem is that it's not inserting the menu id. Looking primefaces' code it hits a code that will always be false:
BaseMenuRenderer:
protected boolean shouldRenderId(MenuElement element) {
if(element instanceof UIComponent)
return shouldWriteId((UIComponent) element);
else
return false;
}
TieredMenuRenderer:
writer.startElement("li", null);
if(shouldRenderId(submenu)) {
writer.writeAttribute("id", submenu.getClientId(), null);
}
So, I decided to override primefaces' TieredMenuRenderer, but my override constructor classe is called but the override method is never called.
Here's how I set my facesconfig.xml
<render-kit>
<renderer>
<component-family>org.primefaces.component</component-family>
<renderer-type>org.primefaces.component.TieredMenuRenderer</renderer-type>
<renderer-class>ui.jsf.TieredMenuRenderer</renderer-class>
</renderer>
</render-kit>
My override class:
public class TieredMenuRenderer extends org.primefaces.component.tieredmenu.TieredMenuRenderer {
#Override
protected void encodeElements(FacesContext context, AbstractMenu menu, List<MenuElement> elements) throws IOException {
System.out.println("----------- TEST --------------");
super.encodeElements(context, menu, elements);
}
Sysout is never print.
Does anyone know what i'm doing wrong?
Thanks!
Edit:
Add ID to DefaultMenuItem:
DefaultMenuItem item = new DefaultMenuItem();
item.setId(menuItem.getMenuId());// just return a string value.
Adding menu xhtml, the "menucontroller.model" is a primefaces MenuModel which I use a DefaultMenuModel :
Iterating over renderer kit, When I execute the following command, returns the correct renderer-type org.primefaces.component.TieredMenuRenderer
Iterator<String> renderKit = kit.getRendererTypes("org.primefaces.component");
When I execute the following code returns my qualified classname ui.jsf.TieredMenuRenderer#64baec0e:
Renderer renderer = kit.getRenderer("org.primefaces.component", "org.primefaces.component.TieredMenuRenderer");
First of all, My renderer was not overriding the correct renderer. I was overriding org.primefaces.component.tieredmenu.TieredMenuRenderer when I should override org.primefaces.component.MenubarRenderer.
Then, to correct the primefaces id problem I did the following in my rendered class:
#Override
protected boolean shouldRenderId(MenuElement element) {
return true;
}
#Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
AbstractMenu menu = (AbstractMenu) component;
encodeMarkup(context, menu);
encodeScript(context, menu);
}
EncodedEnd was calling "generateIds()" from primefaces which override my ids.
Full rendered class:
import java.io.IOException;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import org.primefaces.component.menu.AbstractMenu;
import org.primefaces.model.menu.MenuElement;
public class MenuRenderer extends org.primefaces.component.menubar.MenubarRenderer {
#Override
protected boolean shouldRenderId(MenuElement element) {
return true;
}
#Override
public void encodeEnd(FacesContext context, UIComponent component) throws IOException {
AbstractMenu menu = (AbstractMenu) component;
encodeMarkup(context, menu);
encodeScript(context, menu);
}
}
http://imgur.com/VT3JHH8
This is what I am trying to create essentially to start my menu based game. I am wondering if my approach is good. I have created a frame class "SonomaRoller" shown below. In that class it adds "frame" my "panel1" as shown in the diagram. As of now I also have my images drawn in that class that appear as "panel2". I want the user to be able to switch between panels with the buttons in panel1. Some of the panels where panel 2 is will have buttons of their own as well. What is the best approach for this? Should i make seperate classes for the panels and add them in JFrame? Should I switch between panels using JFrame since it adds the first panel? I have included my code below for my two classes. I also have a Jtext pane below where the panels are.
Thanks in advance
___________________MY FRAME___________________
package sonomaroller;
import javax.swing.*;
import java.awt.*;
import static javax.swing.JFrame.*;
public class SonomaRoller extends JFrame {
public static Dimension size = new Dimension(550,550); //Dimension of Frame
public static String title = "Sonoma Roller v0.00" ;
//Creates new object f to create the window
//boolean
public boolean addShop=false;
public SonomaRoller(){
setTitle(title);
setSize(size);
setResizable(false);
setLocationRelativeTo(null); // null centers window on screen
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
init(addShop);
}
public void init(boolean addShop){
frame panel=new frame();
panel.setLayout(null);
add(panel);
setVisible(true);
}
public static void main(String[] args) {
SonomaRoller object1=new SonomaRoller();
}
}
___________________MY PANEL w/buttons___________________
package sonomaroller;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultCaret;
import javax.swing.text.StyledDocument;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
public class frame extends JPanel implements Runnable {
public void run(){
}
public frame(){
loadpics();
attackButton();
magicButton();
travelButton();
shopButton();
textField();
}
public void paintComponent(Graphics g){
}
}
public void textField(){
}
public void attackButton(){
}
public void magicButton(){
}
public void travelButton(){
}
public void shopButton(){
}
public void loadpics(){
Sonoma = new ImageIcon("C:\\Users\\Camtronius\\Documents\\NetBeansProjects\\SonomaRoller\\src\\sonomaroller\\testImage.jpg").getImage();
System.out.println("image loaded");
loaded = true;
repaint();
}
}
Can you not use ActionListener to set the panel visible or invisible by pressing a button. For example:
panel_1Button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
panel1.setVisible(false)
panel2.setVisible(true);
}
});
I am working with some strange legacy code. They have a custom object which implements a JPanel. This JPanel object is a secondary popup screen within the main application. The issue I'm having is to detect when the secondary popup screen is closed.
I tried to implement a WindowListener for the class, but when I try to add it, there is no JFrame associated with this object. I am assuming this is because they are using a custom object and it is an embedded popup screen.
I tried to retrieve a JFrame using:
JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
which fails on a NullPointerException. I have no idea why it's so difficult to detect the right hand corner "x" close button on this page! I should mention that they were able to add Mouse and Key Listeners to the table which is embedded within the JPanel. But the outside listener for the entire window is causing me troubles.
(Please bear with me, this is my first stackoverflow post and I am new to Swing.)
Thanks so very much!!
Try to call getParent() for that strange panel. It should return the parent GUI component. If this is still not your frame but some intermediate panel instead, call getParent() on it as well. The top level component returns null.
Component p = strangePanel;
while ( p != null && ! (p instanceof Window))
p = p.getParent();
((Window) p ).addWindowListener(..);
Cannot understand why you are getting "NullPointerException" at:
JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
In two cases this can happen:
JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(null);
In your case, this is not possible as you have used this as a parameter.
Second, are you doing some other operations in above code line, like:
JFrame parentFrame = ((JFrame) SwingUtilities.getWindowAncestor(this)).someOperation();
In this case, if your this object represent the top window then you are supposed to get "NullPointerException" because ancestor of top parent is returned as "null". In other cases, I suspect you will get this exception.
Can you post a block of code where you are getting exception.
For this answer I'm making a minor assumption that the Nullpointer is not being thrown at the line that you mentioned, but rather when you attempt to add the WindowListener to the parentFrame. This is most likely because you're calling
JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
before the JPanel has been added to the JFrame hierarchy.
Here's a rought code sample on how you could work around this. The thought it to wait for the panel to be notified that it has been attached to the JFrame somewhere in its hierarchy.
package test;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class HierarchyTest extends JPanel {
protected static void loadApp() {
HierarchyTest test = new HierarchyTest();
JFrame frame = new JFrame();
frame.add(test);
frame.setSize(200, 200);
frame.setVisible(true);
}
/**
* #param args
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
loadApp();
}
});
}
public HierarchyTest() {
this.addHierarchyListener(new HierarchyListener() {
#Override
public void hierarchyChanged(HierarchyEvent e) {
// This can be optimized by checking the right flags, but I leave that up to you to look into
boolean connected = setupListenersWhenConnected();
if (connected) {
HierarchyTest.this.removeHierarchyListener(this);
}
}
});
}
protected boolean setupListenersWhenConnected() {
JFrame parentFrame = (JFrame) SwingUtilities.getWindowAncestor(this);
if (parentFrame == null) {
return false;
}
parentFrame.addWindowListener(new WindowAdapter() {
#Override
public void windowClosing(WindowEvent e) {
// Implementation here
System.out.println("This window is closing!");
}
});
return true;
}
}
I have created a JFrame by creating a class(Display.java) that extends JFrame class. In that class I have created a JPanel object.
class Display extends JFrame {
JPanel jp= new JPanel();
And in another class(TestBroadCastNode.java) I am adding JButtons to that JPanel by using reference to the class Display as shown below:
class TestBroadCastNode {
Display disp;
Graphics g =disp.getGraphics();
JButton bt = new JButton("One");
disp.jp.add(bt);
}
Now I am not able to display the buttons. Help me.... If you want I can send you the complete file.
I tried to match your code as close as possible, but since you only gave limited snippets I had to make some assumptions. But the following code shows the button just fine:
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestClass {
public static class Display extends JFrame {
public JPanel panel = new JPanel( );
public Display(){
super( "TestFrame");
getContentPane().add( panel );
}
}
public static class TestBroadCastNode{
Display display;
public TestBroadCastNode( Display aDisplay ) {
display = aDisplay;
display.panel.add( new JButton( "One" ) );
}
}
public static void main( String[] args ) throws Exception {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
Display display = new Display();
display.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
new TestBroadCastNode( display );
display.pack();
display.setVisible( true );
}
} );
}
}
I am trying to synchronize popups of two combo boxes - call them comboBox and mirrorComboBox.
I want to set popup of mirrorComboBox visible when popup of comboBox becomes visible.
I tried to implement this behavior by adding PopupMenuListener to comboBox and calling mirrorComboBox.setPopupVisible(true) when popupMenuWillBecomeVisible event occurs. It works fine, but unfortunatelly it causes another problem - the popup of comboBox will never be hidden! Event the popupMenuWillBecomeInvisible method is never called after the popup is once set visible.
How to sync popups visibility of two combo boxes?
Here is my implementation:
import java.awt.FlowLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
public class MirrorPopupsMainFrame extends JFrame implements PopupMenuListener {
public static void main(String[] args) {
new MirrorPopupsMainFrame().setVisible(true);
}
JComboBox comboBox;
JComboBox mirrorComboBox;
public MirrorPopupsMainFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildUi();
}
protected void buildUi() {
String[] items = new String[]{"item1", "item2", "item3"};
comboBox = new JComboBox(items);
comboBox.addPopupMenuListener(this);
mirrorComboBox = new JComboBox(items);
setLayout(new FlowLayout());
add(comboBox);
add(mirrorComboBox);
setBounds(0, 0, 300, 200);
}
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
// Not calling the following line will cause
// comboBox's popup to hide correctly.
mirrorComboBox.setPopupVisible(true);
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
mirrorComboBox.setPopupVisible(false);
}
#Override
public void popupMenuCanceled(PopupMenuEvent e) {}
}