Link to home
Start Free TrialLog in
Avatar of incah
incah

asked on

Swing problem

The code below adds two panels in the main window. Can somebody tell me why the second (lowest) panel is not immediately visible but I have to manually resize the main window to see it?

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import com.sun.java.swing.*;
import com.sun.java.swing.event.*;

public class SwingApp extends JFrame implements ActionListener,CaretListener {
  JPanel p1,p2;
  JLabel l1,l2;
  JButton b1,b2;
  JTextField tf1,tf2;
  Connection con;
  public SwingApp() {
    Container contentPane = getContentPane();
    p1=new JPanel();
    p2=new JPanel(new GridLayout(0,1));

    p1.add(l1=new JLabel("Database Name",SwingConstants.LEFT));
    p1.add(tf1=new JTextField(50));
    p1.add(b1=new JButton("Open"));
    p1.add(b2=new JButton("Close"));
    contentPane.add("Center",p1);

    p2.add(l2=new JLabel("",SwingConstants.CENTER));
    contentPane.add("South",p2);

    b1.setEnabled(false);
    b2.setEnabled(false);
    tf1.addCaretListener(this);
    tf1.addActionListener(this);
    b1.addActionListener(this);
    b2.addActionListener(this);
    addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);
      }
    });
  }

  public void actionPerformed(ActionEvent e) {
      if (e.getSource()==b1 || e.getSource()==tf1) {
      try {
         con=DriverManager.getConnection("jdbc:odbc:"+tf1.getText());
         l2.setText("SUCCESS!");
         tf1.setEnabled(false);
         b1.setEnabled(false);
         b2.setEnabled(true);
      } catch(SQLException sqle) {
        l2.setText("Could not open: "+tf1.getText());
        return;
      }
      } else if (e.getSource()==b2) {
      try {
        con.close();
      } catch(SQLException sqle) {
      }
      con=null;
      tf1.setEnabled(true);
      b1.setEnabled(true);
      b2.setEnabled(false);
      l2.setText("");
      }
  }

  public void caretUpdate(CaretEvent e) {
    if (tf1.getText().equals(""))
      b1.setEnabled(false);
    else
      b1.setEnabled(true);
  }

  public static void main(String[] args) {
    SwingApp window = new SwingApp();

    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      DriverManager.setLogStream(null);
    } catch(ClassNotFoundException cnfe) {
      System.err.println("Class not found");
      return;
    }

    window.setTitle("Java Application");
    window.pack();
    window.setVisible(true);
  }
}































ASKER CERTIFIED SOLUTION
Avatar of evijay
evijay

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of incah
incah

ASKER

I did it and worked, but before I grade your response, can you tell me why is this needed?
Hi,

Just go to the swing package source code in

src\com\java\swing\plaf\basic\BasicLabelUI.java and look for the method
getPreferredSize(JComponent c)

This is the method that get invoked when the container wants to find the size of the label.
This method inturn delegates the size calculation to src\com\java\swing\SwingUtilities.java method layoutCompoundLabel.
If you go into this method the interesting parameter is the textR parameter which returns the size of the rectangle the text takes. in this method, textIsEmpty is a boolean variable which is set to true if text is null or empty. This setting causes it to return a rectange of 0 width and size for the text component and this is the reason why getPreferredSize returns incorrect size. In fact, this is documented in the comments of layoutCompoundLabel method.


Avatar of incah

ASKER

Thanks !!