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

NullPointerException error for adding to ArrayList via JFrame
How to find the next in order successor in a binary tree?
SpingMVC: org.springframework.context.i18n.LocaleContextHolder not found
explain what happened (C program) [closed]
Java get AudioFormat type for Mic recording
quartz job isn't running but quartz tables getting updated
Java Can the same event be triggerd multiple times and be running at the same time?
My app is running but the screen is black, and crashes when trying to use a custom view
Write to file last twenty(20) results
Single Linked List remove object after parameter Java
Disable JSF warning for pattern in Eclipse
Restricting Java generic parameter type to a list of final classes
Why is a jar file needed while executing MapReduce code in hadoop but not needed in executing any other Non-MapReduce Java code in hadoop
Multiple Textures bound to single VBO and Rendered With Shaders (LWJGL)
How to handle password Strings in ADF Faces?
2D Array in Java with number manipulation

Categories

HOME
coq
plone
plot
cplex
framework7
elm
sharepoint-designer
binary-tree
fancybox
portia
dax
alignment
messages
fallback
quickfix
msp430
telephony
windows-7-x64
undefined
google-pagespeed
introduction
tokenize
errorlevel
csrf-protection
cultureinfo
h2db
user-interaction
titanium-mobile
form-data
geopositioning
y86
mmenu
jmonkeyengine
disassembling
espeak
opshub
galen
texmaker
rainbowtable
xml-documentation
youcompleteme
atomicity
boost-multi-index
csound
pycaffe
migradoc
word-vba-mac
vmware-tools
asp.net-4.5
ctest
player
blackberry-10
pagerank
itextpdf
r-tree
endeca-workbench
ibaction
responsive-images
fluid-dynamics
notify
gwidgets
android-imagebutton
markers
rdl
phpthumb
mechanize-ruby
bundles
concurrent-collections
ember-charts
app42
geos
ruby-datamapper
android-screen-support
orchardcms-1.7
ember-app-kit
lcs
jelly
wsdl-2.0
simba
hamiltonian-cycle
propertyeditor
onsubmit
dbproviderfactories
qtkit
spring-portlet-mvc
code-cleanup
fireworks
xdomainrequest
lang
coda-slider
dentrix
mirah
microsoft-virtualization
adrotator
text-coloring

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