开发者

Set size of a JLabel inside JToolbar

i have some troubles setting the size of a JLabel component inserted into a JToolbar. I use the JLabel to display actual mouse coordinates, but depending on the lenght of the number JLabel change it's size producing some nasty movements of my JToolbar components.

I'm playing around with setSize, setMinimumSize, etc to overcome this problem but still no results. I know that the behavior of this methods change accordingly to the layout manager.

Which is the correct method to define a fixed minimum dimension of a JLabel into a JToolbar?

Here's my code :

public class EditorStatusBar extends JToolBar{

private JLabel areaCoordX;
private JLabel areaCoordY;

public EditorStatusBar(){
    super();

    addSeparator(new Dimension(100,this.getSize().height));

    this.areaCoordX = new JLabel("    ");
    this开发者_StackOverflow.areaCoordX.setMinimumSize(new Dimension(80,10));
    this.areaCoordX.setPreferredSize(new Dimension(80,10));
    this.areaCoordX.setHorizontalAlignment(JLabel.RIGHT);
    this.areaCoordX.setOpaque(true);

    this.areaCoordY = new JLabel("");
            this.add(areaCoordX);
    this.add(new JLabel(":"));
    this.add(areaCoordY);

     }
}

public void setCoordOfComponent(Point c){
        this.areaCoordX.setText(""+c.x);
        this.areaCoordY.setText(""+c.y);
    }
}

public class Gui extends JFrame implements ActionListener, ItemListener,ChangeListener{
private EditorStatusBar statusBar;
public static void main(String[] args) {
    new Gui();
}
private void buildStatusBar(){

    statusBar = new EditorStatusBar();
    statusBar.setFloatable(false);
    statusBar.setMaximumSize(new Dimension(2000, 20));
}
public Gui() {

super();
getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
buildStatusBar();
this.getContentPane().add(statusBar);

}}

When i'm updating the value with setCoordOfComponent() it works if i set both minimum and preferred size , but when the mouse is for example over the JMenu, JPanel size decrease.


I wasn't able to get it working with a JLabel, but if you're willing to use a JTextField instead, you can create those with a fixed width (number of columns). Here's my working example with buttons on either side to show that the toolbar doesn't resize.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JToolBar;

public class EditorStatusBar extends JToolBar {
    private final JTextField coords;

    public EditorStatusBar(Component parent) {
        super();

        setFloatable(false);

        add(new JButton("Button 1"));
        addSeparator();

        coords = new JTextField(5);
        coords.setEditable(false);
        coords.setHorizontalAlignment(JTextField.CENTER);
        add(coords);

        addSeparator();
        add(new JButton("Button 2"));

        parent.addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                coords.setText(p.x + ":" + p.y);
            }
        });
    }
}

class Gui extends JFrame {
    public static void main(String[] args) {
        Gui gui = new Gui();
        gui.setVisible(true);
    }

    public Gui() {
        super();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());
        add(new EditorStatusBar(this), BorderLayout.NORTH);
        add(new JLabel("Content", JLabel.CENTER), BorderLayout.CENTER);
        pack();
    }
}


I could do it with the following code. To fix the size of the component, I had to use each of setPreferredSize, setMaximumSize and setMinimumSize.

/**
 * Insert a component in the toolbar and specify its width.
 *
 * @param component
 */
protected void addToolbarItem(JLabel component, int width)
{
    addToolbarItem(component);
    final Dimension dim = new Dimension(width, toolBar.getHeight());
    component.setPreferredSize(dim);
    component.setMinimumSize(dim);
    component.setMaximumSize(dim);
    component.setBorder(new LineBorder(Color.BLACK));
}


Your code is fine, but you forgot to add the components to the toolbar.

add these lines in the toolbar constructor

add(areaCoordX);
add(areaCoordY);

It should look like this now

public class EditorStatusBar extends JToolBar{

private JLabel areaCoordX;
private JLabel areaCoordY;

public EditorStatusBar(){
    super();

    addSeparator(new Dimension(100,this.getSize().height));

    this.areaCoordX = new JLabel("    ");
    this.areaCoordX.setMinimumSize(new Dimension(80,10));
    this.areaCoordX.setPreferredSize(new Dimension(80,10));
    this.areaCoordX.setHorizontalAlignment(JLabel.RIGHT);
    this.areaCoordX.setOpaque(true);
    this.areaCoordY = new JLabel("");
    add(areaCoordX);
    add(areaCoordY);
     }
public void setCoordOfComponent(Point c){
        this.areaCoordX.setText(""+c.x);
        this.areaCoordY.setText(""+c.y);
    }
}

I suggest to add some text in the label to see them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜