Solved

Swing problem

Posted on 1998-06-23
4
192 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
replace a word with other 1 44
java stored proc example 9 32
type mismatch (Object[] to double[] 4 25
Which non-HTML GUI front end to use with Java? 3 22
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

832 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