This discussion is archived
5 Replies Latest reply: Oct 1, 2012 5:30 AM by Kayaman RSS

BorderLayout  . . . No Exception Thrown When "Centre" Is Used For "Center"

KonTiki Newbie
Currently Being Moderated
Hi Folks.

Started teaching myself Java 3 months back.
Only sticker was this one that held me up for a couple of days.

When we assign a set of JButtons to a pre-defined JPanel according to a BorderLayout, we assign each JButton according to its desired position in the layout, with each of the 5 pre-defined positions being nominated as "North", "South", "East", "West" and "Center".

In my country we spell "center" as "centre".
So, not surprisingly, I automatically coded it as follows :
import java.awt.*;
import javax.swing.*;




public class ColourArray extends JFrame

{
     // Attributes :
     // ----------
     
     JPanel[] panelArray = new JPanel[9];
//     String[] panelColours = { "green", "white", "orange", "white", "yellow", "white", "red", "white", "blue" };
     JPanel centrePanel = new JPanel();
     JButton aButton = new JButton("A");
     JButton bButton = new JButton("B");
     JButton cButton = new JButton("C");
     JButton dButton = new JButton("D");
     JButton eButton = new JButton("E");
     


     // Methods :
     // -----------


     // Constructor :
     // -----------

     public ColourArray()

     {
          setLocation(400, 300);
          setSize(500, 500);
          setTitle("Question1");
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLayout(new GridLayout(3,3));
          setVisible(true);
          
          
          // Add attribute elements to the grid :
          
          for ( int i = 0; i < panelArray.length; i++ )

          {
                    panelArray[i] = new JPanel();
                    add(panelArray);
                                   
          }
          

          panelArray[0].setBackground(Color.green);
          panelArray[1].setBackground(Color.white);
          panelArray[2].setBackground(Color.orange);
          panelArray[3].setBackground(Color.white);
          panelArray[4].setBackground(Color.white);
          panelArray[5].setBackground(Color.white);
          panelArray[6].setBackground(Color.red);
          panelArray[7].setBackground(Color.white);
          panelArray[8].setBackground(Color.blue);
          
          panelArray[4].setLayout(new BorderLayout() ); // To set button layout here.


          panelArray[4].add("North", aButton);     
          panelArray[4].add("West", bButton);
          panelArray[4].add("Centre", cButton);
          panelArray[4].add("South", eButton);
          panelArray[4].add("East", dButton);
          


     }
     
}
And not surprisingly, it did not work as I'd planned. The centre button was not shown and any other JButtons that were assigned after the wrongly assigned JButton weren't shown either.

Eventually I found my error by some 'experimental coding'.

Yet I'm very surprised that it never showed an error nor threw a runtime exception.

Why is this ?
If I use "Bozo" rather than "Center", I get the same sort of behaviour. . . .

The great thing for me with Java is that it really shows me what's wrong syntactically and throws exceptions well at runtime. Now my faith is shaken.

Anyone got ideas on this ?

Edited by: KonTiki on 30-Sep-2012 05:24

Edited by: KonTiki on 30-Sep-2012 05:27

Edited by: KonTiki on 30-Sep-2012 05:30

Edited by: KonTiki on 30-Sep-2012 05:31

Edited by: KonTiki on 30-Sep-2012 05:34

Edited by: KonTiki on 30-Sep-2012 10:37

Edited by: KonTiki on 25-Mar-2013 14:32                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
  • 1. Re: BorderLayout  . . . No Exception Thrown When "Centre" Is Used For "Center"
    rp0428 Guru
    Currently Being Moderated
    Welcome to the forum!
    >
    When we assign a set of JButtons to a pre-defined JPanel according to a BorderLayout, we assign each JButton according to its desired position in the layout, with each of the 5 pre-defined positions being nominated as "North", "South", "East", "West" and "Center".

    In my country we spell "center" as "centre".
    . . .
    And not surprisingly, it did not work as I'd planned. The centre button was not shown and any other JButtons that were assigned after the wrongly assigned JButton weren't shown either.

    Eventually I found my error by some 'experimental coding'.

    Yet I'm very surprised that it never showed an error nor threw a runtime exception.

    Why is this ?
    If I use "Bozo" rather than "Center", I get the same sort of behaviour. . . .

    The great thing for me with Java is that it really shows me what's wrong syntactically and throws exceptions well at runtime. Now my faith is shaken.
    >
    Well then let me restore your faith.

    When you code like this
    panelArray[4].add("North", aButton); 
    panelArray[4].add("West", bButton);
    panelArray[4].add("Centre", cButton);
    panelArray[4].add("South", eButton);
    panelArray[4].add("East", dButton);
    You are calling the 'add' method and passing a String and an object. If you read the container API
    http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Container.html#add(java.lang.String, java.awt.Component)

    That method simply adds the component to the container. It does not take constraints into account.
    >
    add
    public Component add(String name,
    Component comp)Adds the specified component to this container. This is a convenience method for addImpl(java.awt.Component, java.lang.Object, int).
    This method is obsolete as of 1.1. Please use the method add(Component, Object) instead.
    >
    If you want to take constraints into account you would call the version of 'add' and pass the component and the constraints you want used
    >
    add
    public void add(Component comp,
    Object constraints)Adds the specified component to the end of this container. Also notifies the layout manager to add the component to this container's layout using the specified constraints object. This is a convenience method for addImpl(java.awt.Component, java.lang.Object, int).
    Note: If a component has been added to a container that has been displayed, validate must be called on that container to display the new component. If multiple components are being added, you can improve efficiency by calling validate only once, after all the components have been added.

    Parameters:
    comp - the component to be added
    constraints - an object expressing layout contraints for this component
    >
    For example see the BorderLayout API doc and the example at the start of this link
    http://docs.oracle.com/javase/7/docs/api/java/awt/BorderLayout.html
    >
    A border layout lays out a container, arranging and resizing its components to fit in five regions: north, south, east, west, and center. Each region may contain no more than one component, and is identified by a corresponding constant: NORTH, SOUTH, EAST, WEST, and CENTER. When adding a component to a container with a border layout, use one of these five constants, for example:

    Panel p = new Panel();
    p.setLayout(new BorderLayout());
    p.add(new Button("Okay"), BorderLayout.SOUTH);

    As a convenience, BorderLayout interprets the absence of a string specification the same as the constant CENTER:

    Panel p2 = new Panel();
    p2.setLayout(new BorderLayout());
    p2.add(new TextArea()); // Same as p.add(new TextArea(), BorderLayout.CENTER);

    >
    See the difference? You are passing a string as the first parameter and the component as the second parameter. The example above is passing the component as the first parameter and the desired layout constraint as the second parameter.

    Your spelling of 'Centre' rather than 'Center' is irrevelant since any string you pass is just a string.

    Try the example above from the BorderLayout API and see what happens if you use BorderLayout.CENTRE instead of BorderLayout.CENTER.

    As part of teaching yourself Java why not learn from the experts and work your way through the Java Tutorials?

    Start with the Basic trails
    http://docs.oracle.com/javase/tutorial/
  • 2. Re: BorderLayout  . . . No Exception Thrown When "Centre" Is Used For "Center"
    KonTiki Newbie
    Currently Being Moderated
    Thank you for your response and explanations.

    OK.
    I've tried the second add method you showed and it does error-mark all constraint strings bar those designated ones, "North", "South", "East", "West" and "Center". Thus "Centre" and "Bozo" are stopped straight off prior to running.

    That is the answer as far as the immediate coding problem is concerned.

    But from the point of view of good software engineering (I'm learning Java after learning Ada), it is unusual to see a language leaving booby trap methods like that just for the sake of having an alternative 'quick' way for component assignment.

    Furthermore, after specifying that one was applying a border layout to the panel (i.e.p) via :

    p.setLayout(new BorderLayout());

    It should not really be necessary to again explicitly state it in the constraint parameter with :

    +p.add(new Button("Okay"), BorderLayout.SOUTH);+

    Why not just have an add method with a set of allowed strings for the constraints ? (i.e. "North", "South", . . . "Center")
    Any misspelling or non-permitted strings would then be trapped at coding stage.

    I hope that this 'looseness' of syntaxt is not endemic in Java - a lot of apps are riding on it.

    Edited by: KonTiki on 01-Oct-2012 03:36

    Edited by: KonTiki on 01-Oct-2012 03:40
  • 3. Re: BorderLayout  . . . No Exception Thrown When "Centre" Is Used For "Center"
    Kayaman Guru
    Currently Being Moderated
    KonTiki wrote:
    But from the point of view of good software engineering (I'm learning Java after learning Ada), it is unusual to see a language leaving booby trap methods like that just for the sake of having an alternative 'quick' way for component assignment.
    No it's not unusual. Every language has its quirks. Before Java had enums, constants were used for defining such things. That had the side effect of allowing illegal values to be passed in (of course the values could've been checked, but they weren't).
    Furthermore, after specifying that one was applying a border layout to the panel (i.e.p) via :

    p.setLayout(new BorderLayout());

    It should not really be necessary to again explicitly state it in the constraint parameter with :

    +p.add(new Button("Okay"), BorderLayout.SOUTH);+
    You could of course statically import SOUTH.

    But I think in this case the question is that you're simply rebelling against Java. Sure there are plenty of things that could have been done differently. Java could have had closures. Generics were half-poorly implemented as an afterthought.

    But that's what you're stuck with now, so if you can't deal with it, I suggest you forget about Java right now. Being angry because a programming language is what it is (and for example not like another programming language you're used to) is completely useless.
    Why not just have an add method with a set of allowed strings for the constraints ? (i.e. "North", "South", . . . "Center")
    Any misspelling or non-permitted strings would then be trapped at coding stage.
    That's not how Java does it. The proper way would be with enums. But Swing is an old API and they're not going to break backwards compatibility.
    I hope that this 'looseness' of syntaxt is not endemic in Java - a lot of apps are riding on it.
    There's plenty of "looseness". I've never coded Ada by the way. Was it perfect?
  • 4. Re: BorderLayout  . . . No Exception Thrown When "Centre" Is Used For "Center"
    KonTiki Newbie
    Currently Being Moderated
    Well, a typically hot American defense there !

    No, Ada - not even SPARK Ada with formal methods code design - could be said to be be 'perfect'.

    In fact Ada and Java have been borrowing good ideas from one another for the last 2-3 versions.

    Yes, backwards compatibility is a good plausible reason for this non-correction.
    As issues go, it's not so serious.
    And no plane or reactor is relying on Java code to stay non-critical.

    My own interest in Java is for app development.
    I wonder if it be possible to modfy this sort of issue in the Java Mobile version and leave the SE and EE alone ?
    In mobile apps, a bug - and the users' reactions to it as posted on Google Play - have a big bearing on continuing sales.
  • 5. Re: BorderLayout  . . . No Exception Thrown When "Centre" Is Used For "Center"
    Kayaman Guru
    Currently Being Moderated
    KonTiki wrote:
    Well, a typically hot American defense there !
    More like "typically cool Finnish defense".
    No, Ada - not even SPARK Ada with formal methods code design - could be said to be be 'perfect'.
    Indeed.
    In fact Ada and Java have been borrowing good ideas from one another for the last 2-3 versions.

    Yes, backwards compatibility is a good plausible reason for this non-correction.
    As issues go, it's not so serious.
    And no plane or reactor is relying on Java code to stay non-critical.
    Hopefully. I was just telling you to be ready for further disappointments. I've been involved with Java for some 15 years(?) now, since the times of 1.2 and there's plenty of clumsiness in Java.
    Modern languages may strive for perfection, but they won't have Java's user base in years. So far Ruby seems like one of the few viable challengers.
    My own interest in Java is for app development.
    I wonder if it be possible to modfy this sort of issue in the Java Mobile version and leave the SE and EE alone ?
    In mobile apps, a bug - and the users' reactions to it as posted on Google Play - have a big bearing on continuing sales.
    Java Mobile version, i.e. Java ME is an (in my opinion) outdated technology. I suppose you mean Android's SDK, which is similar to Java, but not exactly the same. The language uses the same syntax, but the development style is very different.
    Of course this kind of bug isn't much of an issue with mobile development. The real problem is testing with all sorts of different handsets/tablets and versions.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points