Layout Problem(box layout)

Some elements get out of the JPanel

Page 1 of 1

9 Replies - 1425 Views - Last Post: 23 July 2010 - 12:24 PM Rate Topic: -----

#1 deemeetar  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 66
  • Joined: 22-January 10

Layout Problem(box layout)

Posted 22 July 2010 - 02:12 PM

These JButtons get out of their JPanel
Posted Image

// Layout - setup
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.LINE_AXIS));
		
		left = new JPanel();
		left.setLayout(new BoxLayout(left, BoxLayout.PAGE_AXIS));
		left.setPreferredSize(new Dimension(220, 600));
		left.setMaximumSize(new Dimension(220, 600));
		left.setMinimumSize(new Dimension(220, 600));

		
		left.add(Box.createRigidArea(new Dimension(0,5)));
		left.add(startBtn);
		left.add(Box.createRigidArea(new Dimension(0,5)));
		left.add(pauseBtn);
		left.add(Box.createRigidArea(new Dimension(0,5)));
		left.add(insertLessionBtn);
		left.add(Box.createRigidArea(new Dimension(0,5)));
		left.add(listScroll);
		left.add(Box.createRigidArea(new Dimension(0,5)));
		
		rightHead = new JPanel();
		rightHead.setLayout(new BoxLayout(rightHead, BoxLayout.LINE_AXIS));
		
		rightHead.add(Box.createRigidArea(new Dimension(5,0)));
		rightHead.add(title);
		rightHead.add(Box.createRigidArea(new Dimension(5,0)));
		rightHead.add(duration);
		rightHead.add(Box.createRigidArea(new Dimension(5,0)));
		rightHead.add(WPM);
		rightHead.add(Box.createRigidArea(new Dimension(5,0)));
		rightHead.add(errors);
		rightHead.add(Box.createRigidArea(new Dimension(5,0)));
		
		rightKeyboard = new JPanel();
		
		rightInputContainer = new JPanel();
		rightInputContainer.add(inputScroll);
		rightInputContainer.setPreferredSize(new Dimension(580, 350));
		rightInputContainer.setMaximumSize(new Dimension(580, 350));
		rightInputContainer.setMinimumSize(new Dimension(580, 350));
		
		right = new JPanel();
		right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
		right.setPreferredSize(new Dimension(595, 600));
		right.setMaximumSize(new Dimension(595, 600));
		right.setMinimumSize(new Dimension(595, 600));
		
		right.add(Box.createRigidArea(new Dimension(0,5)));
		right.add(rightHead);
		right.add(Box.createRigidArea(new Dimension(0,10)));
		right.add(progress);
		right.add(Box.createRigidArea(new Dimension(0,10)));
		right.add(rightInputContainer);
		right.add(Box.createRigidArea(new Dimension(0,5)));
		right.add(rightKeyboard);
		right.add(Box.createRigidArea(new Dimension(0,5)));
		
		mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
		mainPanel.add(left);
		mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
		mainPanel.add(right);
		mainPanel.add(Box.createRigidArea(new Dimension(5,0)));
		
		this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(mainPanel);


and i have set the button size:

        startBtn.setPreferredSize(new Dimension(220, 30));
		startBtn.setMaximumSize(new Dimension(220, 30));
		startBtn.setMinimumSize(new Dimension(220, 30));


also the other elements on right have their sizes.

Is This A Good Question/Topic? 0
  • +

Replies To: Layout Problem(box layout)

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,202
  • Joined: 06-March 08

Re: Layout Problem(box layout)

Posted 22 July 2010 - 06:18 PM

You are paid by the number of objects created or what ?

Create your Dimension only once

Dimension dim5 = new Dimension(0,5);
Dimension dim10 = new Dimension(0,10);


and then use them as you need them

         left.add(Box.createRigidArea(dim5));  
         left.add(startBtn);  
         left.add(Box.createRigidArea(dim5));  
         left.add(pauseBtn);  
         left.add(Box.createRigidArea(dim5));  
         left.add(insertLessionBtn);  
         left.add(Box.createRigidArea(dim5));  
         left.add(listScroll);  
         left.add(Box.createRigidArea(dim5));  


Was This Post Helpful? 0
  • +
  • -

#3 deemeetar  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 66
  • Joined: 22-January 10

Re: Layout Problem(box layout)

Posted 22 July 2010 - 07:25 PM

View Postpbl, on 22 July 2010 - 05:18 PM, said:

You are paid by the number of objects created or what ?

Create your Dimension only once

Dimension dim5 = new Dimension(0,5);
Dimension dim10 = new Dimension(0,10);


and then use them as you need them

         left.add(Box.createRigidArea(dim5));  
         left.add(startBtn);  
         left.add(Box.createRigidArea(dim5));  
         left.add(pauseBtn);  
         left.add(Box.createRigidArea(dim5));  
         left.add(insertLessionBtn);  
         left.add(Box.createRigidArea(dim5));  
         left.add(listScroll);  
         left.add(Box.createRigidArea(dim5));  



lol you made my day.

Anyhow, how to solve my problem?
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Layout Problem(box layout)

Posted 22 July 2010 - 07:41 PM

you have to tell use your problem or you get no solution, lol. From the pic, I assume it's those three buttons sticking out of place?

I would try creating a vertical box and adding all of your components in JPanel left to that. Or maybe turn from BoxLayout.PAGE_AXIS to BoxLayout.Y_AXIS.

This post has been edited by Luckless: 22 July 2010 - 07:48 PM

Was This Post Helpful? 0
  • +
  • -

#5 deemeetar  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 66
  • Joined: 22-January 10

Re: Layout Problem(box layout)

Posted 23 July 2010 - 03:12 AM

I want to fix the problem with those JButtons(3 of them). I have replaced them with JLabels and it's the same thing. Also it would be helpful if you point me why my JPanel background isn't working?

Here is the whole code:
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent.*;
import java.io.*;

public class InterFace extends JFrame
{
    JPanel mainPanel;
    JPanel left;
    JPanel leftMain;
    JPanel right;
    JPanel rightHead;
    
    JProgressBar progress;
    JPanel rightInputContainer;
    JPanel rightKeyboard;
    JScrollPane inputScroll;
    JScrollPane listScroll;
    JLabel startBtn;
    JLabel pauseBtn;
    JLabel insertLessionBtn;
    JLabel title;
    JLabel duration;
    JLabel WPM;
    JLabel errors;
    JList lessionList;
    JTextPane input;
    Image textBg;
    Font inputFont;
    Font theFont;
    Boolean ended = false;

    public InterFace(){
        super("Typing Teacher");

        // The main INPUT
        input = new JTextPane(){
            {setOpaque(false);}
        };
        input.setEditable(false);
        input.setText("This is just some usual text. What other can i say? I am really interested if the wordwrap works, lets see:D");
        input.addKeyListener(new KeyListener(){

                public void keyPressed(KeyEvent e){
                    if(!ended){
                        if(e.getKeyChar() == input.getSelectedText().charAt(0) && e.getKeyCode() != KeyEvent.VK_ALT && e.getKeyCode() != KeyEvent.VK_SHIFT && e.getKeyCode() != KeyEvent.VK_CONTROL )
                            rightKey(input.getSelectionstart());
                        else
                            wrongKey(input.getSelectionstart());

                        if(!(input.getSelectionend() == input.getText().length()) && e.getKeyCode() != KeyEvent.VK_ALT && e.getKeyCode() != KeyEvent.VK_SHIFT && e.getKeyCode() != KeyEvent.VK_CONTROL){
                            input.setSelectionstart(input.getSelectionend());
                            input.setSelectionend(input.getSelectionstart() + 1);
                        }               
                        else if(e.getKeyCode() != KeyEvent.VK_ALT && e.getKeyCode() != KeyEvent.VK_SHIFT && e.getKeyCode() != KeyEvent.VK_CONTROL)
                            reachedEnd();
                    }

                }

                public void keyReleased(KeyEvent e){

                }

                public void keyTyped(KeyEvent e){

                }
            }
        );
        prepareInput();
        inputFont = loadFont(12, Font.BOLD, this);
        theFont = loadFont(18, Font.BOLD, this);
        input.setFont(theFont);
        inputScroll = new JScrollPane(input){
            {setOpaque(false);}
        };
        inputScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
        inputScroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        inputScroll.setPreferredSize(new Dimension(550, 320));
        inputScroll.setMaximumSize(new Dimension(550, 320));
        inputScroll.setMinimumSize(new Dimension(550, 320));
        inputScroll.setBorder(null);

        // JList

        String[] data = {"one", "two", "three", "four", "one", "two", "three", "four", "one", "two", "three", "four"};
        lessionList = new JList(data);
        lessionList.setFont(inputFont);
        listScroll = new JScrollPane(lessionList);
        listScroll.setPreferredSize(new Dimension(150, 375));
        listScroll.setMaximumSize(new Dimension(150, 375));
        listScroll.setMinimumSize(new Dimension(150, 375));

        // controll buttons

        startBtn = new JLabel("Start", SwingConstants.CENTER){

            public void paintComponent(Graphics g){
                Image buttonBg = new ImageIcon("images/button.png").getImage();
                g.drawImage(buttonBg, 0, 0, 150, 30, this);
                super.paintComponent(g);
            }
        };
        startBtn.setFont(inputFont);
        pauseBtn = new JLabel("Pause", SwingConstants.CENTER){

            public void paintComponent(Graphics g){
                Image buttonBg = new ImageIcon("images/button.png").getImage();
                g.drawImage(buttonBg, 0, 0, 150, 30, this);
                super.paintComponent(g);
            }
        };
        pauseBtn.setFont(inputFont);
        insertLessionBtn = new JLabel("Create lession", SwingConstants.CENTER){

            public void paintComponent(Graphics g){
                Image buttonBg = new ImageIcon("images/button.png").getImage();
                g.drawImage(buttonBg, 0, 0, 150, 30, this);
                super.paintComponent(g);
            }
        };
        insertLessionBtn.setFont(inputFont);
        
        Dimension btnDim = new Dimension(150, 30);

        startBtn.setPreferredSize(btnDim);
        startBtn.setMaximumSize(btnDim);
        startBtn.setMinimumSize(btnDim);
        pauseBtn.setPreferredSize(btnDim);
        pauseBtn.setMaximumSize(btnDim);
        pauseBtn.setMinimumSize(btnDim);
        insertLessionBtn.setPreferredSize(btnDim);
        insertLessionBtn.setMaximumSize(btnDim);
        insertLessionBtn.setMinimumSize(btnDim);

        // Info bar
        Dimension ver5 = new Dimension(0, 5);
        Dimension hor5 = new Dimension(5, 0);
        Dimension hor10 = new Dimension(10, 0);

        title = new JLabel("Lession title goes here");
        title.setFont(inputFont);
        duration = new JLabel("Time: 00:00");
        duration.setFont(inputFont);
        WPM = new JLabel("WPM: 15");
        WPM.setFont(inputFont);
        errors = new JLabel("Mistakes: 0");
        errors.setFont(inputFont);

        progress = new JProgressBar();
        progress.setMaximum(input.getText().length());
        progress.setValue(34);
        progress.setPreferredSize(new Dimension(350, 10));
        progress.setMinimumSize(new Dimension(350, 10));
        progress.setMaximumSize(new Dimension(350, 10));

        // Layout - setup
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.LINE_AXIS));

        left = new JPanel();
        left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
        left.setPreferredSize(new Dimension(220, 600));
        left.setMaximumSize(new Dimension(220, 600));
        left.setMinimumSize(new Dimension(220, 600));

        left.add(Box.createRigidArea(ver5));
        left.add(startBtn);
        left.add(Box.createRigidArea(ver5));
        left.add(pauseBtn);
        left.add(Box.createRigidArea(ver5));
        left.add(insertLessionBtn);
        left.add(Box.createRigidArea(ver5));
        left.add(listScroll);
        left.add(Box.createRigidArea(ver5));
        
        rightHead = new JPanel();
        rightHead.setLayout(new BoxLayout(rightHead, BoxLayout.LINE_AXIS));

        rightHead.add(Box.createRigidArea(hor5));
        rightHead.add(title);
        rightHead.add(Box.createRigidArea(hor10));
        rightHead.add(duration);
        rightHead.add(Box.createRigidArea(hor10));
        rightHead.add(WPM);
        rightHead.add(Box.createRigidArea(hor10));
        rightHead.add(errors);
        rightHead.add(Box.createRigidArea(hor5));

        rightKeyboard = new JPanel();

        rightInputContainer = new JPanel(){
            {setOpaque(false);}

            public void paintComponenet(Graphics g){
                super.paintComponent(g);
                Image contentBg = new ImageIcon("images/textBg.png").getImage();
                g.drawImage(contentBg, 0, 0, 350, 350, this);
            }

        };
        rightInputContainer.setLayout(new BorderLayout());
        rightInputContainer.add(inputScroll, BorderLayout.CENTER);
        rightInputContainer.setPreferredSize(new Dimension(550, 350));
        rightInputContainer.setMaximumSize(new Dimension(550, 350));
        rightInputContainer.setMinimumSize(new Dimension(550, 350));
        rightInputContainer.setBackground(Color.blue);

        right = new JPanel();
        right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
        right.setPreferredSize(new Dimension(560, 600));
        right.setMaximumSize(new Dimension(560, 600));
        right.setMinimumSize(new Dimension(560, 600));

        right.add(Box.createRigidArea(ver5));
        right.add(rightHead);
        right.add(Box.createRigidArea(new Dimension(0,10)));
        right.add(progress);
        right.add(Box.createRigidArea(new Dimension(0,10)));
        right.add(rightInputContainer);
        right.add(Box.createRigidArea(ver5));
        right.add(rightKeyboard);
        right.add(Box.createRigidArea(ver5));

        mainPanel.add(Box.createRigidArea(hor5));
        mainPanel.add(left);
        mainPanel.add(Box.createRigidArea(hor5));
        mainPanel.add(right);
        mainPanel.add(Box.createRigidArea(hor5));

        this.getContentPane().setLayout(new BorderLayout());
        this.getContentPane().add(mainPanel);

    }

    public void setInputStyle(Boolean right, int charPos){
        MutableAttributeSet attributes = input.getInputAttributes();
        StyledDocument doc = input.getStyledDocument();
        Color col = right ? Color.green : Color.red;
        StyleConstants.setForeground(attributes, col);
        doc.setCharacterAttributes(charPos, 1, attributes, false);
    }

    public void prepareInput(){
        input.setSelectionstart(0);
        input.setSelectionend(1);
    }

    public void wrongKey(int charPos){
        setInputStyle(false, charPos);
    }

    public void rightKey(int charPos){
        setInputStyle(true, charPos);
    }

    public void reachedEnd(){
        ended = true;
        input.setText(input.getText() + "\nEnd!");
    }

    public static Font loadFont(float size, int style, JFrame jp) {
        // Opens the JPanel's resource called font/chem1.ttf
        InputStream is = jp.getClass().getResourceAsStream("fonts/chinrg__.ttf");
        Font font = null;
        try {
            font = Font.createFont(Font.TRUETYPE_FONT, is);
            font = font.deriveFont(size);
            font = font.deriveFont(style);
        } catch (FontFormatException e) {
            System.err.println("Font is null");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Font is null");
            System.exit(1);
        }

        // CAREFUL, if font returns null, you can have NullPointerExceptions
        return font;
    }

}


This post has been edited by deemeetar: 23 July 2010 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1508
  • View blog
  • Posts: 3,219
  • Joined: 11-December 07

Re: Layout Problem(box layout)

Posted 23 July 2010 - 03:17 AM

On my phone so I can't really check this out properly but I remember having a similar problem. Pretty sure it was to do with the different widgets having different horizontal alignments.
Was This Post Helpful? 0
  • +
  • -

#7 deemeetar  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 66
  • Joined: 22-January 10

Re: Layout Problem(box layout)

Posted 23 July 2010 - 03:35 AM

Any idea how to fix that? I tried to put only the buttons in new JPanel which will be added in the left, but it is the same result. I also tried to set the alignment to center, but no result.
Was This Post Helpful? 0
  • +
  • -

#8 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,141
  • Joined: 31-August 09

Re: Layout Problem(box layout)

Posted 23 July 2010 - 07:07 AM

Try creating a separate JPanel for the buttons then add that JPanel to another JPanel with BorderLayout.EAST if the other suggestions made aren't working for you.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • Joined: 15-July 08

Re: Layout Problem(box layout)

Posted 23 July 2010 - 11:21 AM

I think this explains you problem and will help you fix it:
http://download.orac...x.html#features
Was This Post Helpful? 1
  • +
  • -

#10 deemeetar  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 66
  • Joined: 22-January 10

Re: Layout Problem(box layout)

Posted 23 July 2010 - 12:24 PM

panel.setAlignmentX(Component.CENTER_ALIGNMENT) solve the problem.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1