Results 1 to 4 of 4
  1. #1
    mike_ledis is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default java problem with list

    Hello i have this code.This applet creates 2 list graphick items and when the selected index of one list changes it draws the background with a color or it paints a shape.I have a problem when i try to add a big string to one of my lists it hides the other list.Any suggestion?
    Java Code:
    import java.applet.Applet;
    import java.awt.BorderLayout;
    import java.awt.Choice;
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    
    import javax.swing.JPanel;
    
    
    public class Α2P4 extends Applet {
    	Choice c1,c2;
    	JPanel p1;
    	public void init()
    	  {
    		this.setLayout(new BorderLayout());
    	c1=new Choice();
    	c1.addItemListener(new ItemListener() {
    		  
    	      public void itemStateChanged(ItemEvent e)
    	      {
    	    	  int cm=c1.getSelectedIndex();
    	    		if (cm ==0)
    	    		setBackground(Color.red);
    
    	    		if (cm==1 )
    	    		setBackground(Color.blue);
    
    	    		if (cm ==2)
    	    		setBackground(Color.yellow);
    
    	    		if (cm ==3)
    	    		setBackground(Color.GREEN);
    
    	    		if (cm ==4)
    	    		setBackground(Color.black);
    	    		if (cm ==5)
    		    		setBackground(Color.gray);
    	    		if (cm ==6)
    		    		setBackground(Color.orange);
    
    	    		
    	      }
    	  });
        c1.add("Κόκκινο");
        c1.add("Μπλε");
        c1.add("Κιτρινο");
        c1.add("Πράσσινο");
        c1.add("Μαύρο");
        c1.add("Γκρί");
        c1.add("Πορτοκαλί");
        c2=new Choice();
        c2.addItemListener(new ItemListener() {
    		  
    	      public void itemStateChanged(ItemEvent e)
    	      {
    	          repaint();
    
    	    		
    	      }
    	  });
        c2.add("Line");
        c2.add("Rectangle");
        c2.add("Oval");
        c2.add("FillRect");
        c2.add("FilledOval");
        p1=new JPanel();
        p1.setLayout(new FlowLayout());
        p1.setSize(200,200);
        p1.add(c2);
        p1.add(c1);
        this.add(p1,BorderLayout.PAGE_START);
        
    	  }
    	 public void paint(Graphics g){
    		 super.paint(g);
    		 g.setColor(Color.cyan);
    		 int c3=c2.getSelectedIndex();
    		 if(c3==0)
    		 {
    			g.drawLine(0, 50, 50, 50); 
    		 }
             if(c3==1)
             {
    			 g.drawRect(this.getWidth()/2, this.getHeight()/2, this.getWidth()/3, this.getHeight()/3);
    		 }
             if(c3==2)
             {
    			 g.drawOval(this.getWidth()/2, this.getHeight()/2, this.getWidth()/3,this.getHeight()/3);
    		 }
             if(c3==3)
             {
    			 g.fillRect(this.getWidth()/2, this.getHeight()/2, this.getWidth()/3, this.getHeight()/3);
    		 }
             if(c3==4)
             {
    			 g.fillOval(this.getWidth()/2, this.getHeight()/2, this.getWidth()/3, this.getHeight()/3);
    		 }
    		
    		 
    	 }
    	
    }

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by mike_ledis View Post
    .I have a problem when i try to add a big string to one of my lists it hides the other list.Any suggestion?

    what do you mean with "big string"? your list are added in a jpanel with a flowlayout, so when the first list has a big string the width is also enlarged and the second list should be wrapped to an other line. to fix this problem, you should use a layout with a fix size ex. Gridlayout.
    Last edited by j2me64; 03-13-2011 at 11:33 AM.

  3. #3
    mike_ledis is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    If you try to replace the string item "FillRect" with "FilledRectangle" you will see that a list does not show up!Give it a chance and run the program if you have some time!

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    ok for demostration purposes i inserted a button in the applet called replace and i used the following code as actionlistener

    Java Code:
    	replace.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			c2.remove(3);
    			c2.insert("FilledRectangle", 3);
    		}
    	});

    the code is clear, the element with the index 3 is removed and then the new one is inserted. when you push the button and reopen the list, quess what you will see?

Similar Threads

  1. List problem
    By Bulelakes in forum JDBC
    Replies: 7
    Last Post: 07-30-2010, 08:51 AM
  2. assignment problem with List<T>
    By Newbie666 in forum New To Java
    Replies: 11
    Last Post: 01-21-2010, 01:12 PM
  3. how to create list of list in java ???
    By ilayaraja in forum Advanced Java
    Replies: 1
    Last Post: 10-26-2009, 05:30 PM
  4. Array List Problem
    By khamuruddeen in forum New To Java
    Replies: 1
    Last Post: 12-22-2007, 09:10 AM
  5. list problem
    By fareez in forum New To Java
    Replies: 3
    Last Post: 06-28-2007, 11:26 AM

Posting Permissions

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