Labels would not show up in the output window. The red highlighted code is where said labels get added to the window. I can't tell what's wrong with the code and I've looked at it over and over.

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Preferences extends JFrame {
  
  // Declare variables:
  
  // Panels
  private JPanel shapeType;
  private JPanel fillState;
  private JPanel gradientState;
  private JPanel lines;
  private JPanel innerPanel;
  
  // Booleans
  private boolean hasLayout;
  
  // Labels
  private JLabel label_shapeType;
  private JLabel label_fillState;
  private JLabel label_gradientState;
  private JLabel label_gradientCols;
  private JLabel label_lineWidth;
  private JLabel label_dashState;
  private JLabel label_dashLength;
  
  // Buttons
  private JButton but_commit;
  
  // Constructor
  public Preferences() {
    
    // Window properties
    super( "Preferences" ); // Title
    setLayout( new GridLayout(5,1) ); // Grid layout with five columns
    
    // Initialize panels into grid
    newPanel( shapeType );
    newPanel( fillState );
    newPanel( gradientState );
    newPanel( lines );
    innerPanel = new JPanel();
    
    // Initialize labels into the panels
    newLabel( label_shapeType, "Shape Type", shapeType, 1 );
    newLabel( label_fillState, "Fill State", fillState, 1 );
    newLabel( label_gradientState, "Gradient State", gradientState, 2 );
    newLabel( label_gradientCols, "Gradient Colours", gradientState, 2 );
    hasLayout = false;
    newLabel( label_lineWidth, "Line Width", lines, 3 );
    newLabel( label_dashState, "Dash State", lines, 3 );
    newLabel( label_dashLength, "Dash Length", lines, 3 );
    
    // Initialize buttons into grid/panels
    but_commit = new JButton( "Commit Changes" );
    add( but_commit );
    
  }
  
  private JPanel newPanel( JPanel panel ) {
    
    // Initialize panel and set its layout
    panel = new JPanel();
    panel.setLayout( new BorderLayout() );
    
    // Add panel into JFrame
    add( panel );
    
    // Return the new panel
    return panel;
    
  }
  
  private JLabel newLabel( JLabel label, String text, JPanel panel, int numLabels ) {
    
    // Initialize label
    label = new JLabel( text );
    // Initialize panel
    panel = new JPanel();
    
    // If there is more than one label to be added
    if( numLabels > 1 ) {
      // Give innerPanel rows depending on the number of labels
      // This will happen only if the layout has not already been set
      if ( hasLayout == false ) {
        innerPanel.setLayout( new GridLayout(1,numLabels) );
        hasLayout = true;
        // Add the panel into BorderLayout's north region
        panel.add( innerPanel, BorderLayout.NORTH ); 
      }
      // Add the label to innerPanel (grid within BorderLayout)
      innerPanel.add( label );
    } else {
      // Add label to BorderLayout's north region
      panel.add( label, BorderLayout.NORTH );
    }
    
    // Return the new Label
    return label;
    
  }
  
}

Test class if it's necessary:

import javax.swing.JFrame;

public class Preferences_TEST {
   public static void main( String args[] )
   { 
      Preferences app = new Preferences(); 
      app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      app.setSize( 450, 300 );
      app.setVisible( true );
   }
}

I can't figure out what the problem is... and it's been 22 days without a response from anyone else so I assume they can't either.

Did you end up solving the problem?

I can't figure out what the problem is... and it's been 22 days without a response from anyone else so I assume they can't either.

Did you end up solving the problem?

Thanks for the concern, I guess :D I ended up posting this somewhere else, forgot where exactly, and it was solved. I've changed the code so this answer doesn't match anymore, but nevertheless it was a stupid error: apparently I didn't put 'label = new JLabel( text )' anywhere. I did it here though so I still don't get why there's a problem with this version, but. Yeah. Here's the working code in case anyone has a similar problem:

private JPanel newLabel( String[] text ) {
    
    // Get the length of the array
    int numLabels = text.length;
    // Create an array of labels
    JLabel label[] = new JLabel[ numLabels ];
    
    // Create new JPanel to store all labels into
    // Give innerPanel rows depending on the number of labels
    JPanel innerPanel = new JPanel();
    innerPanel.setLayout( new GridLayout(1,numLabels) );
    
    // Set label text and
    // put labels into the grid
    for( int i = 0; i < numLabels; i++ ) {
      label[i] = new JLabel( text[i] );
      innerPanel.add( label[i] );
    }
    
    // Return the label(s)
    return innerPanel;
    
  }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.