Results 1 to 4 of 4
  1. #1
    mlfatty is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default [SOLVED] Adding JLabel to JFrame

    This is s program that allows a user to draw shapes by inputting the specifications(size, location, color etc). The user can select a shape and edit or delete it after creating it. I created JLabels and added MouseListeners to them. These JLabels are created when the main program runs but they are not shown yet. When a user creates/draws a shape, a label's setText is set and the label is displayed(the arrayList is updated as well). The problem is that I dont know how many shapes the user is going to create, so I dont know how many labels I need. And all these labels need MouseListeners because clicking on a label means selecting the shape associated with. So you can click on a label and you will be able to edit or delete the shape associated that label. I tried creating the labels in the actionEvent method for the shapes but they cannot be displayed if i create them there.

    Java Code:
    
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Graphics.*;
    import javax.swing.*;
    import java.util.*;
    import java.awt.Color.*;
    import javax.swing.text.*;
    import java.text.*;
    import java.awt.Font.*;
    /**
     * Write a description of class Ellipse here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
     public class drawingFrame extends JFrame implements ActionListener  {
            
            Color bdcol, outcol ;
            public int x, y, w, h, x1, x2, x3, y1, y2, y3 ;
            public int numEl = 0 ;
            boolean sel ;
           
            Container myContent = getContentPane() ;
            
           /** Panel for user to see textual out */
            private JPanel interfacePanel ;
            
             /** Panel for user to see textual out */
            private JPanel drawingBoard ;
            
            /** Label for SHapes */
            public JLabel rec = new JLabel("", JLabel.CENTER) ;
            
            /** Label for SHapes */
            public JLabel ell = new JLabel("", JLabel.CENTER) ;
            
            /** Label for SHapes */
            public JLabel tri = new JLabel("", JLabel.CENTER) ;
    
            
           /** Panel for menu buttons */
            private JPanel menuPanel ;
            
            /** Panel for menu buttons */
            private JMenuBar myMenu = new JMenuBar();
            
            /** Menu header */
            private JMenu drawShapes = new JMenu("drawShape"); 
        
            /** Menu Item to draw Ellipse */
            private JMenuItem drawEllipse = new JMenuItem("Ellipse");
            
            /** Menu Item to draw Rectangle */
            private JMenuItem drawRect = new JMenuItem("Rectangle");
            
            /** Menu Item to draw Triangle*/
            private JMenuItem drawTriangle = new JMenuItem("Triangle");
            
            /** Menu header */
            private JMenu editShape = new JMenu("editShape");   
            
            /** Menu Item to draw Rectangle */
            private JMenuItem edit = new JMenuItem("Edit");
            
            /** Menu Item to draw Rectangle */
            private JMenuItem delete = new JMenuItem("Delete");
             
            /** Menu header */
            private JMenu aboutMenu = new JMenu("About"); 
            
             /** Menu Item to draw Rectangle */
            private JMenuItem aboutItem = new JMenuItem("About/Help");
          
            /** Instance of Base Class */
            private Rectangle rectangle ;
            
             /** Instance of Base Class */
            private Ellipse ellipse ;
            
    
            /** Instance of Base Class */
            private GeometShape baseShape ;
            
            /** Instance of Component Class */
            private drawShapes myShape ;
            
             /** Instance of Component Class */
            private ShapeArray list ;
     
            
            
       /**
        * Constructor for the Frame Class
        * 
        */     
        public drawingFrame() {
             
            list =  new ShapeArray() ; 
            myShape =  new drawShapes( list ) ;
          
            setTitle("Ma Freaking Board");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            
          
      
            drawEllipse.addActionListener(this) ;
            drawRect.addActionListener(this) ;
            drawTriangle.addActionListener(this) ;
            edit.addActionListener(this) ;
            delete.addActionListener(this) ;
            aboutItem.addActionListener(this) ;
            
            
            interfacePanel = new JPanel( ) ;
            interfacePanel.setLayout( new BoxLayout(interfacePanel, BoxLayout.PAGE_AXIS));
            interfacePanel.setBorder(BorderFactory.createLineBorder (Color.black, 2));
            interfacePanel.setBackground(Color.white);
            interfacePanel.setPreferredSize( new Dimension( 150, 0)) ;
            interfacePanel.add(ell);
            interfacePanel.add(rec);
            interfacePanel.add(tri);
            
            drawShapes.add(drawEllipse) ;
            drawShapes.add(drawRect) ;
            drawShapes.add(drawTriangle) ;
            editShape.add(edit) ;
            editShape.add(delete) ;
            aboutMenu.add(aboutItem) ;
            
        
            myMenu.add(drawShapes) ;
            myMenu.add(editShape) ;
            myMenu.add(aboutMenu) ;
            this.setJMenuBar(myMenu) ;
     
            Container myContent = getContentPane() ;
            myContent.add(interfacePanel, BorderLayout.WEST);
            myContent.add(myShape) ;
            pack();
            
            this.setSize(600, 600);                       
            setResizable(false);                                                                                                                                    
            setVisible(true);
              
            ell.addMouseListener(new MouseAdapter()  
                  {  
                    public void mousePressed(MouseEvent e)  
                     {  
                      setTitle("This action is suppose to select the particular ellipse that created this label, so i can delete or edit this particular ellipse") ;
                     }  
                 });    
          }//End of public drawingFrame
        
        
       
        public void  actionPerformed(ActionEvent e)  {
                
            if(e.getActionCommand().equalsIgnoreCase("Ellipse"))
                  {
    
    		//This is where I want to create a new JLabel. Meaning each created ellipse will create a new JLabel, 
    		//BUt if i create the label here and add it to the interfacepanel, it is not displayed
    		//And each label needs a mouseListener.
                    
    	
    		showDialog() ;
                    list.myList.add(new Ellipse( x, y, w, h, bdcol, outcol, sel)) ;
                
                    String s = String.valueOf( numEl + " : Ellipse: ") ;  
                    ell.setText(s) ; 
                    interfacePanel.add(ell) ; 
                    repaint() ;
                    numEl++ ;
    
                  }
              
            if(e.getActionCommand().equalsIgnoreCase("Rectangle"))
                { 
                  showDialog() ;
                 list.myList.add(new Rectangle( x, y, w, h, bdcol, outcol, sel)) ;
                
                 String s = String.valueOf( numEl + " : Rectangle ") ;  
                 rec.setText(s) ;
                 interfacePanel.add(rec);
                 repaint() ;
                 numEl++ ;
                } 
              
        
            }  //End of Action Performed for Shapes
               
       
       public static void main(String[] args) 
         { 
           drawingFrame myFrame = new drawingFrame() ;
         } 
          
          
        public void showDialog() {
              
            class customDialg extends JDialog  
             {       
               JPanel textF = new JPanel(new GridLayout(0,1)) ;
               JPanel labelF = new JPanel(new GridLayout(0,1)) ;
               
               JLabel XL = new JLabel("Enter Xpos") ;
               JLabel YL = new JLabel("Enter Ypos") ;
               JLabel WL = new JLabel("Enter Width") ;
               JLabel HL = new JLabel("Enter Height") ;
               JLabel BDL = new JLabel("Ouline_Color") ;
               JLabel FLL = new JLabel("Fill_Color") ;
               JLabel SELL = new JLabel("Fill_Color") ;
             
               JTextField XT = new JTextField("100", 6);  
               JTextField YT = new JTextField("200", 6);  
               JTextField WT = new JTextField("300", 6);  
               JTextField HT = new JTextField("200", 4);  
               JTextField BDT = new JTextField("black", 6);  
               JTextField FLT = new JTextField("magenta", 6);  
               JTextField SELT = new JTextField("true", 6);  
               
               public customDialg ()  
               {  
                 setModal(true);  
                 setLocation(400,300);  
                 Container cont = getContentPane() ;
     
                 labelF.add(XL) ;
                 labelF.add(YL) ;
                 labelF.add(WL) ;
                 labelF.add(HL) ;
                 labelF.add(BDL) ;
                 labelF.add(FLL) ;
                 labelF.add(SELL) ;
                 
                 textF.add(XT) ;
                 textF.add(YT) ;
                 textF.add(WT) ;
                 textF.add(HT) ;
                 textF.add(BDT) ;
                 textF.add(FLT) ;
                 textF.add(SELT) ;
                 
                 cont.add(labelF, BorderLayout.CENTER) ;
                 cont.add(textF, BorderLayout.LINE_END) ;
                 
                 JButton mybutt = new JButton("Enter Specs");  
                 mybutt.addActionListener(new ActionListener(){  
                 public void actionPerformed(ActionEvent e){  
                 
                     x = Integer.parseInt(XT.getText() ) ;
                     y = Integer.parseInt(YT.getText() ) ;
                     w = Integer.parseInt(WT.getText() ) ;
                     h = Integer.parseInt(HT.getText() ) ;
                     
                     if(SELT.getText().equals("true")) {  sel = true ; }
                     else if(SELT.getText().equals("false")) {  sel = false ; }
                     
                     
                     
                     if(BDT.getText().equals("white")) { bdcol = Color.white; } 
                     else if(BDT.getText().equals("black")) { bdcol = Color.black; } 
                     else if(BDT.getText().equals("red")) { bdcol = Color.red; } 
                     else if(BDT.getText().equals("green")) { bdcol = Color.green; } 
                     else if(BDT.getText().equals("blue")) { bdcol = Color.blue; } 
                     else if(BDT.getText().equals("yellow")) { bdcol = Color.yellow; } 
                     else if(BDT.getText().equals("cyan")) { bdcol = Color.cyan; } 
                     else if(BDT.getText().equals("magenta")) { bdcol = Color.magenta; } 
                     
                     if(FLT.getText().equals("white")) { outcol = Color.white; } 
                     else if(FLT.getText().equals("black")) { outcol = Color.black; } 
                     else if(FLT.getText().equals("red")) { outcol = Color.red; } 
                     else if(FLT.getText().equals("green")) { outcol = Color.green; } 
                     else if(FLT.getText().equals("blue")) { outcol = Color.blue; } 
                     else if(FLT.getText().equals("yellow")) { outcol = Color.yellow; } 
                     else if(FLT.getText().equals("cyan")) { outcol = Color.cyan; } 
                     else if(FLT.getText().equals("magenta")) { outcol = Color.magenta; } 
                     
                     
                 dispose();}});  
                      
                 cont.add(mybutt, BorderLayout.SOUTH) ; 
                 pack();  
                 setVisible(true);  
               } 
             }
                new customDialg (); 
         }
         
       
         
        public void showAbout() {
    
          class aboutFrame extends JFrame{
    
           public AboutFrame() {
            
            JFrame frame = new JFrame("Frame with components");
            JTextPane  textArea = new JTextPane();
            textArea.setEditable(false) ;
            textArea.setBackground(Color.white) ;
            textArea.setText("This is a Java Swing tutorial. The Java Swing tutorial" + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nis for beginners and intermediate Swing developers. After reading this tutorial," + 
            "\nyou will be able to develop non-trivial Java Swing applications." ) ;
            
            frame.add(textArea, BorderLayout.CENTER);
            frame.setSize(450, 450);
            frame.setLayout(new FlowLayout());
            frame.setVisible(true);
          }
         }
           new AboutFrame() ;
       }
    
            
     }    //End of Class drawingFrame

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    I don't have time to read your code, but here's a suggestion. Create a class that encapsulates everything to do with a single object the user draws, including the label. If possible, have the object handle its own events, possibly by calling a public controller method (google MVC). That way, you don't have to worry about how many objects the user creates.

  3. #3
    mlfatty is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    Thanks. I realised I would have got much more feedback if the code was shorter. I think I'll use the Java.awt.List to display the textual representation of the drawn shape. So i dont need the JLabel to do that anymore.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    If you are creating a Swing program, then don't use an AWT List object as you'll be mixing Swing with AWT visual components. Instead use a JList. HTH.

Similar Threads

  1. Adding wav sound into a JLabel.
    By hitmen in forum AWT / Swing
    Replies: 2
    Last Post: 02-17-2009, 06:59 AM
  2. Replies: 6
    Last Post: 07-28-2008, 07:04 PM
  3. GUI - JLabel
    By Azndaddy in forum New To Java
    Replies: 8
    Last Post: 05-02-2008, 08:03 AM
  4. help me with JFrame and JLabel
    By michcio in forum New To Java
    Replies: 5
    Last Post: 11-20-2007, 08:44 AM
  5. JLabel
    By Jack in forum AWT / Swing
    Replies: 2
    Last Post: 07-02-2007, 02:55 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
  •