Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Can I condense this code in Java

  • 29-11-2011 12:35am
    #1
    Registered Users, Registered Users 2 Posts: 266 ✭✭


    I'm wondering, Is there an easier way to write these setters...

    mouseClickedLabel.setBorder(new LineBorder(Color.BLACK));
    mouseClickedLabel.setHorizontalAlignment(JLabel.CENTER);
    mouseEnteredLabel.setBorder(new LineBorder(Color.BLACK));
    mouseEnteredLabel.setHorizontalAlignment(JLabel.CENTER);
    mouseExitedLabel.setBorder(new LineBorder(Color.BLACK));
    mouseExitedLabel.setHorizontalAlignment(JLabel.CENTER);
    mousePressedLabel.setBorder(new LineBorder(Color.BLACK));
    mousePressedLabel.setHorizontalAlignment(JLabel.CENTER);
    mouseReleasedLabel.setBorder(new LineBorder(Color.BLACK));
    mouseReleasedLabel.setHorizontalAlignment(JLabel.CENTER);
    mouseDraggedLabel.setBorder(new LineBorder(Color.BLACK));
    mouseDraggedLabel.setHorizontalAlignment(JLabel.CENTER);
    mouseMovedLabel.setBorder(new LineBorder(Color.BLACK));
    mouseMovedLabel.setHorizontalAlignment(JLabel.CENTER);
    mouseWheelMovedLabel.setBorder(new LineBorder(Color.BLACK));
    mouseWheelMovedLabel.setHorizontalAlignment(JLabel.CENTER);

    by that I mean can I condense it somehow?

    Any help much appreciated

    Ger


Comments

  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    This is what arrays are for.

    JLabel labelArray[] = new JLabel[8];

    for (int x = 0; x < labelArray.length; x++) {
    labelArray[x].setBorder(new LineBorder(Color.BLACK));
    labelArray[x].setHorizontalAlignment(JLabel.CENTER);
    }

    Use comments if you are worried about being able to easily identify the labelArray items, or if the labels have text, then use a String array for the label text with the array index of both arrays corresponding.

    String stringLabelArray[] = new String[8];

    stringLabelArray[0] = "Mouse Clicked";
    ...
    stringLabelArray[7] = "Mouse Wheel Moved";


    for (int x = 0; x < labelArray.length; x++) {
    labelArray[x].setText(stringLabelArray[x])
    labelArray[x].setBorder(new LineBorder(Color.BLACK));
    labelArray[x].setHorizontalAlignment(JLabel.CENTER);
    }


  • Registered Users, Registered Users 2 Posts: 266 ✭✭Gerb68


    As I have assigned an action to labels such as mouseClickedLabel, mouseEnteredLabel ... etc in the mouse listener methods.Can I assign a new label to each index in the Array? e.g.

    Jlabel mouseLabelArray[] = new JLabel[8];

    mouseLabelArray[0] = new JLabel(mouseClickedLabel);
    ...
    mouseLabelArray[7] = new JLabel(mouseWheelMoved);

    or

    mouseLabelArray[0] = mouseClickedLabel;
    ...
    mouseLabelArray[7] = mouseWheelMoved;


    or should I change the label names assigned to the listeners...

    if(e.getSource() == mouseClickedLabel)

    {
    mouseClickedLabel.setText("Mouse Clicked")
    }


  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    Could use enums to index the array of labels or use a dictionary maybe. As long as you don't access it out of bounds.


  • Registered Users, Registered Users 2 Posts: 2,081 ✭✭✭GetWithIt


    Looks ok to me.

    You could follow the suggestions above but, in my opinion, that'd be fairly pointless. You would have turned a simple piece of code into an unmaintainable blob of "here be dragons" code.


  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    Anima wrote: »
    Could use enums to index the array of labels or use a dictionary maybe. As long as you don't access it out of bounds.

    enums are a good idea! What ya think? See many dragons? :pac:
    ...
    
    enum MouseEvents {
    	CLICKED("Clicked"),
    	ENTERED("Entered"),
    	EXITED("Exited"),
    	PRESSED("Pressed"),
    	RELEASED("Released"),
    	DRAGGED("Dragged"),
    	MOVED("Moved"),
    	WHEELMOVED("Wheel Moved");
    
    	private final String name;
    
    	public String toString() {
    		return name;
    	}
    
    	MouseEvents( String name ) {
    		this.name = name;
    	}
    }
    
    ...
    
    JLabel<MouseEvents> mE = new JLabel<MouseEvents> (MouseEvents.values());
    
    mE.setBorder(new LineBorder(Color.BLACK));
    mE.setHorizontalAlignment(JLabel.CENTER);
    mE.addMouseListener(new MouseListener() {
    		
    	...
    
    }
    
    ...
    
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,945 ✭✭✭Anima


    Pretty cool.

    I think GetWithIt does have a point though. Each of the labels are unique objects so they deserve to have their own name and treated as such.

    You're just adding bloat by packaging them this way and it ruins the readability of the code.


  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    It does make it harder to distinguish each label alright.

    I still prefer it as it is easier to apply changes to multiple labels, and it's condensed which is what the OP asked for.


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    I don't understand this line:
    JLabel<MouseEvents> mE = new JLabel<MouseEvents> (MouseEvents.values());
    

    There is a JLabel<T>? Is this meant to be a mapping between MouseEvents and JLabels?

    You could do something like this (untested and won't compile as-is). If your event handling you could also do a system such as this one, but you might be better off just manually writing your events (or writing a more flexible event system).

    I commented out the array declaration and initialisation, but if you construct it that way you can still refer to your variables or to the array, depending on your use case.
    
    public class Main : JSometingOrAnother
    {
    	private JLabelMouseHandling mouseHandler = new JLabelMouseHandling();
    	
    	public JLabel createStandardLabel(String text)
    	{
    		JLabel label = new JLabel(text);
    		label.setBorder(new LineBorder(Color.BLACK));
    		label.setHorizontalAlignment(JLabel.CENTER));
    		
    		return label;
    	}
    	
    	public JLabel createStandardLabel(String text, int mouseEvent, String changeText)
    	{
    		JLabel label = createStandardLabel(text);
    		mouseHandler.registerTextChangeEvent(mouseEvent, label, changeText);
    		
    		return label;
    	}
    
    	public void InitLabels()
    	{
    		JLabel mouseClickedLabel = createStandardLabel("Mouse Click", MouseEvent.MOUSE_CLICKED, "Mouse Clicked");
    		JLabel mouseEnteredLabel = createStandardLabel("Mouse Enter", MouseEvent.MOUSE_ENTERED, "Mouse Entered");
    		// ...
    		
    		// If you want an array of labels to make bulk changes:
    		// JLabel[] labels = { mouseClickedLabel, mouseEnteredLabel };
    	}
    }
    
    public class JLabelMouseHandling : MouseListener
    {
    	private Map<int, Map<JLabel, String>> eventChanges = new HashMap<int, Map<JLabel, String>>();
    	
    	public void mouseClicked(MouseEvent e)
    	{
    		handleMouseChange(e);
    	}
    	
    	public void mouseEntered(MouseEvent e)
    	{
    		handleMouseChange(e);
    	}
    	
    	public void mouseExited(MouseEvent e)
    	{
    		handleMouseChange(e);
    	}
    	
    	public void mousePressed(MouseEvent e)
    	{
    		handleMouseChange(e);
    	}
    	
    	public void mouseReleased(MouseEvent e)
    	{
    		handleMouseChange(e);
    	}
    	
    	public void registerTextChangeEvent(JLabel label, int mouseEvent, String changeText)
    	{
    		Map<JLabel, String> handlers = eventChanges.get(mouseEvent);
    		if (handlers == null) 
    		{
    			eventChanges.put(mouseEvent, new HashMap<JLabel, String>());
    			eventChanges.get(mouseEvent).put(label, changeText);
    		} 
    		else 
    		{
    			handlers.put(label, changeText);
    		}
    		
    		label.addMouseListener(this);
    	}
    	
    	public void handleMouseChange(MouseEvent ev)
    	{
    		Map<JLabel, String> handlers = eventChanges.get(ev.getID());
    		if (handlers != null) 
    		{
    			for (Map.Entry<JLabel, String> entry : handlers.entrySet()) 
    			{
    				entry.getKey().setText(entry.getValue());
    			}
    		}
    	}
    }
    


  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    I don't understand this line:
    JLabel<MouseEvents> mE = new JLabel<MouseEvents> (MouseEvents.values());
    

    There is a JLabel<T>? Is this meant to be a mapping between MouseEvents and JLabels?

    Bad naming convention! :pac: MouseEvents is the name of the enum and it's values are the String sent to the JLabel(String text) constructor.


  • Registered Users, Registered Users 2 Posts: 1,311 ✭✭✭Procasinator


    MouseEvents is the name of the enum and it's values are the String sent to the JLabel(String text) constructor.

    But doesn't MouseEvents.values() return MouseEvents[] (i.e. a Array with all Enum entries) rather than a String?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    But doesn't MouseEvents.values() return MouseEvents[] (i.e. a Array with all Enum entries) rather than a String?

    Oh yes! Scrap that solution! :pac:


Advertisement