How can I set only the width of a JPanel?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 29269 Views - Last Post: 19 September 2013 - 01:07 PM Rate Topic: -----

#1 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

How can I set only the width of a JPanel?

Posted 19 September 2013 - 08:42 AM

I am trying to make a toolbar for the program I am working on, with the toolbar always the same width. But when I have a user with a short name, the toolbar is small, and when I have a user with a long name, the toolbar is big. I think this shouldn't happen so i want to set the width of the toolbar without effecting the height.

User 1:


Posted Image

User 2:


Posted Image

The code for my toolbar is this:

import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

public class ToolBar extends JPanel
{
	private static final long serialVersionUID = 1L;
	
    public Color colour1;
    public Color colour2;
	
	public ToolBar(Color colour3, Color colour4)
	{
		setLocation(0, 0);
		setBorder(BorderFactory.createLineBorder(Color.black));
		
		colour1 = colour3;
		colour2 = colour4;
	}
    
	public ToolBar()
	{
		setLocation(0, 0);
		setBorder(BorderFactory.createLineBorder(Color.black));
		
        colour1 = getBackground();
        colour2 = colour1.darker();
	}
	
    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        int w = getWidth();
        int h = getHeight();
        GradientPaint gp = new GradientPaint(0, 0, colour1, 0, h, colour2);
        g2d.setPaint(gp);
        g2d.fillRect(0, 0, w, h);
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: How can I set only the width of a JPanel?

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 08:59 AM

Depends on your layout,which layout do you use in your Frame?
Was This Post Helpful? 0
  • +
  • -

#3 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:00 AM

borderlayout
Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:08 AM

Use method setPrefferedSize(new Dimension(width,weight));
Was This Post Helpful? 0
  • +
  • -

#5 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:09 AM

whats the weight?
Was This Post Helpful? 0
  • +
  • -

#6 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:11 AM

lol height*, only width will change ..you cant change height in BorderLayout at WEST position.

This post has been edited by ChrisNt: 19 September 2013 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#7 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:15 AM

Well how can I do that without changing the height? I have tried setPrefferedSize(new Dimension(150,getHeight())); but it just glitches out the panel... should I do this:

int height = this.getHeight();
setPrefferedSize(new Dimension(150,height)));

well that seems to work... Don't know why I didn't think of that earlier
Was This Post Helpful? 0
  • +
  • -

#8 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:39 AM

Is it just me or do those two bits of code look identical?
Was This Post Helpful? 1
  • +
  • -

#9 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:40 AM

what 2 bits
Was This Post Helpful? 0
  • +
  • -

#10 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:46 AM

setPrefferedSize(new Dimension(150,getHeight()));


and

int height = this.getHeight();
setPrefferedSize(new Dimension(150,height)));

Was This Post Helpful? 0
  • +
  • -

#11 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 09:47 AM

its just you
Was This Post Helpful? 0
  • +
  • -

#12 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 10:00 AM

I don't mean the same code character by character - but that they perform the exact same logic.
Was This Post Helpful? 1
  • +
  • -

#13 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 10:01 AM

setPrefferedSize(new Dimension(150,getHeight())); was glitching out my GUI when I tried it... So I did the next thing.
Was This Post Helpful? 0
  • +
  • -

#14 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 10:11 AM

Its not just him ,what you are saying makes no sense.
Was This Post Helpful? 1
  • +
  • -

#15 IntelligentTurtle   User is offline

  • D.I.C Head

Reputation: -4
  • View blog
  • Posts: 50
  • Joined: 25-July 13

Re: How can I set only the width of a JPanel?

Posted 19 September 2013 - 10:16 AM

ok can you please stop replying... getting bored of gmail telling me I have new mail
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2