Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Devaka Cooray
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Jeanne Boyarsky
Tim Cooke
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Bartenders:
Forum:
Swing / AWT / SWT
GridBag Problem (Replacing Component)
Jerry Crothers
Ranch Hand
Posts: 34
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have a class (JPanel) thats uses the gridbag layout - its a form.
One of the event handlers needs to remove a textfield and replace it with a another component (JButton). So I am writing code like this to remove the textfield:
this.remove(textfield);
this.repaint();
this.setVisible(true);
thats fine, but it won't let me create a new JButton (or any component) and put it in that cell. So then I tried to put the original textfield back:
add(textfield, gridbagconstraints);
..and it was there. Even just add(textfield) put the textfield back.
Why I can not put another component in the original cell after the textfield is removed??
Craig Wood
Ranch Hand
Posts: 1535
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class GridBagTest implements ActionListener { JTextField textField; JPanel panel; int count = 0; public void actionPerformed(ActionEvent e) { Component c = panel.getComponent(2); GridBagLayout layout = (GridBagLayout)panel.getLayout(); GridBagConstraints gbc = layout.getConstraints(c); panel.remove(c); if(c == textField) { JButton button = new JButton("button " + ++count); layout.setConstraints(button, gbc); panel.add(button, 2); } else { layout.setConstraints(textField, gbc); panel.add(textField, 2); } panel.revalidate(); panel.repaint(); } private JPanel getContent() { textField = new JTextField("swap me", 12); panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; gbc.weighty = 1.0; gbc.gridwidth = GridBagConstraints.RELATIVE; panel.add(new JLabel("label 1"), gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; panel.add(new JLabel("another label"), gbc); gbc.gridwidth = GridBagConstraints.RELATIVE; panel.add(textField, gbc); gbc.gridwidth = GridBagConstraints.REMAINDER; panel.add(new JButton("another button"), gbc); return panel; } private JPanel getControl() { JButton button = new JButton("swap components"); button.addActionListener(this); JPanel panel = new JPanel(); panel.add(button); return panel; } public static void main(String[] args) { GridBagTest test = new GridBagTest(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test.getContent()); f.getContentPane().add(test.getControl(), "Last"); f.setSize(300,200); f.setLocation(200,200); f.setVisible(true); } }
Jerry Crothers
Ranch Hand
Posts: 34
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Geez after I added:
panel.revalidate();
it worked. Thats annoying. Thanks for the code sample though.
I love a good mentalist. And so does this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
grid bag panels overlap : please HELP if you can
faceing problem while using GridBagLayout
null pointer exception
GUI - tile
Borders
More...