Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7054785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:40:09+00:00 2026-05-28T03:40:09+00:00

I’m trying to do as the title says. I’ve got an efficient way for

  • 0

I’m trying to do as the title says.

I’ve got an efficient way for posting several of the same swing objects to a frame by storing them in an array and adding them using a for loop like so:

JLabel[] contrllabels= new JLabel[8];
contrllabels[0] = new JLabel("SCF Type: ");
contrllabels[1] = new JLabel("Units: ");
contrllabels[2] = new JLabel("Spherical Harmonics: ");
contrllabels[3] = new JLabel("Molecular Charge: ");
contrllabels[4] = new JLabel("PP: ");
contrllabels[5] = new JLabel("DFT Type: ");
contrllabels[6] = new JLabel("Max Iterations: ");
contrllabels[7] = new JLabel("Mult: ");


for(int i = 0;i<contrllabels.length;i++){
    c.gridy = i;
    frame.add(contrllabels[i],c);
}

But what if there are several swing objects of different types? I’ve got several combo boxes and textfields which I’d like to be added to the frame in a similar manner. I use gridbaglayout so if I don’t use a for loop, I end up with lots of unnecessary code due to giving the constraints new values every time I want a different object added.

Is there such thing as an array of references which point to these different objects which I can then iterate through to add to the frame? Something like

JTextField tf = new JTextField(5);
JComboBox cb = new JComboBox("example");

Swing[] array = {tf,cb}

for(int i = 0;i<array.length;i++){
    c.gridy = i;
    frame.add(array[i],c);
}

I know such an array doesn’t exist, but is there some way of implementing such a thing? It would greatly reduce the number of lines in my code and make it less confusing.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:40:10+00:00Added an answer on May 28, 2026 at 3:40 am

    You could use an array or collection of a common super type such as an array or ArrayList of JComponent. I’m curious if you’re using a parallel array of GridBagConstraints to go with each component that is being added — ugh. I often use arrays of components but usually if they are like components such as JLabel/JTextField pairs or a cluster of JRadioButtons.

    As an aside, for my money, I try to avoid GridBagLayout as much as possible and instead nest containers that use the more coder-friendly layouts.

    For instance this small GUI was made with a combination of FlowLayout, BoxLayout, BorderLayout and GridLayout:

    enter image description here

    The large JPanel that holds the whole GUI uses BorderLayout, The JTextArea in the center is placed BorderLayout.CENTER, the Provider JLabel and JTextField at the top are in a FlowLayout JPanel that is placed overall BorderLayout.NORTH, the bottom buttons are in a JPanel that uses GridLayout(1, 0, 5, 0) which is held in another JPanel that uses FlowLayout which is placed in the GUI BorderLayout.SOUTH, and the stuff on the right are in a BoxLayout using JPanel.

    For example:

    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.awt.Window;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.UIManager;
    import javax.swing.UIManager.LookAndFeelInfo;
    import javax.swing.UnsupportedLookAndFeelException;
    
    @SuppressWarnings("serial")
    public class GetLetterTextGui extends JPanel {
       private static final int TA_ROWS = 20;
       private static final int TA_COLS = 35;
       private static final int PROVIDER_FIELD_COLS = 10;
       private static final String GUI_TITLE = "Get Letter Text";
       private JList letterList;
       private JTextArea textarea = new JTextArea(TA_ROWS, TA_COLS);
       private JTextField providerField = new JTextField(PROVIDER_FIELD_COLS);
       private JCheckBox addValedictionChkBox = new JCheckBox("Add Valediction", true);
    
       public GetLetterTextGui() {
          letterList = new JList(new String[]{"Fe", "Fi", "Fo", "Fum"});
    
          providerField.setText("John Smith, MD");
    
          textarea.setWrapStyleWord(true);
          textarea.setLineWrap(true);
    
          JPanel northPanel = new JPanel();
          northPanel.add(new JLabel("Provider:"));
          northPanel.add(providerField);
    
          JPanel southPanel = new JPanel();
          JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
          btnPanel.add(new JButton("Copy to Clipboard"));
          btnPanel.add(new JButton("Clear"));
          btnPanel.add(new JButton(new ExitAction()));
          southPanel.add(btnPanel);
    
          JPanel eastPanel = new JPanel();
          eastPanel.setLayout(new BoxLayout(eastPanel, BoxLayout.PAGE_AXIS));
          eastPanel.add(new JScrollPane(letterList));
          eastPanel.add(new JPanel() {{add(addValedictionChkBox);}});
    
          int eb = 0;
          setBorder(BorderFactory.createEmptyBorder(eb, eb, eb, eb));
          setLayout(new BorderLayout(eb, eb));
          add(northPanel, BorderLayout.PAGE_START);
          add(eastPanel, BorderLayout.LINE_END);
          add(new JScrollPane(textarea), BorderLayout.CENTER);
          add(southPanel, BorderLayout.PAGE_END);
       }
    
    
       private class ExitAction extends AbstractAction {
          private final Object MNEMONIC = new Integer(KeyEvent.VK_X);
    
          public ExitAction() {
             super("Exit");
             putValue(MNEMONIC_KEY, MNEMONIC);
          }
    
          @Override
          public void actionPerformed(ActionEvent evt) {
             Window win = SwingUtilities.getWindowAncestor(GetLetterTextGui.this);
             win.dispose();
          }
       }
    
       private static void createAndShowGui() {
          GetLetterTextGui mainPanel = new GetLetterTextGui();
    
          JFrame frame = new JFrame(GUI_TITLE);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          try {
             for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                   UIManager.setLookAndFeel(info.getClassName());
                   break;
                }
             }
    
             SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                   createAndShowGui();
                }
             });
    
          } catch (ClassNotFoundException e) {
             e.printStackTrace();
          } catch (InstantiationException e) {
             e.printStackTrace();
          } catch (IllegalAccessException e) {
             e.printStackTrace();
          } catch (UnsupportedLookAndFeelException e) {
             e.printStackTrace();
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
i got an object with contents of html markup in it, for example: string
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.