java


Swing GridBagLayout can't distribute(remove) empty spaces


Even though I have read something about weighty I couldn't remove spaces given code fragment below:
JPanel panel = new JPanel();
scrollPane.setViewportView(panel);
GridBagLayout gbl_panel = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints ();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
for(int i = 0;i<3;i++){
gc.gridy = i;
panel.add(new JButton("hi"),gc);
}
That's what I get
That's what I want
It distributes these 3 buttons with equal spaces(top,middle,bottom)
If the components total size is bigger than panel's size(for example add 10 button) there is no problem.But if the total size of components smaller than panel's size it distributes all spaces.I want to set my components line by line(top to bottom withoud any spaces between components)
Sorry for inconvenience thanks !
You need GridBagConstraints fill field
From JavaDocs
* This field is used when the component's display area is larger
* than the component's requested size. It determines whether to
* resize the component, and if so, how.
fill defines whether component should be resized and how.
weight defines how much extra space the component should use
UPDATE:
Add any JComponent e.g. JPanel after all your buttons and set fill for the JPanel and weight=1. Your buttons' weights are 0 so all the extra space is added to the panel.

Related Links

popping an element of a queue at initialisation of for loop ends up popping the same element all the time
If data gets updated in the db within catch time how to handle in hibernate?
Log file not rolling automatically in jetty 9.3.16
Formatting Integer with a decimal point
Thread.join() explanation
How to use stream#sorted() to sort by the product of two fields in Java 8
Is it possible to add customize method in String class? [duplicate]
Android Gradle Errors
partition in a singly linked list
Parse soap xml response to java Object
Getting 400 BAD REQUEST when uploading a blob to JAVA server Jersey API
Check the status of updateRequest in ElasticSearch in Java
Prevent HighlightPainter from overriding selection color in Java
Showing a stage from the Main class in JavaFX
Is it sensible to use a public and private key encryption between a client server model?
javax.naming.NoInitialContextException when accessing the application

Categories

HOME
hashmap
webstorm
framework7
routes
tizen-web-app
alpha
in-app-purchase
paradox
add
amazon-cloudformation
adobe-analytics
reveal.js
hapi
vaadin7
ghc
seaborn
transformation
oxyplot
samoa
predix
mustache.php
tdd
bootstrap-duallistbox
user-interaction
linkerd
atlassian-crucible
frame
http-status-code-503
contact-form
siesta-swift
jspresso
uiswipegesturerecognizer
azure-sql-database
ghost4j
y86
html5-fullscreen
bower-install
dosbox
serve
x11-forwarding
multilingual
gesture
withings
turbogears
jsch
smartcontracts
jedis
react-scripts
jquery-validate
worksheet
hls.js
rdfs
directory-structure
libusb-win32
pdfclown
eventkit
root-framework
passport-google-oauth
player
angstrom-linux
react-native-listview
make-install
independentsoft
browser-link
freelancer.com-api
endeca-workbench
collapse
cannon.js
qdialog
feedback
purge
content-length
php-ci
proj4js
codeigniter-url
tablelayout
phpthumb
poller
dealloc
centos5
flexmojos
odata4j
dotnetnuke-5
easy-install
ora-00911
yui-compressor
jelly
cassini-dev
hamiltonian-cycle
mt
broken-links
armcc
twrequest
objective-c-2.0
ticoredatasync
reddot
libc++
gnustep
invite
dmx512
dentrix
icanhaz.js
mirah
audio-capture
mtj
xetex
django-notification
.net-1.0

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App