java - Setting the width of a JList in a JScrollPane with GridBagLayout -


i have example java swing code below produces following screenshot (screenshot has been edited make smaller):

jlist in jscrollpane not expected width here code:

import java.awt.gridbagconstraints; import java.awt.gridbaglayout; import java.awt.insets;  import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jlist; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.swingutilities;   public class main extends jframe {      public main() {         jlisttest thegui = new jlisttest();         setdefaultcloseoperation(jframe.exit_on_close);         setresizable(false);         add(thegui);         pack();         setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             public void run() {                 new main();             }         });     }      private class jlisttest extends jpanel {         private static final int padding = 3;         private jlabel label;         private jtextarea textarea;         private string listoptions[] =              {"here veeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrry long text",              "here more loooooooooooooooooooooooooooooooooooooooooooooooooooooong text",              "here veeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrry long text",              "here more loooooooooooooooooooooooooooooooooooooooooooooooooooooong text",              "here veeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrry long text",              "here more loooooooooooooooooooooooooooooooooooooooooooooooooooooong text",              "here veeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrry long text",              "here more loooooooooooooooooooooooooooooooooooooooooooooooooooooong text",              "here veeeeeeeeeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrry long text",              "here more loooooooooooooooooooooooooooooooooooooooooooooooooooooong text"};         private jlist<string> list;         private jscrollpane scrollpane;         private gridbagconstraints gbc;          public jlisttest() {             super(new gridbaglayout());             label = new jlabel("here label:");             textarea = new jtextarea(20, 50);             list = new jlist<string>(listoptions);             scrollpane = new jscrollpane(list);              gbc = new gridbagconstraints();             gbc.gridheight = 1;             gbc.insets = new insets(padding, padding, padding, padding);             gbc.anchor = gridbagconstraints.line_start;              gbc.gridwidth = 4;             gbc.gridx = 0;             gbc.gridy = 0;             add(label, gbc);              // want scrollpane take 1/4 width of window.             gbc.gridwidth = 1;             gbc.gridx = 0;             gbc.gridy = 1;             add(scrollpane, gbc);              // want textarea take 3/4 width of window.             gbc.gridwidth = 3;             gbc.gridx = 1;             gbc.gridy = 1;             add(textarea, gbc);         }     } } 

as can see, have jlist containing items long text inside of jscrollpane on left, , jtextarea on right. jscrollpane/jlist take 1/4 of window's width , have horizontal scrollbar appear when text of jlist items gets long. jtextarea take 3/4 of window's width.

i tried gridbaglayout, not doing right since appearance off.

any appreciated. thanks!

gridbaglayout wants work preferred size's of components, 1 of weird points think can , can conflict.

essentially, want do, override layout decisions gridbaglayout making own logic (you take 25% of width , take 75% of width). problem is, gridbaglayout doesn't think way, thinks more along lines of, can have 25% of space left on after i've calculated everybody's preferred layout requirements , can have 75%. doesn't come out way thought should.

the first step, make use of weightx , weighty

// want scrollpane take 1/4 width of window. gbc.fill = gridbagconstraints.both; gbc.gridwidth = 1; gbc.gridx = 0; gbc.gridy = 1; gbc.weightx = 0.25; gbc.weighty = 1; add(scrollpane, gbc);  // want textarea take 3/4 width of window. gbc.gridx = 1; gbc.gridy = 1; gbc.weightx = 0.75; add(textarea, gbc); 

this provides sizing hints gridbaglayout in should left on space might have after it's done it's initial layout.

but, wait, doesn't seem change anything! no, doesn't, unless resize window enough, said, components layed out (as best can) preferred size. jlist , jtextarea wrangling things against us.

a jscrollpane get's it's preferred size view component. jlist implements scrollable interface provides getpreferredscrollableviewportsize method, used jscrollpane it's preferred size (otherwise uses components preferred size)

so, can little cheeky , modify preferred viewport width...

list = new jlist<string>(listoptions) {     @override     public dimension getpreferredscrollableviewportsize() {         dimension size = super.getpreferredscrollableviewportsize();         size.width = 100;         return size;     } }; 

(ps, did test setfixedcellwidth , setprototypecellvalue, both these prevented horizontal scroll bars been presented).

okay, works little better :p. there time minimum size compete preferred size, beware of :p

remember though, weightx/y works remaining space, not effect initial layout :p


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -