Question

I wrote this on screen keyboard that should display a keyboard. my problem is that i want the JButtons fit their size of the labels and not to set fix size for all of the JButtons. for example the numbers on keyboard: "1,2,3...." should get small jbutton and the keys "backspace" "tab" etc... should be bigger (fit the size of their label)

this is the code I wrote:

package Q2;
import java.awt.*;

import javax.swing.*;

public class MainPanel extends JPanel{

    private JButton[][] button;
    private JPanel[] panel;                                                     //Array of panels for each buttons line
    private JPanel parent;
    private static final String[][] key = {
        {"`","1","2","3","4","5","6","7","8","9","0","-","+","Backspace"},
        {"Tab","Q","W","E","R","T","Y","U","I","O","P","[","]"},
        {"Caps","A","S","D","F","G","H","J","K","L",";","'","\\","Enter"},
        {"Shif","Z","X","C","V","B","N","M",",",".","?","/"},
        {"                  ",",","<","v",">"}
    };

    //Constructor for main Panel
    public MainPanel(){
        super();
        setLayout(new BorderLayout());
        TextField textField= new TextField(20);
        Font font1 = new Font("david", Font.BOLD, 22);
        textField.setFont(font1);
        add(textField,BorderLayout.CENTER);

        //initialize the parent panel and array of 5 panels and the buttons array
        parent = new JPanel();
        parent.setLayout(new GridLayout(0,1));
        panel = new JPanel[5];
        button = new JButton[20][20];

        for (int row = 0; row<key.length; row++){
            panel[row] = new JPanel();
            for (int column = 0; column<key[row].length; column++){
                button[row][column] = new JButton(key[row][column]);
                button[row][column].setPreferredSize(new Dimension(key[row][column].length()+80,30));
                button[row][column].putClientProperty("row", row);
                button[row][column].putClientProperty("column", column);
                button[row][column].putClientProperty("key", key[row][column]);
                //button[row][column].addActionListener(new MyActionListener());
                panel[row].add(button[row][column]);
            }
            parent.add(panel[row]);
        }
        add(parent,BorderLayout.SOUTH);


    }
    /*
    //panel for line 1 of keyboard buttons - numbers
    protected JComponent getPanelLine1(){
        JPanel panel1 = new JPanel();
        for (int i=0; i<10; i++){

        }
    }

    //panel for line 1 of keyboard buttons - Q-P
    protected JComponent getPanelLine2(){

    }

    //panel for line 1 of keyboard buttons - A-L
    protected JComponent getPanelLine3(){

    }![enter image description here][1]

    //panel for line 1 of keyboard buttons - Z-M    
    protected JComponent getPanelLine4(){

    }

    //panel for line 1 of keyboard buttons - space and arrows
    protected JComponent getPanelLine5(){

    }*/
}

image: my keyborad

Was it helpful?

Solution

As discussed here, don't set the preferred size!

// button[row][column].setPreferredSize(new Dimension(key[row][column].length()+80,30));

Instead set the margins

button[row][column].setMargin(new Insets(10, 20, 10, 20));
                       // Insets ( top, left, bottom, right )

And/or set the font size

Font font = button[row][column].getFont();
String family = font.getFamily();
int style = font.getStyle();
button[row][column].setFont(new Font(family, style, 24));

You probably don't want to create a new font every iteration, but it's just showing you how to get the defaults from the button

Doing either of these things will increase the preferred size of the panel accordingly, taking into account the size of the text contained within it

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top