Question

This is the code for a simple notepad in swing using miglayout.I have used span and grow to fill the horizontal space but fill and grow are not working as row constraints.Why? I want the textarea to fill all the available space in it's both minimum and maximum size but that's not happening. Below is the code :

import javax.swing.*;
import net.miginfocom.swing.MigLayout;
import java.awt.*;
import java.awt.event.*;
public class Notepad{
JFrame jf;
JMenuBar jmbar;
JMenu jm1,jm2;
JMenuItem jmnew,jmopen,jmsave,jmexit;
JMenuItem jmselect,jmpaste,jmcopy,jmcut;
Notepad(){
    jf=new JFrame("Notepad");
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setLayout(new MigLayout("debug,fill","","[push,grow]"));

    /*Toolkit tk = Toolkit.getDefaultToolkit();  
    int xSize = ((int) tk.getScreenSize().getWidth());  
    int ySize = ((int) tk.getScreenSize().getHeight());  
    jf.setSize(xSize,ySize);  */
    jf.setExtendedState(Frame.MAXIMIZED_BOTH);

    createTextArea();
    createMenu();
    //JTextArea txt=new JTextArea("", 0,0,);
    jf.setMinimumSize(new Dimension(600,400));
    jf.setVisible(true);
}
void createMenu(){
    jmbar=new JMenuBar();
    jm1=new JMenu("File");
    jmnew=new JMenuItem("New");
    jmopen=new JMenuItem("Open");
    jmsave=new JMenuItem("Save");
    jmexit=new JMenuItem("Exit");
    jmexit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae){
            System.exit(0);
        }
    });
    jm1.add(jmnew);
    jm1.add(jmopen);
    jm1.add(jmsave);
    jm1.addSeparator();
    jm1.add(jmexit);
    jmbar.add(jm1);


    jm2=new JMenu("Edit");
    jmcut=new JMenuItem("Cut");
    jmcopy=new JMenuItem("Copy");
    jmpaste=new JMenuItem("Paste");
    jmselect=new JMenuItem("Select All");

    jm2.add(jmcut);
    jm2.add(jmcopy);
    jm2.add(jmpaste);
    jm2.add(jmselect);
    jmbar.add(jm2);
    jf.setJMenuBar(jmbar);

}
void createTextArea(){
    JTextArea textArea = new JTextArea("", 10,40);
    JScrollPane textScroll = new JScrollPane(textArea,    JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    textArea.setFont(new Font("Century Gothic", Font.BOLD, 12));
    jf.add(textScroll,"grow,span,wrap");
}
public static void main(String s[]){
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            new Notepad();
        }
    });
}
}
Was it helpful?

Solution 2

It does seem like it should be, I don't see any competing components.

Regardless, jf.add(textScroll,"w 100%, h 100%,span,wrap"); will work, though you might need to trim a few pixel off to keep it from clipping the frame decoration (i.e. jf.add(textScroll,"w 100%-10, h 100%-10,span,wrap"), though I don't believe trimming will be required.

OTHER TIPS

Looks like a bug to me: moving the wrap from the component constraint to the layout constraint seems to make it behave as expected.

jf = new JFrame("Notepad");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLayout(new MigLayout("debug, fill, wrap 1", "", ""));
// add a label to better see the cell vs. component size
// fills as expected without component wrap
jf.add(new JLabel("nothing"), "grow");
// bug: doesn't fill vertically with component wrap
//jf.add(new JLabel("nothing"), "grow, wrap");
jf.setSize(800, 600);
jf.setVisible(true);

This might (or not, didn't dig) be related to an issue reported last summer

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