简体   繁体   中英

Swing GUI moving when user enters text

I am trying to do a fairly basic swing GUI where a user can enter a url, choose a local file location etc. I am using multiple layout managers including boxlayout, borderlayout and flowlayout. The code is below. My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?

Setup menu bar
    JButton menu_File = new JButton("File");
    JButton menu_Edit = new JButton("Edit");
    JToolBar toolBar = new JToolBar();
    toolBar.add(menu_File);
    toolBar.add(menu_Edit);

    //Setup options area
    JPanel options = new JPanel();
    options.setBorder(BorderFactory.createTitledBorder("Options"));
    BoxLayout layout_Options = new BoxLayout(options, BoxLayout.Y_AXIS);
    options.setLayout(layout_Options);
    JLabel optionsLblURL = new JLabel("Enter URL:");
    optionsTxtUrl = new JTextArea(1,15);        
    JLabel chooseDestLbl = new JLabel("Choose save location:");
    chooseDest = new JButton("Browse");
    chooseDest.addActionListener(this);
    options.add(optionsLblURL);
    options.add(optionsTxtUrl);
    options.add(chooseDestLbl);
    options.add(chooseDest);

    //Setup launch area
    JPanel launch = new JPanel();
    launch.setBorder(BorderFactory.createTitledBorder("Launch"));
    launchBtnStart = new JButton("Start Download");
    launchBtnStart.setVerticalAlignment(SwingConstants.CENTER);
    launchBtnStart.setHorizontalAlignment(SwingConstants.CENTER);
    launchBtnStart.addActionListener(this);
    launch.add(launchBtnStart);

    //Setup reporting area
    JPanel logging = new JPanel();
    logging.setBorder(BorderFactory.createTitledBorder("Log"));
    BoxLayout layout_Logging = new BoxLayout(logging, BoxLayout.Y_AXIS);
    logging.setLayout(layout_Logging);
    JTextArea loggingTxt = new JTextArea(3,10);
    loggingTxt.setEditable(false);
    logging.add(pb);
    logging.add(loggingTxt);

    //Add components to window        
    BorderLayout borderLayout = new BorderLayout();
    setLayout(borderLayout);
    add("North", toolBar);
    add("West", options);
    add("East", launch);
    add("South", logging);

    setVisible(true);

"My problem is that some of the components are moving around when the user puts text into the optionsTxt jtextarea. Anyone know where I should start to stop this happening?"

Start by putting your JTextArea in a ScrollPane and setLineWrap(true) and setWrapStyleWord(true) . You may want to consider doing this with both JTextArea s you have

JTextArea optionsTxtUrl = new JTextArea(1,15);
optionsTxtUrl.setLineWrap(true);
optionsTxtUrl.setWrapStyleWord(true);
JScrollPane scroll = new JScrollPane(optionsTxtUrl);

options.add(scroll); // take out options.add(optionsTxtUrl);

This will make your lines wrap when they reach the right edge of the text area

  • public void setWrapStyleWord(boolean word) - Sets the style of wrapping used if the text area is wrapping lines. If set to true the lines will be wrapped at word boundaries (whitespace) if they are too long to fit within the allocated width. If set to false, the lines will be wrapped at character boundaries. By default this property is false.

  • public void setLineWrap(boolean wrap) - Sets the line-wrapping policy of the text area. If set to true the lines will be wrapped if they are too long to fit within the allocated width. If set to false, the lines will always be unwrapped. A PropertyChange event ("lineWrap") is fired when the policy is changed. By default this property is false.


If this doesn't solve your problem, you should edit your post with a Minimal, Complete, Tested and Readable example so we can test out your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM