Solved

Swing problem

Posted on 1998-06-23
4
186 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
Comment Utility


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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks !!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now