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,CaretListen er {
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",p 1);
p2.add(l2=new JLabel("",SwingConstants.C ENTER));
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(ActionEven t e) {
if (e.getSource()==b1 || e.getSource()==tf1) {
try {
con=DriverManager.getConne ction("jdb c:odbc:"+t f1.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.od bc.JdbcOdb cDriver");
DriverManager.setLogStream (null);
} catch(ClassNotFoundExcepti on cnfe) {
System.err.println("Class not found");
return;
}
window.setTitle("Java Application");
window.pack();
window.setVisible(true);
}
}
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,CaretListen
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",p
p2.add(l2=new JLabel("",SwingConstants.C
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(ActionEven
if (e.getSource()==b1 || e.getSource()==tf1) {
try {
con=DriverManager.getConne
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.od
DriverManager.setLogStream
} catch(ClassNotFoundExcepti
System.err.println("Class not found");
return;
}
window.setTitle("Java Application");
window.pack();
window.setVisible(true);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
Just go to the swing package source code in
src\com\java\swing\plaf\ba sic\BasicL abelUI.jav a and look for the method
getPreferredSize(JComponen t 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\SwingUt ilities.ja va 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.
Just go to the swing package source code in
src\com\java\swing\plaf\ba
getPreferredSize(JComponen
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\SwingUt
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.
ASKER
Thanks !!
ASKER