java


Placing a JPanel on a new line using GridBagLayout


I'm trying to figure out a way to place a panel containing combo boxes in my JFrame on a new line using gridbaglayout. At the moment it appears next to another panel of the same type.
Here's what it looks like:
Here is the code for the particular areas
JPanel saleOfferPanel = new JPanel();
JLabel dateOfSale = new JLabel("Select Date of Sale:");
saleOfferPanel.add(dateOfSale);
JLabel saleDay = new JLabel("Day:");
saleOfferPanel.add(saleDay);
JComboBox<Integer> dayOfSale = new JComboBox<>();
saleOfferPanel.add(dayOfSale);
JLabel saleMonth = new JLabel("Month:");
saleOfferPanel.add(saleMonth);
JComboBox<Integer> monthOfSale = new JComboBox<>();
saleOfferPanel.add(monthOfSale);
JLabel saleYear = new JLabel("Year:");
saleOfferPanel.add(saleYear);
JComboBox<Integer> yearOfSale = new JComboBox<>();
saleOfferPanel.add(yearOfSale);
JPanel endSalePanel = new JPanel();
JLabel endOfSale = new JLabel("Select End Date of Sale:");
saleOfferPanel.add(endOfSale);
JLabel endDay = new JLabel("Day:");
saleOfferPanel.add(endDay);
JComboBox<Integer> endDayOfSale = new JComboBox<>();
saleOfferPanel.add(endDayOfSale);
JLabel endMonth = new JLabel("Month:");
saleOfferPanel.add(endMonth);
JComboBox<Integer> endMonthOfSale = new JComboBox<>();
saleOfferPanel.add(endMonthOfSale);
JLabel endYear = new JLabel("Year:");
saleOfferPanel.add(endYear);
JComboBox<Integer> endYearOfSale = new JComboBox<>();
saleOfferPanel.add(endYearOfSale);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 0.5;
gbc.weighty = 0;
gbc.anchor = GridBagConstraints.NORTHWEST;
//other panels...
//panels in question
gbc.gridx = 0;
gbc.gridy = 9;
gbc.gridheight = 1;
add(saleOfferPanel, gbc);
gbc.gridx = 0;
gbc.gridy = 10;
gbc.gridheight = 1;
add(endSalePanel, gbc);
Hoping that someone may be able to offer assistance.
Thanks
I have changed your code a little and succeeded changing its position. here is the code snippet.
`// setLayout(new GridBagLayout());
GridBagLayout gb = new GridBagLayout();
this.setLayout(gb);
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = 1;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.BOTH;
//other panels...
//panels in question
gbc.gridwidth = GridBagConstraints.REMAINDER; //this will tell GridBagLayout component below should be last one on this line.
gb.setConstraints(saleOfferPanel, gbc);
this.add(saleOfferPanel);
gb.setConstraints(endSalePanel, gbc);
this.add(endSalePanel);
this.pack();
this.setSize(500, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);`
Look carefully at the code that creates and populates endSalePanel:
JPanel endSalePanel = new JPanel();
JLabel endOfSale = new JLabel("Select End Date of Sale:");
//[KA] Here's where it all starts to go wrong!!! You wanted to add
// endOfSale to endSalePanel, but...
saleOfferPanel.add(endOfSale);
JLabel endDay = new JLabel("Day:");
saleOfferPanel.add(endDay);
Cut-and-paste strikes again!

Related Links

How prepare simple crud java project for git
Failed to start appium: TypeError
What's the equivalent of Java Android.graphics.Paint setColor(int color) in Xamarin - C#?
Making an RxJava Operator Chain Concurrent
Polar Point Distance
Selenium Webdriver Web crawler
I can't find out what is the error in this?
Using SpringFrameWork #Async for methods that return void
foreach in Java: is having the statement which calculates the iterable object worse than having a variable in the loop? [duplicate]
How can I display chosen array contents to the user in java?
Eclipse dark theme on mac
ArrayIndexOutOfBoundsException when reading a file [duplicate]
How do I inject an object into Android Priority Job Queue with Dagger 2?
The Method is undefined for the type object Eclipse error
Reading CA Cert Private Key to Sign Certificate
Java applet cannot be run

Categories

HOME
magnific-popup
plot
homebrew
react-virtualized
react-redux
amazon-ecs
twitter-bootstrap-4
retrofit
applepay
awesome-wm
cloudkit
export-to-csv
apache-cayenne
fallback
try-catch
clearcase-ucm
django-simple-history
pc
ef-migrations
one-hot-encoding
riot.js
rundeck
pepper
react-css-modules
traffic
facebook-apps
binary-data
ioio
create-table
contact-form
copying
textmate
subset-sum
azure-sql-database
wallpaper
nssegmentedcontrol
ios5
http-digest
bitbucket-pipelines
scaffold
dynamics-crm-2013
service-discovery
impersonation
g-code
gpx
babel-core
qwt
angular-resource
yii2-extension
svmlight
nstextview
imanage
mcafee
color-profile
django-scheduler
medium.com
ableton-live
infix-notation
dstu2-fhir
fody
mikroc
py2app
django-debug-toolbar
react-native-listview
bluegiga
design-by-contract
itextpdf
ford-fulkerson
endeca-workbench
feedback
citrus-pay
content-length
ibaction
graph-drawing
truevault
kineticjs
qpainter
cdt
device-manager
gulp-less
article
quantlib-swig
access-rights
html4
yui-compressor
spring-validator
eventlistener
gdata-api
window-management
forums
perfect-hash
uimenucontroller
yagni
pascal-fc
mediarss

Resources

Encrypt Message