Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default Creating a custom Swing

    I'm trying to create a custom swing that contains a JTextField and a JButton.
    I have a class that represents the UI(BasicJPopupUI.java), another class that represents the Model(DefaultJPopupModel.java) and a class for the main swing(JPopup.java).

    The problem is that the text field and the button are frozen at run time. I can't write nothing in the text box and if I click the button nothing heapens even if I have a event connected to the button.

    Thank you.

    This is the code(partial):
    1) main class
    Java Code:
    public class JPopup extends JComponent  {
        private static final String uiClassID = "JPopupUI";
        public JPopup() {
            this.updateUI();
        }
        public void updateUI() {
            if (UIManager.get(getUIClassID()) != null) {
                setUI((JPopupUI) UIManager.getUI(this));
            } else {
                setUI(new BasicJPopupUI());
            }
        }
        @Override
        public String getUIClassID() {
            return uiClassID;
        }
    }
    2) UI class
    Java Code:
    public class BasicJPopupUI extends JPopupUI {
        protected JTextField textfield;
        protected JButton button;
        public void installUI(JComponent c) {
          this.jpopup = (JPopup) c;
           .....
            this.button.addActionListener(new java.awt.event.ActionListener() {
                @Override
                public void actionPerformed(java.awt.event.ActionEvent evt) { 
                     System.out.println("Executed");
                }
            });
        }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Not sure you code snippets are enough to work with. Can you make a SMALL complete program that compiles and executes to demonstrate your problem.

  3. #3
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default Full example

    Hi Norm.

    I attached a full example.
    The problem is that the JButton and the JTextField looks just like an image.
    If I attach an event to the button, that event doesn't work.

    PS: I posted this in advanced java but I'm not there yet. :)

    Thank you.
    Attached Files Attached Files

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Please post the code in one place here on the forum.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Can you make a SMALL complete program that compiles and executes to demonstrate your problem.
    Not your whole project with many source files.

  6. #6
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please post the code in one place here on the forum.
    I made a mutch smaller example.

    1) JPopupSimple.java - here I create the instance for the new swing and I display it.
    Java Code:
    package jpopupsimple;
    
    import java.awt.Container;
    import javax.swing.JFrame;
    import jpopup.JPopup;
    
    public class JPopupSimple {
        public static void main(String[] args) {
            JFrame main_frame = new JFrame();
            Container pane = main_frame.getContentPane();
            main_frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
            JPopup p = new JPopup();
            pane.add(p);        
            main_frame.pack();
            main_frame.setVisible(true);        
    
        }
    }
    2) JPopup.java - the swing class
    Java Code:
    package jpopup;
    
    import JPopup.BasicJPopupUI;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    
    public class JPopup extends JComponent  {
        public JButton button;
        
        public JPopup() {
            this.button = new JButton();       
            this.button.setText("ACT");
            this.updateUI();
        }
    
        @Override
        public void updateUI() {
                setUI(new BasicJPopupUI());
        }
    }

    3) BasicJPopupUI.java - maybe here is the problem
    Java Code:
    package JPopup;
    
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.LayoutManager;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.CellRendererPane;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.border.EmptyBorder;
    import javax.swing.plaf.ComponentUI;
    import jpopup.JPopup;
    
    
    public class BasicJPopupUI extends ComponentUI  {
        protected JPopup jpopup;
        protected JButton button;
    
        protected MouseListener mouseListener;
        protected CellRendererPane popupRendererPane;
    
        @Override
        public void installUI(JComponent c) {
            this.jpopup = (JPopup) c;
            //store the button in the local UI class
            this.button = jpopup.button;
            //add the button - not sure it is ok
            this.jpopup.add(this.button);
            this.popupRendererPane = new CellRendererPane();        
            this.jpopup.add(popupRendererPane);
    
            //I try here to attach a mouse listener to the button but is not working. The button text should change but it doesn't
            this.mouseListener = new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        jpopup.button.setText("test");
                    }
            };
            this.button.addMouseListener(this.mouseListener);
        }
        
        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            java.awt.Rectangle sliderBounds = popupRendererPane.getBounds();
            this.popupRendererPane.paintComponent(g, this.button,  this.jpopup, sliderBounds.x+50, sliderBounds.y,100, 20, true);
        }
    }
    Last edited by victorqedu; 08-28-2011 at 07:16 PM.

  7. #7
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    I thought from the beginning that the problem would be how to explain this situation in a simple manner, but I don't think I can do mutch better.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Please explain in detail what your code is supposed to do. You call method that call methods that call methods.
    That takes a lot of work to track through all those methods to see what the code is supposed to do.

    Better yet, put your comments in the source code so they are where I can see them as I try to work my way through your code.

  9. #9
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please explain in detail what your code is supposed to do. You call method that call methods that call methods.
    That takes a lot of work to track through all those methods to see what the code is supposed to do.

    Better yet, put your comments in the source code so they are where I can see them as I try to work my way through your code.
    I posted a mutch simpler code(I edited the code above - the original post with code).
    I also attached the files for this simple example here(they are still 3 files).
    I wrote a comment in the place where I think is the problem.

    What I want to do in detail:
    I want a custom swing that will have a JTextBox and a JButton. When I press the button I will see a JDialog(popup) with a grid that represents some rows from the database. When I double click a line in the grid the JDialog dissapears and the swing must contain the id of the selected row and the textbox must contain a description of the selected row.

    I have implemented the grid with the database connection, but the swing is the hard part...
    Right now I just want to implement the simplest possible custom swing. So the example contains just a button that does not work because the listener seems to have no effect.
    In fact the button acts like as a painted image.

    Thank you.
    Attached Files Attached Files
    Last edited by victorqedu; 08-28-2011 at 07:20 PM.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    What is this last posted code supposed to demo?
    I get a stack overflow from a recursive call in the paint method.
    When that is fixed the frame is minimized and when I use setSize() nothing shows.
    So far a big waste of time.

  11. #11
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    This is strange.
    I run the same code(I copy pasted the example) and I get no error.
    When the app starts I see a button(the window is minimize, I maximize it) but if I click it nothing heapens.

    I'll read some docs and I'll get back.

    Thank you.
    Last edited by victorqedu; 08-28-2011 at 07:47 PM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    This is a recursive call for me:
    Java Code:
       public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            //java.awt.Rectangle sliderBounds = popupRendererPane.getBounds();
            //this.popupRendererPane.paintComponent(g, this.button,  this.jpopup, sliderBounds.x+50, sliderBounds.y,100, 20, true);
            this.paint(g, this.button);   // THIS CALLS ITSELF<<<<<<<

  13. #13
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    You are right.
    The archive I posted was incorrect.
    I attached another one. It must run without error.
    The code from the archive is also posted in the forum(post nr. 6.)
    Attached Files Attached Files
    Last edited by victorqedu; 08-28-2011 at 08:13 PM.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Just post the changed code here in the forum so I can copy and paste it into my editor. I am keeping this source in one file with static inner classes.
    Don't post code that has not changed.

  15. #15
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    This is the code.

    1) JPopupSimple.java - here I create the instance for the new swing and I display it.
    Java Code:
    package jpopupsimple;
    
    import java.awt.Container;
    import javax.swing.JFrame;
    import jpopup.JPopup;
    
    public class JPopupSimple {
        public static void main(String[] args) {
            JFrame main_frame = new JFrame();
            Container pane = main_frame.getContentPane();
            main_frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
            JPopup p = new JPopup();
            pane.add(p);        
            main_frame.pack();
            main_frame.setVisible(true);        
    
        }
    }
    2) JPopup.java - the swing class
    Java Code:
    package jpopup;
    
    import JPopup.BasicJPopupUI;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    
    public class JPopup extends JComponent  {
        public JButton button;
        
        public JPopup() {
            this.button = new JButton();       
            this.button.setText("ACT");
            this.updateUI();
        }
    
        @Override
        public void updateUI() {
                setUI(new BasicJPopupUI());
        }
    }

    3) BasicJPopupUI.java - maybe here is the problem
    Java Code:
    package JPopup;
    
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.LayoutManager;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.CellRendererPane;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.border.EmptyBorder;
    import javax.swing.plaf.ComponentUI;
    import jpopup.JPopup;
    
    
    public class BasicJPopupUI extends ComponentUI  {
        protected JPopup jpopup;
        protected JButton button;
    
        protected MouseListener mouseListener;
        protected CellRendererPane popupRendererPane;
    
        @Override
        public void installUI(JComponent c) {
            this.jpopup = (JPopup) c;
            //store the button in the local UI class
            this.button = jpopup.button;
            //add the button - not sure it is ok
            this.jpopup.add(this.button);
            this.popupRendererPane = new CellRendererPane();        
            this.jpopup.add(popupRendererPane);
    
            //I try here to attach a mouse listener to the button but is not working. The button text should change but it doesn't
            this.mouseListener = new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        jpopup.button.setText("test");
                    }
            };
            this.button.addMouseListener(this.mouseListener);
        }
        
        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            java.awt.Rectangle sliderBounds = popupRendererPane.getBounds();
            this.popupRendererPane.paintComponent(g, this.button,  this.jpopup, sliderBounds.x+50, sliderBounds.y,100, 20, true);
        }
    }
    Thank you.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    What happens when you execute this code? What is its problem?

  17. #17
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What happens when you execute this code? What is its problem?
    The button has an event attached in BasicJPopupUI.java . The problem is that the event is not triggered.

    Java Code:
            //I try here to attach a mouse listener to the button but is not working. The button text should change but it doesn't
            this.mouseListener = new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        jpopup.button.setText("test");
                    }
            };
            this.button.addMouseListener(this.mouseListener);

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    Change the extends to extend JPanel vs JComponent.

  19. #19
    victorqedu is offline Member
    Join Date
    Aug 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Change the extends to extend JPanel vs JComponent.
    It works. :)
    You have been very patient.
    Thank you.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default

    I think there is much more to do for your code to work like you want.
    Here's what I have been working with:
    Java Code:
    //http://www.java-forums.org/advanced-java/48051-creating-custom-swing.html#post231521
    
    import java.awt.Container;
    import javax.swing.JFrame;
    
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.*; //LayoutManager;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.CellRendererPane;
    import javax.swing.*; //JButton;
    import javax.swing.JComponent;
    import javax.swing.border.EmptyBorder;
    import javax.swing.plaf.ComponentUI;
    
    
    public class JPopupSimple {
    
    
        public static void main(String[] args) {
            JFrame main_frame = new JFrame("Simple JPopup");
            Container pane = main_frame.getContentPane();
    //        main_frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);    ?????
            main_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
            JPopup p = new JPopup("test");
            pane.add(p, BorderLayout.CENTER);        
            main_frame.pack();
            main_frame.setVisible(true);        
    
        }
    
       //------------------------------------------------------------
       static class BasicJPopupUI extends ComponentUI  {
           protected JPopup jpopup;
           protected JButton button;
       
           protected MouseListener mouseListener;
           protected CellRendererPane popupRendererPane;
       
           @Override
           public void installUI(JComponent c) {
               System.out.println("installUI c=" + c);
               jpopup = (JPopup) c;
               button = jpopup.button = new JButton("ACT2"); // button was null here <<<<<<<<<<<<
               jpopup.add(button);
    
               //this.popupRendererPane = new CellRendererPane();        
               //this.jpopup.add(popupRendererPane);
       
               //I try here to attach a mouse listener to the button but is not working. 
               // The button text shoul change but it doesn't
               mouseListener = new MouseAdapter() {
                       @Override
                       public void mousePressed(MouseEvent e) {
                           jpopup.button.setText("test");
                       }
               };
               button.addMouseListener(mouseListener);
           }
           
           int cntr = 0; // DEBUG
           @Override
           public void paint(Graphics g, JComponent c) {
               super.paint(g, c);
               if(cntr++ > 100) { 
                 return;
               }
               System.out.println("paint cntr=" + cntr);
               //java.awt.Rectangle sliderBounds = popupRendererPane.getBounds();
               //this.popupRendererPane.paintComponent(g, this.button,  this.jpopup, sliderBounds.x+50, sliderBounds.y,100, 20, true);
    //           paint(g, button);   // Recursive loop here????????????
           }
       }   
    
       //------------------------------------------------
       static class JPopup extends  JPanel { //JComponent  {  //<<<<<<<<<<<<<<<<  What does JPanel have extra???
           public JButton button; 
           Dimension ourSize = new Dimension(100, 100);
           
           public JPopup(String i) {              // causes call to installUI
               super(); //<<<<<
               if(button == null)
                  button = new JButton("ACT1");  
               button.addMouseListener(new MouseAdapter() {  //<<<<<<<<<<<<
                 @Override
                 public void mousePressed(MouseEvent e) {
                     button.setText("test1");
                 }
               });
           }
    
           @Override
           public Dimension getPreferredSize() {  //<<<<<<<< called with pack()
             System.out.println("gPS");
             return ourSize;
           }
       
           @Override
           public void updateUI() {
               System.out.println("updateUI");  // Called twice ????
    //           Thread.dumpStack();  // show who called
               setUI(new BasicJPopupUI());
           }
       }
    }
    Good luck on your project.

Page 1 of 2 12 LastLast

Similar Threads

  1. Creating a custom panel:
    By xterradaniel in forum AWT / Swing
    Replies: 4
    Last Post: 10-03-2010, 08:40 PM
  2. Swing components vs. custom painting
    By Cruncher in forum New To Java
    Replies: 6
    Last Post: 05-06-2010, 07:16 AM
  3. Creating custom objects
    By coltragon in forum New To Java
    Replies: 11
    Last Post: 12-29-2009, 10:50 PM
  4. Swing themes & custom GUI's
    By Mr.Beans in forum AWT / Swing
    Replies: 3
    Last Post: 04-28-2009, 02:10 AM
  5. Creating Custom annotations
    By ajeeb in forum New To Java
    Replies: 2
    Last Post: 04-02-2009, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •