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

verifyGoogleJavaFormat FAILED in intelliJ
Sending multiple email over javax.mail
Modelling tree/hierarchy structure in Spring Data Neo4j 4.1
How to merge my client's log4j property file into my jar's one, sl4j?
String to Integer Expressions
JLabel background distorts while using JXDatePicker
Error creating bean - Spring Boot
Android ListView how can I get the value of each individual row
what is need of service layer in spring mvc ? what kind of logic recommended?
Is it possible to generate html file with angularjs script using server side java program?
How to analyze performance of Objectify?
How to transform the key and value of a each entry set of a Map using Java 8?
How do i change the printing output for an enum, as enum values are all upper case [duplicate]
Sum of the difference of pairs of numbers in an array - Java
Is there a Java library which is similar to the C# WSTrustChannelFactory
Not able to delete image file using JAVA

Categories

HOME
sendgrid
twitter
openlayers
amazon-ec2
reflection
tizen
rsyslog
yahoo-oauth
spring-jdbc
callback
mouse
communication
spring-kafka
row
resize
remote-access
messages
hex-editors
event-handling
iolanguage
django-simple-history
http-status-code-504
connection-string
sms-gateway
jqwidget
shared-hosting
conemu
url-scheme
pingfederate
fluentvalidation
strncpy
xacml
replaceall
madlib
yadcf
oracle-fusion-middleware
typed.js
hybridauth
avcapturesession
amazon-kinesis-kpl
mpmediaquery
jmonkeyengine
http-digest
x11-forwarding
neuroscience
mixture-model
botbuilder
unixodbc
wso2carbon
taffy
scrollable
powercli
tizen-native-app
integrity
nstextview
directory-structure
pg-dump
bind9
smart-table
google-cdn
angular-cache
sts-springsourcetoolsuite
adxstudio-portals
php-internals
pyke
netmq
endeca-workbench
eclipse-clp
t4mvc
marching-cubes
libressl
sframe
appfabric-cache
sorl-thumbnail
jsonpickle
winddk
wordpress-theme-customize
markers
elliptic-curve
comexception
centos5
viadeo
prettify
ccss
jsctypes
shellexecute
rabl
android-hardware
distutils
rdoc
runas
objective-c-2.0
chrono
dotnethighcharts
f#-powerpack
mysql-error-1005
createwindow
ncqrs
swing-app-framework
ajax-forms





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