Sign in to follow this  
cptrnet

[java] JComboBox Problem

Recommended Posts

Hi, I have a problem with JComboBox. I have an applet where I display the JComboBox with a layout of null, and the location at a fixed point with setBounds. The arrow to the right is being cut off though and it looks like a normal text box, you can still click on it and selected things but the look is the problem. I am very new to java so I don't know why this is happening, any help will be appreciated, thanks.

Share this post


Link to post
Share on other sites
It sounds like you don't have enough space in your underlying container to hold the JComboBox fully. You could try calling the setPreferredSize(Dimension d) method of either the combo box or the container which holds it (possibly the applet itself) in order to force it to fit. If that doesn't work then your code would be useful in finding the problem

Share this post


Link to post
Share on other sites
This is a JPanel that I add to the JApplet, its an options menu where I display the JComboBox.


import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.FlowLayout;

public class Options extends JPanel implements MouseListener{

private static final long serialVersionUID = 1L;

private JLabel lblWhoStarts;
private JComboBox cmbWhoStarts;

private JLabel lblRounds;
private JTextField txtRounds;

private JLabel lblDifficulty;
private JComboBox cmbDifficulty;

private JLabel lblBack;

TicTacToe mainApplet;
MainPanel mainPanel;

public Options(TicTacToe applet, MainPanel panel){

mainApplet = applet;
mainPanel = panel;

setLayout(new FlowLayout());
setSize(500, 300);
setBackground(Color.WHITE);

Insets insets = getInsets();

lblWhoStarts = new JLabel("Who starts first?");
lblWhoStarts.setBounds(200 + insets.left, 50 + insets.top, 150, 25);
add(lblWhoStarts);

String[] cmbString = {"Player One", "Player Two"};

cmbWhoStarts = new JComboBox(cmbString);
cmbWhoStarts.setSize(100, 25);
cmbWhoStarts.setPreferredSize(new Dimension(100,25));
cmbWhoStarts.setBounds(150, 75, 200, 25);
add(cmbWhoStarts);

lblRounds = new JLabel("How many rounds?");
lblRounds.setBounds(195 + insets.left, 110 + insets.top, 150, 25);
add(lblRounds);

txtRounds = new JTextField("1");
txtRounds.setBounds(150 + insets.left, 135 + insets.top, 200, 25);
add(txtRounds);

lblDifficulty = new JLabel("Computer Difficulty.");
lblDifficulty.setBounds(195 + insets.left, 175 + insets.top, 150, 25);
add(lblDifficulty);

String[] cmbDiff = {"Easy", "Medium", "Hard"};

cmbDifficulty = new JComboBox(cmbDiff);
cmbDifficulty.setBounds(150 + insets.left, 200 + insets.top, 200, 25);
add(cmbDifficulty);

lblBack = new JLabel("Back");
lblBack.setBounds(235 + insets.left, 250 + insets.top, 30, 15);
lblBack.addMouseListener(this);
add(lblBack);

}

public void mouseClicked(MouseEvent e)
{
if(e.getComponent() == lblBack){
mainApplet.remove(this);
mainApplet.add(mainPanel);
mainApplet.repaint();
}
}
public void mouseExited(MouseEvent e){
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void mouseEntered(MouseEvent e){
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}

}


Share this post


Link to post
Share on other sites
I did some testing. I made another applet with combo boxes and It works. The thing that I saw is that in this one I added the combo's in the init method. My tictactoe game I add them when I click something. So I was looking around the documentation and came across updateUI(); it Resets the UI property with a value from the current look and feel. And it worked, it wasn't that is was too small or size or anything, it was loosing its look and feel.

Share this post


Link to post
Share on other sites
If you are dynamically adding components to the applet that need to be drawn (or redrawn) you have to remember to call validate() or repaint(). Failure to do this might sometimes cause your applet or application to appear as if its crashing or hanging.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this