Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Swing problem

Posted on 1998-06-23
4
193 Views
Last Modified: 2013-11-23
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);
  }
}































0
Comment
Question by:incah
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
evijay earned 100 total points
ID: 1223561


Please replace

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

with

  p2.add(l2=new JLabel("         ",SwingConstants.CENTER)); // some dummy spaces added !!


0
 

Author Comment

by:incah
ID: 1223562
I did it and worked, but before I grade your response, can you tell me why is this needed?
0
 
LVL 4

Expert Comment

by:evijay
ID: 1223563
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.


0
 

Author Comment

by:incah
ID: 1223564
Thanks !!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jdbc error in jsp application 20 63
eclipse apache tomcat admin console 52 121
How to configure empty element in XML Document parser? 15 36
Java class and jar 3 20
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question