0

I have designed a JFrame in Netbeans as shown in the image below:

http://tinypic.com/r/2em2wxd/8

When I run the program, the combo box goes all white as shown in the image below:

http://tinypic.com/r/2vblwuf/8

And.. When I click the "white" space it shows the Item(s) inside the combo box which can be selected.. but even after selecting, it is still the same white space not showing the layout properly and the selected item.

My code is as following:

public class LoginWindow extends javax.swing.JFrame {

/**
 * Creates new form LoginWindow
 */
public LoginWindow() {
    initComponents();
    setLocationRelativeTo(null);

    new MyWorker().execute();
}
..........

And the Worker class is as following:

    class MyWorker extends javax.swing.SwingWorker<String, Void> {

    protected String doInBackground() {
        databaseCheck();
        return "done";
    }

    protected void done() {
        progressPanel.setVisible(false);
        loginPasswordField.setEnabled(true);
        locationComboBox.setEnabled(true);
        loginButton.setEnabled(true);
        loginPasswordField.requestFocusInWindow();
    }
}

It must be noted that this behaviour is mostly experienced, although sometimes it is working just fine (very rarely)... Any help would be appreciated.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
HMH
  • 33
  • 6
  • 3
    Can you post an [SSCCE](http://sscce.org) ? – joey rohan Sep 02 '14 at 18:29
  • 2
    SSCCE is also known as [MCVE](http://stackoverflow.com/help/mcve) and it reffers to a Minimal Example (the less code you can provide), but that still reproduce the error, it must be a complete code, so we can copy-paste it and see it on our computers and be more able to help you. – Frakcool Sep 02 '14 at 18:32

1 Answers1

2

I just seemed to have fixed it while trying to post an SSCCE..

The actual component size visible on the frame was [256, 29].. whereas the preferredSize was different which seemed to have caused the problem.

    locationComboBox.setPreferredSize(new java.awt.Dimension(256, 29));

This fixed the problem.

HMH
  • 33
  • 6
  • 3
    I would recommend you to read [this question about using set vs get Minimum/Preferred/Maximum sizes](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi/7229519#7229519) too and answers for `setMinimumSize` and `setPreferredSize`. And of course, that's why we ask for an MCVE, when trying to solve anything if you bring it to basics then sometimes you get to the answer by your own. Think of it next time before asking :) – Frakcool Sep 02 '14 at 18:57
  • Oh and don't forget to check your answer as the accepted one (when system allows you to, which if I'm not wrong will be in 2 days). And just in case you're using multiple `JFrames` you should also read [The use of multiple JFrames, Good / Bad Practice](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-bad-practice/9554657#9554657) – Frakcool Sep 02 '14 at 18:58
  • Thank you for the link. I will definitely investigate more into the layout managers and implementing it in my application as I have noticed the components having size issues across different platforms.. But I wonder why this would be an issue at first place if the GUI builder shows the component as I make it and when the application is run, it can cause unexpected problems/ self-resizing according to the default minimumSize/preferredSize values which I didn't set at first place.. And haha! yes I used to use multiple JFrames but now I have changed all my application after reading that link :P – HMH Sep 02 '14 at 19:06
  • yes Java Swing is designed to use [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html), instead of using multiple `JFrames` you can use a [CardLayout](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html). Hope it helps and I didn't understood what you meant `But I wonder why this would be an issue at first place if the GUI builder shows the component as I make it and when the application is run` that part and until "And haha!" but I can't copy-paste it all because of char limit. Can you explain so maybe I can give you another advice? – Frakcool Sep 02 '14 at 19:13
  • I meant when you are designing your form in Netbeans, you drag-drop the component onto the form and resize it as you want expecting it to show as is when running application. But, just like in my case above, the combo box was having "size" issues as far as I understood.. and me purposely setting the preferredSize solved the issue.. so why does this issue arise at first place even though you design the components and set their sizes while designing. – HMH Sep 02 '14 at 19:20
  • 1
    not sure, maybe someone else can answer that question. But IMHO there's nothing better than doing it w/o an IDE. – Frakcool Sep 02 '14 at 19:42