Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8007825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:57:07+00:00 2026-06-04T17:57:07+00:00

I’m new to SWT and currently struggling to make a Label instance visible. At

  • 0

I’m new to SWT and currently struggling to make a Label instance visible. At the moment, I need to resize the window manually to make it appear. I found a similar question on SO that mentions the cache of SWT. So I tried the suggested call of shell.layout(); but it does not work for me. Here is my code:

public class SimpleTextEditorCount {
private Display display = new Display();
private Shell shell = new Shell(this.display);
private StyledText styledText;
private boolean unsaved;
private File file;
private String lastDirectory;
private Label status;

public SimpleTextEditorCount() {
    this.shell.setLayout(new GridLayout());
    this.createMenuBar();
    this.styledText = new StyledText(this.shell, SWT.MULTI | SWT.WRAP
            | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    this.styledText.setLayoutData(new GridData(GridData.FILL_BOTH));
    final Font font = new Font(this.shell.getDisplay(), "Book Antiqua", 12,
            SWT.NORMAL);
    this.styledText.setFont(font);
    this.styledText.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent e) {
            unsaved = true;
        }
    });
    this.shell.setText("Editor");
    this.shell.setSize(400, 300);
    this.createCount();
    this.shell.open();
    while (!this.shell.isDisposed()) {
        if (!this.display.readAndDispatch()) {
            this.display.sleep();
        }
    }
    this.display.dispose();
}

private void createMenuBar() { 
    // Create the File top-level menu
    Menu menu = new Menu(this.shell, SWT.BAR);      
    MenuItem open = new MenuItem(menu, SWT.CASCADE);
    open.setText("File");

    Menu dropMenu = new Menu(this.shell, SWT.DROP_DOWN);
    open.setMenu(dropMenu); 

    // File->Open
    open = new MenuItem(dropMenu, SWT.NULL);
    open.setText("Open...\tCtrl+O");
    open.setAccelerator(SWT.CTRL + 'O');
    open.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            loadText();
        }
    });

    // File->Save
    MenuItem save = new MenuItem(dropMenu, SWT.NULL);
    save.setText("Save\tCtrl+S");
    save.setAccelerator(SWT.CTRL + 'S');
    save.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            saveText();
        }
    });

    // File->Save As
    MenuItem saveAs = new MenuItem(dropMenu, SWT.NULL);
    saveAs.setText("Save As...");
    saveAs.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });

    // File->Exit
    MenuItem exit = new MenuItem(dropMenu, SWT.SEPARATOR); 
    exit = new MenuItem(dropMenu, SWT.NULL);
    exit.setText("Exit\tAlt+F4");
    exit.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            if (saveChanges()) {
                shell.dispose();
            }
        }
    });

    // Create Edit
    MenuItem edit = new MenuItem(menu, SWT.CASCADE);
    edit.setText("Edit");
    dropMenu = new Menu(shell, SWT.DROP_DOWN);
    edit.setMenu(dropMenu); // Create Edit->Cut
    edit = new MenuItem(dropMenu, SWT.NULL);
    edit.setText("Cut\tCtrl+X");
    edit.setAccelerator(SWT.CTRL + 'X');
    edit.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            styledText.cut();
        }
    });

    // Create Edit->Copy
    MenuItem copy = new MenuItem(dropMenu, SWT.NULL);
    copy.setText("Copy\tCtrl+C");
    copy.setAccelerator(SWT.CTRL + 'C');
    copy.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            styledText.copy();
        }
    });

    // Create Edit->Paste
    MenuItem paste = new MenuItem(dropMenu, SWT.NULL);
    paste.setText("Paste\tCtrl+V");
    paste.setAccelerator(SWT.CTRL + 'V');
    paste.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            styledText.paste();
        }
    });

    // Create Select All
    MenuItem selectAll = new MenuItem(dropMenu, SWT.SEPARATOR); 
    selectAll = new MenuItem(dropMenu, SWT.NULL);
    selectAll.setText("Select All\tCtrl+A");
    selectAll.setAccelerator(SWT.CTRL + 'A');
    selectAll.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });

    MenuItem undo = new MenuItem(dropMenu, SWT.SEPARATOR); // Create Undo
    undo = new MenuItem(dropMenu, SWT.NULL);
    undo.setText("Undo\tCtrl+Z");
    undo.setAccelerator(SWT.CTRL + 'Z');
    undo.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });

    // Create Help
    MenuItem help = new MenuItem(menu, SWT.CASCADE);
    help.setText("Help");
    dropMenu = new Menu(shell, SWT.DROP_DOWN);
    help.setMenu(dropMenu); // Create Help->About
    help = new MenuItem(dropMenu, SWT.NULL);
    help.setText("About");
    help.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent event) {
            System.err.println("NEEDS TO BE IMPLEMENTED");
        }
    });
    this.shell.setMenuBar(menu);
}

public void createCount() {
    this.status = new Label(this.shell, SWT.BORDER);
    this.status.setText("test");
    this.status.setLayoutData(new GridData(GridData.FILL,
            GridData.BEGINNING, true, false, 2, 1));
    this.shell.layout();
    styledText.addModifyListener(new ModifyListener() {
        @Override
        public void modifyText(ModifyEvent arg0) {
            updateStatus();
        }
    });
    styledText.addKeyListener(new KeyListener() {
        @Override
        public void keyReleased(KeyEvent arg0) {
        }

        @Override
        public void keyPressed(KeyEvent arg0) {
            updateStatus();
        }
    });
}

private void updateStatus() {
    StringBuffer buf = new StringBuffer();
    buf.append("Offset: ");
    buf.append(this.styledText.getCaretOffset());
    buf.append("\tChars: ");
    buf.append(this.styledText.getCharCount());
    buf.append("\tLine: ");
    buf.append(this.styledText.getLineAtOffset(this.styledText
            .getCaretOffset()) + 1);
    buf.append(" of ");
    buf.append(this.styledText.getLineCount());
    this.status.setText(buf.toString());
}

boolean saveChanges() {
    if (!this.unsaved) {
        return true;
    }
    final MessageBox box = new MessageBox(this.shell, SWT.ICON_WARNING
            | SWT.YES | SWT.NO | SWT.CANCEL);
    box.setMessage("save changes? ");
    box.setText("Editor");
    final int condition = box.open();
    if (condition == SWT.YES) {
        return this.saveText();
    } else if (condition == SWT.NO) {
        return true;
    } else {
        return false;
    }
}

boolean loadText() {
    final FileDialog dialog = new FileDialog(this.shell, SWT.OPEN);
    if (this.lastDirectory != null) {
        dialog.setFilterPath(this.lastDirectory);
    }
    final String selectedFile = dialog.open();
    if (selectedFile == null) {
        System.out.println("File is not opened");
        return false;
    }
    this.file = new File(selectedFile);
    this.lastDirectory = this.file.getParent();

    try {
        final BufferedReader reader = new BufferedReader(new FileReader(
                this.file));
        final StringBuffer buffer = new StringBuffer();
        String line = null;
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
            buffer.append("\r\n");
        }
        this.styledText.setText(buffer.toString());
        return true;
    } catch (final IOException e) {
    }
    return false;
}

boolean saveText() {
    if (this.file == null) {
        final FileDialog fileDialog = new FileDialog(this.shell, SWT.SAVE);
        if (this.lastDirectory != null) {
            fileDialog.setFilterPath(this.lastDirectory);
        }
        final String selectedFile = fileDialog.open();
        if (selectedFile == null) {
            System.out.println("File is not saved");
            return false;
        }
        this.file = new File(selectedFile);
        this.lastDirectory = this.file.getParent();
    }
    try {
        final FileWriter writer = new FileWriter(this.file);
        writer.write(this.styledText.getText());
        writer.close();
        this.unsaved = false;
        return true;
    } catch (final IOException e) {
    }
    return false;
}

public static void main(String[] args) {
    new SimpleTextEditorCount();
}

}

Do you have any suggestions? Thank you!

Edit: I replaced the code snippet with the entire file. I figured out that the strange behavior seems to be triggered by the menu bar. If I comment out this.createMenuBar(); creation in the constructor, the label is visualized properly (created by the createCount() method).

Edit: I wasn’t able to find a right solution. So I added a pack() call after the windows has been opened to enforced the resize. Here are my changes that I made to the constructor:

//... code 
this.shell.setSize(400, 300);
this.shell.open();
this.shell.pack();
this.shell.setSize(400, 300);
while (!this.shell.isDisposed()) {
    if (!this.display.readAndDispatch()) {
        this.display.sleep();
    }
}
// ... further code

I made to screen shots without an with the added pack and resize call.The Label is not visible.

and with the added two lines:

enter image description here

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:57:10+00:00Added an answer on June 4, 2026 at 5:57 pm

    I don’t think this is necessarily related to cache. Check the following:

    1. setting correct layout manager on shell
    2. setting wrong GridData on widgets.
    3. You’re not being very specific. Assuming you construct your layout, open your shell and the label is not visible, it might occur that you have set a too small window size. Maybe a simple call of shell.pack() solves the problem?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I am currently running into a problem where an element is coming back from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.