• 11
• 9
• 10
• 9
• 11

# [java] JApplet and setting coordinates of components

This topic is 4062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I have a JApplet with a JPanel inside it. On the JPanel there are 14 buttons, but I need to set the coordinates of these buttons and was wondering if there is an easy way. I've looked at setLocation, but that didn't seem to work when i tried it out. Any ideas? Thanks, SD

##### Share on other sites
If you really want the easy way, use eclipse and use the Visual Editor (VE) plugin. This will let you click and drag the buttons where you want them, and it also has nice tools for lining up components.

As for set location, the default layout for a JPanel is a FlowLayout. This layout will ignore the location of the object, and auto-position them for you. What you want to do is remove the Layout by passing in null. This should let the setLocation method work, but I still think the VE editor from eclipse would be the way to go.

JPanel p = new JPanel();p.setLayout( null );JButton b = new JButton( "test" );b.setLocation( 50, 50 );p.add( b );

P.S. I just typed in the code from memory, so don't trust the syntax to be 100%. If this does not work, let us know.

##### Share on other sites
The important part you may have missed is setting the layout to null.

##### Share on other sites
Plus, you have a few 'third party' alternatives for layout, if that's really a concern for you and you don't want to depend upon (usually messy) generated code. Just type a few keywords on google to find out.

##### Share on other sites
Quote:
 Original post by Glass_KnifeIf you really want the easy way, use eclipse and use the Visual Editor (VE) plugin. This will let you click and drag the buttons where you want them, and it also has nice tools for lining up components.As for set location, the default layout for a JPanel is a FlowLayout. This layout will ignore the location of the object, and auto-position them for you. What you want to do is remove the Layout by passing in null. This should let the setLocation method work, but I still think the VE editor from eclipse would be the way to go.*** Source Snippet Removed ***P.S. I just typed in the code from memory, so don't trust the syntax to be 100%. If this does not work, let us know.

Well I tried the code but it doesn't display anything when I have the this.setLayout(null); code in the constructor of the JPanel.

Well here is the constructor, sorry for the late reply, been busy with other things

public class KalahGUI extends JPanel implements ActionListener, MouseListener {        private Game game;        // Graphical representations of the Kalahs    private JButton kalah[];        // Graphical representations of the pits    private JButton pits[][];        /** Creates a new instance of KalahGUI */    public KalahGUI(Game game) {        KalahGameBoard startingBoard;        this.setLayout(null);                this.game = game;        startingBoard = (KalahGameBoard)game.getState().getBoard();                        kalah = new JButton[2];        kalah[0] = new JButton("" + startingBoard.getKalah(0));        kalah[1] = new JButton("" + startingBoard.getKalah(1));                kalah[0].setActionCommand("Kalah0");        kalah[1].setActionCommand("Kalah1");                kalah[0].addActionListener(this);        kalah[1].addActionListener(this);              kalah[0].setLocation(100,100);        kalah[1].setLocation(1, 1);        this.add(kalah[0]);        this.add(kalah[1]);                                //kalah[0].setBounds(0, 200, 50, 50);        //kalah[1].setLocation(200, 100);                pits = new JButton[2][6];        for(int i = 0; i < 2; i++)            for(int j = 0; j < 6; j++)            {                pits[j] = new JButton("" + startingBoard.getStones(i, j));                pits[j].setActionCommand("Pit" + ((6*i) + j));                pits[j].addActionListener(this);                this.add(pits[j]);            }    }

##### Share on other sites
Well, it looks like if you do not set the size of the button, it will not be visible. Try this:

kalah[0].setLocation(100,100);kalah[0].setSize(kalah[0].getPreferredSize());kalah[1].setLocation(1, 1);kalah[1].setSize(kalah[1].getPreferredSize());

That will not only make the button visible, but auto-size it based on
the button text.

##### Share on other sites
Quote:
 Original post by Glass_KnifeWell, it looks like if you do not set the size of the button, it will not be visible. Try this:*** Source Snippet Removed ***That will not only make the button visible, but auto-size it based onthe button text.

Awesome, worked, thanks alot!