Java 在错误的文本周围画红色边框

Java 在错误的文本周围画红色边框,java,swt,eclipse-rcp,Java,Swt,Eclipse Rcp,我正在尝试实现一种方法,如果用户没有输入任何内容,它会在控件周围绘制一个红色边框,就像文本一样。我正在使用EclipseSWT。 我的方法如下所示: protected void drawRedBorder(Control cont){ final Control control = cont; cont.getParent().addPaintListener(new PaintListener(){ public void paintControl(Paint

我正在尝试实现一种方法,如果用户没有输入任何内容,它会在控件周围绘制一个红色边框,就像文本一样。我正在使用EclipseSWT。 我的方法如下所示:

protected void drawRedBorder(Control cont){
    final Control control = cont;
    cont.getParent().addPaintListener(new PaintListener(){
        public void paintControl(PaintEvent e){
            GC gc = e.gc;
            Color red = new Color(null, 255, 0 ,0);
            gc.setBackground(red);
            Rectangle rect = control.getBounds();
            Rectangle rect1 = new Rectangle(rect.x - 2, rect.y - 2,
                    rect.width + 4, rect.height + 4);
            gc.setLineStyle(SWT.LINE_SOLID);
            gc.fillRectangle(rect1);
        }
    });
}
当我在创建带有文本字段的对话框时调用它时,它工作得很好。但是,当我在checkInput()之类的方法中调用它时,它不起作用,该方法检查用户是否输入了内容

我试图通过调用redraw()或update()来解决这个问题,但没有任何效果。你知道我如何解决这个问题吗?

试试下面的方法

import org.eclipse.swt.*;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;

import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

public class Demo {

    public static void main (String [] args) {


        Display display = new Display ();
        Shell shell = new Shell (display);
        final Text txt;
        txt= new Text(shell, SWT.BORDER);
        drawRedBorder(txt);

        txt.addModifyListener(new ModifyListener() {

            @Override
            public void modifyText(ModifyEvent arg0) {
                // TODO Auto-generated method stub
                txt.getParent().redraw();

            }
        });


        shell.setLayout(new GridLayout());
        shell.open ();
        while (!shell.isDisposed ()) {
            if (!display.readAndDispatch ()) display.sleep ();
        }


        display.dispose ();
    }

    protected static void drawRedBorder(Control cont){
        final Control control = cont;

        cont.getParent().addPaintListener(new PaintListener(){
            public void paintControl(PaintEvent e){
               if(((Text) control).getText().length()<=0){
                    GC gc = e.gc;
                    Color red = new Color(null, 255, 0 ,0);
                    gc.setBackground(red);
                    Rectangle rect = control.getBounds();
                    Rectangle rect1 = new Rectangle(rect.x - 2, rect.y - 2,
                            rect.width + 4, rect.height + 4);
                    gc.setLineStyle(SWT.LINE_SOLID);
                    gc.fillRectangle(rect1);
               }
            }
        });
    }

} 
import org.eclipse.swt.*;
导入org.eclipse.swt.events.ModifyEvent;
导入org.eclipse.swt.events.ModifyListener;
导入org.eclipse.swt.events.PaintEvent;
导入org.eclipse.swt.events.PaintListener;
导入org.eclipse.swt.graphics.*;
导入org.eclipse.swt.layout.GridLayout;
导入org.eclipse.swt.widgets.*;
公开课演示{
公共静态void main(字符串[]args){
显示=新显示();
外壳=新外壳(显示);
最终文本txt;
txt=新文本(shell,SWT.BORDER);
drawRedBorder(txt);
addModifyListener(新ModifyListener(){
@凌驾
公共无效modifyText(ModifyEvent arg0){
//TODO自动生成的方法存根
txt.getParent().redraw();
}
});
setLayout(新的GridLayout());
shell.open();
而(!shell.isDisposed()){
如果(!display.readAndDispatch())display.sleep();
}
display.dispose();
}
受保护的静态无效drawRedBorder(控制续){
最终控制=控制;
cont.getParent().addPaintListener(新的PaintListener()){
公共无效油漆控制(油漆事件e){

if(((Text)control).getText().length()是否尝试了
updateUI()
?在哪里可以调用此方法?在调用
drawRedBorder
方法之后。我的意思是找不到名为updateUI()的方法。您使用的是
JFrame