Swing Eigene JComponent Scrollable

Animal21

Mitglied
Hallo Leute,

ich hab mir eine Component gebastelt, geerbt von JComponent, auf der ich zeiche und stuff mache...

Nun möchte ich diese Komponente noch Scrollable haben, leider geht das mit JScrollPane(myComponent); nicht wie es soll (sprich garnicht, da meine gezeichneten sachen nicht angezeigt werden.)

Was muss ich tun, damit ich meine Komponente Scrollable bekomme?

gruß
ani
 
Hallo,

ja habe ich, eigentlich soll die Größe immer dynamisch an die Hauptfenstergröße angepasst werden, aber selbst dies habe ich zum testen auf eine feste größe gesetzt.

Mein Problem ist nun, dass GARNICHTS gezeichnet wird.

gruß
mario
 
ist schlecht zu machen...

Java:
public abstract class gl_AbstControl extends JComponent{
	public gl_AbstControl(){
	}
}

public class gl_Card extends gl_AbstControl implements HierarchyBoundsListener, DropTargetListener, 
														MouseListener, MouseMotionListener, Scrollable{
	public enum UpdateAction { New, Add, Remove };
	public class gl_CardButton extends gl_AbstControl{
                 //...
	}
	
	//Fields
	private String title;
	private List<Student> students;
	private List<gl_CardButton> buttons;
	private DropTarget dropTarget;
	private DataFlavor dataFlavor;
	
	private Rectangle rectAddButton;
 	private FontMetrics fm;
	private boolean bHasAddButton;
	private boolean bMouseInAddButton;
	private Color cAddButtonColor;
	private Color cBorderColor;

	protected GL_RequestEventMulticaster multicaster;
	
	//Properties
	public boolean HasAddButton(){ return bHasAddButton; }
	public void SetHasAddButton(boolean hasAddButton){ this.bHasAddButton = hasAddButton; repaint(); }
	
	//Constructor
	public gl_Card(String title){
		super();
		
		this.setFont(new Font("Myriad Pro", Font.BOLD, 16));
		fm = getFontMetrics(this.getFont());
		this.title = title;
		buttons = new ArrayList<gl_CardButton>();
		this.setBackground(new Color(255, 255, 255, 0));
		this.cBorderColor = Color.white;
		this.bHasAddButton = false;
		this.cAddButtonColor = Color.white;
		
		rectAddButton = new Rectangle(this.getX() + this.getWidth() - (fm.getHeight() + 4), 4, 
				fm.getHeight() - 4, fm.getHeight() - 4);
		
		dropTarget = new DropTarget(this, DnDConstants.ACTION_COPY_OR_MOVE,
		        this, true, null);
		
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.addHierarchyBoundsListener(this);
	}
	
	//Methods
	//Private
	private void calcStuff(){
		if(students != null && buttons != null){
			for(int i=0; i<buttons.size(); i++){
				buttons.get(i).setSize(this.getWidth() - 10, 20);
				buttons.get(i).setLocation(5, 30 + i * 25);
			}		
		}
	}
	
	//Handler
	@Override
	public void paint(Graphics g){
		super.paint(g);
	
		//Border
		g.setColor(cBorderColor);
		g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 25, 25);

		//Background
		g.setColor(new Color(135, 197, 255));
		g.fillRoundRect(g.getClipBounds().x + 2, g.getClipBounds().y + 2, g.getClipBounds().width - 4, g.getClipBounds().height - 4, 25, 25);

		//Text
		g.setColor(Color.white);
		g.setFont(this.getFont());
		g.drawString(this.title, (int)((g.getClipBounds().getWidth() - fm.stringWidth(title))/2), 18);

		//Seperator
		g.fillRect(0, fm.getHeight() + 2, (int)g.getClipBounds().getWidth(), 3);
		
		//AddButton
		if(bHasAddButton){
			g.setColor(cAddButtonColor);
			Rectangle r = rectAddButton = new Rectangle(g.getClipBounds().x + g.getClipBounds().width - (fm.getHeight() + 4), 4, 
					fm.getHeight() - 4, fm.getHeight() - 4);
			g.drawRoundRect(r.x, r.y, r.width, r.height, 
							fm.getHeight() - 4, fm.getHeight() - 4);
			g.drawLine( r.x + (int)(r.width / 2) + 1, 
					r.y + 3, 
					r.x + (int)(r.width / 2) + 1, 
					r.y + r.height - 3);
			g.drawLine( r.x + 3, 
					r.y + (int)(r.height / 2), 
					r.x + r.width - 3, 
					r.y + (int)(r.height / 2));
		}
		
		super.paintComponents(g);
	}
	@Override
	public Dimension getPreferredScrollableViewportSize() {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public int getScrollableBlockIncrement(Rectangle arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public boolean getScrollableTracksViewportHeight() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public boolean getScrollableTracksViewportWidth() {
		// TODO Auto-generated method stub
		return false;
	}
	@Override
	public int getScrollableUnitIncrement(Rectangle arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
		return 0;
	}
}

ist jetzt nicht alles, aber vll reicht es.

später wird es wie folgt aufgerufen:
Java:
		studentsCard = new gl_Card("Studenten");
		studentsCard.SetHasAddButton(true);
		studentsCard.Update(dataManager.GetAllStudents(), gl_Card.UpdateAction.New);
		studentsCard.Add_GL_ActionListener(this);
		studentsCard.setSize(150, 200);
		studentsCard.setPreferredSize(new Dimension(150, 200));
                //...
                add(new JScrollPane(studentsCard));

gruß
ani
 
Bei
Code:
getPreferredScrollableViewportSize()
null zurück zu geben ist wohl nicht so geschickt ;-)
In Deinem Fall würde ich gar nicht das Interface Scrollable implementieren.
Es reicht einfach aus die PreferredSize zu setzen und ggf. zu aktualisieren, damit die Komponent in einer JScrollPane im Fall von (PreferredSize < JScrollPane) scrollbar ist.
 

Neue Themen


Zurück
Oben