Results 1 to 7 of 7
- 06-13-2010, 04:39 AM #1
Member
- Join Date
- Jun 2010
- Posts
- 14
- Rep Power
- 0
new JLabels not showing in JPanel (doing revalidate&repaint)
Hello all,
I am trying to add components to a JPanel that is already displayed.
Whenever I do something like
Java Code:panel.add(new JPanel("hello world!")); panel.revalidate(); panel.repaint();
I've tried different layout managers as well, but lets assume its Flow for example's sake.
I did an extensive Google search, and this is a common question, however the common solution by revalidate() and repaint() methods does not work!
I made the following test program in NetBeans to demonstrate the problem:
Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /* * Main.java * * Created on Jun 12, 2010, 9:51:52 PM */ //package addcomponent; import javax.swing.*; /** * * @author Vladislav Sorkin */ public class Main extends javax.swing.JFrame { /** Creates new form Main */ public Main() { initComponents(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { button = new javax.swing.JButton(); panel = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new java.awt.FlowLayout()); button.setText("add label"); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonActionPerformed(evt); } }); getContentPane().add(button); panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel); panel.setLayout(panelLayout); panelLayout.setHorizontalGroup( panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 247, Short.MAX_VALUE) ); panelLayout.setVerticalGroup( panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 190, Short.MAX_VALUE) ); getContentPane().add(panel); pack(); }// </editor-fold> [B][COLOR="Red"] private void buttonActionPerformed(java.awt.event.ActionEvent evt) { JLabel label = new JLabel("hello world!"); panel.add(label); label.setVisible(true); panel.revalidate(); panel.repaint(); this.repaint(); }[/COLOR][/B] /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Main().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton button; private javax.swing.JPanel panel; // End of variables declaration }
All help is appreciated.Last edited by r00tb33r; 06-13-2010 at 04:43 AM. Reason: formatting
-
The last layout you want to use for this is GroupLayout. You say that you tried this with FlowLayout, yet we can't see how you've tried, nor what you did wrong. I suggest that you study the layout manager tutorial and then try to do this using some of the more user-friendly layout managers. You may find the tutorials starting here: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
Also note that often containers that use different layout managers are nested to achieve a goal. For instance:
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class AddPanels { private static void createAndShowUI() { JPanel panel = new AddPanelsGui().getMainPanel(); JFrame frame = new JFrame("Add Check Panels"); frame.getContentPane().add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class AddPanelsGui { private JPanel mainPanel = new JPanel(); private JPanel holderPanel = new JPanel(new GridLayout(0, 1)); private int panelCount = 0; public AddPanelsGui() { int checkPanelCnt = 3; for (int i = 0; i < checkPanelCnt ; i++) { holderPanel.add(new AddPanelsCheckBoxPanel(panelCount).getPanel()); panelCount++; } JButton addCheckPanelBtn = new JButton("Add Check Panel"); JPanel southPanel = new JPanel(); southPanel.add(addCheckPanelBtn); addCheckPanelBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { holderPanel.add(new AddPanelsCheckBoxPanel(panelCount).getPanel()); panelCount++; holderPanel.revalidate(); } }); JPanel innerBorderLOPanel = new JPanel(new BorderLayout()); innerBorderLOPanel.add(holderPanel, BorderLayout.NORTH); JScrollPane scrollPane = new JScrollPane(innerBorderLOPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); Dimension iblopSize = innerBorderLOPanel.getPreferredSize(); iblopSize = new Dimension(iblopSize.width, 2 * iblopSize.height); scrollPane.getViewport().setPreferredSize(iblopSize); int eb = 10; mainPanel.setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb)); mainPanel.setLayout(new BorderLayout()); mainPanel.add(scrollPane, BorderLayout.CENTER); mainPanel.add(southPanel, BorderLayout.SOUTH); } public JPanel getMainPanel() { return mainPanel; } } class AddPanelsCheckBoxPanel { private JPanel panel = new JPanel(); public AddPanelsCheckBoxPanel(int panelCount) { panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10)); panel.add(new JCheckBox()); panel.add(new JLabel("Label: " + panelCount)); panel.add(new JTextField(20)); panel.setBorder(BorderFactory.createLineBorder(Color.black)); } public JPanel getPanel() { return panel; } }
Last edited by Fubarable; 06-13-2010 at 05:26 AM.
- 06-13-2010, 05:40 AM #3
Member
- Join Date
- Jun 2010
- Posts
- 14
- Rep Power
- 0
OK, I'm not and now that you say that I won't.
Really? I even highlighted it for you in the original post. I really can't add much if you aren't even looking. I will provide excerpts from my first post however:
Java Code:... getContentPane().setLayout(new java.awt.FlowLayout()); ...
Java Code:... private void buttonActionPerformed(java.awt.event.ActionEvent evt) { JLabel label = new JLabel("hello world!"); panel.add(label); label.setVisible(true); panel.revalidate(); panel.repaint(); this.repaint(); } ...
Thanks but I've started there, and I have studied it. Hows this relevant to the problem anyway? I have objects that aren't being shown, I'm not concerned about the alignment at this point.
Thanks for trying but its not helpful.
[EDIT]
Apparently my JPanel did not inherit the layout from the parent content pane. I had a JLabel display just now, but I won't rush calling it solved.Last edited by r00tb33r; 06-13-2010 at 05:55 AM.
-
You are adding JLabels to panel not to the contentPane, and panel most definitely does not use FlowLayout but rather GroupLayout. Do you want me to point out the relevant code?
Thanks for trying but its not helpful.
- 06-13-2010, 06:15 AM #5
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 10
I think he's right. If you get rid of all that GroupLayout crap-code, your code works.
Java Code:import java.awt.Dimension; import javax.swing.*; public class RootMain extends javax.swing.JFrame { public RootMain() { initComponents(); } private void initComponents() { button = new javax.swing.JButton(); panel = new javax.swing.JPanel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); getContentPane().setLayout(new java.awt.FlowLayout()); button.setText("add label"); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonActionPerformed(evt); } }); getContentPane().add(button); panel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); panel.setPreferredSize(new Dimension(400, 400)); getContentPane().add(panel); pack(); } private void buttonActionPerformed(java.awt.event.ActionEvent evt) { JLabel label = new JLabel("hello world!"); panel.add(label); label.setVisible(true); panel.revalidate(); panel.repaint(); this.repaint(); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new RootMain().setVisible(true); } }); } private javax.swing.JButton button; private javax.swing.JPanel panel; }
By the way, nice job biting the hand that feeds you. Good show.
-
- 06-16-2010, 07:03 AM #7
Member
- Join Date
- Jun 2010
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Adding a JLabel to a JPanel - jlabel not showing
By Bongeh in forum New To JavaReplies: 17Last Post: 04-07-2010, 12:02 AM -
Showing JList in a JPanel
By nico.hvi in forum AWT / SwingReplies: 0Last Post: 03-10-2010, 03:26 PM -
Overlapping JLabels
By techbossmb in forum AWT / SwingReplies: 3Last Post: 09-21-2009, 04:21 PM -
[SOLVED] JLabel not showing on JPanel
By onefootswill in forum New To JavaReplies: 11Last Post: 08-23-2008, 02:32 PM -
problem with JLabels
By geork in forum New To JavaReplies: 3Last Post: 01-31-2008, 03:30 PM
Bookmarks