Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to save a text box to a text file in SWT?

Posted on 2013-06-24
15
Medium Priority
?
916 Views
Last Modified: 2013-08-07
I have a GUI and in this GUI I have a text box. After typing something in the textbox, I would like to save these characters in the text box to a text file by clicking a button in the toolbar.

How can I do it in SWT?

Please see this question for more information on how I create the text box:

http://www.experts-exchange.com/Programming/Languages/Java/Q_28166271.html
0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 7

Expert Comment

by:gudii9
ID: 39273575
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39273605
As I said in the other question, once you make "txt" accessible from elsewhere in you code, then you can get the text simply by calling txt.getText() and so the code to write this to a text file could be as simple as...
PrintWriter writer = new PrintWriter("textfile.txt");
writer.println(txt.getText());
writer.close();

Open in new window

0
 
LVL 16

Accepted Solution

by:
Valeri earned 2000 total points
ID: 39301766
take a look at the attached class. it has two methods "writeToFile" and "readFromFile"
reading is implemented line by line in case you want to process the content for some reason.
Add instance of the attached file as member in your Gui.java class (file is in "/psd" folder, you have to create it manually):
static RWFile rwFile = new RWFile();

1. add to the respective ToolItem's (itemRead & itemWrite) for reading and writing listeners:
itemRead.addListener(SWT.Selection, toolBarListener);
itemWrite.addListener(SWT.Selection, toolBarListener);
2. add this two methods to the Gui class:
public static void readTxt() {
  txt.setText(rwFile.readFromFile());
}

public static void writeTxt() {
  rwFile.writeToFile(txt.getText());
}

3. in ToolBarListener class insert these lines:
if (toolItem == Gui.itemRead) {
  Gui.readTxt();
} else if (toolItem == Gui.itemWrite) {
  Gui.writeTxt();
}
RWFile.java
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Tolgar
ID: 39331014
Where should I create psd folder?
0
 

Author Comment

by:Tolgar
ID: 39331059
Hi Valeri,
Also, I did this in the Gui.java file:

private static CTabItem txt;

Is this correct?
0
 
LVL 16

Expert Comment

by:Valeri
ID: 39364696
"private static CTabItem txt;"  - no, it's not correct, it should be of type Text:
static Text txt;

psd folder should be subfolder of the root.
0
 

Author Comment

by:Tolgar
ID: 39364891
I did all these steps except the psd folder. I created this psd folder manually but I am not sure how I will put RWFile.java file in this folder.

If I create a class, it does not go under psd folder because I need to select a package. If I put the RWFile.java file as a file in that folder then it Gui.java does not see this file.

Can you please help?
0
 

Author Comment

by:Tolgar
ID: 39364977
So, this is my directory structure in the attachment. I have the RWFile.java in the package. The swtFile.txt is in the psd folder.

But I am getting this error:

readFromFile problem:
java.io.FileNotFoundException: \psd\swtFile.txt (The system cannot find the file specified)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:120)
	at java.io.FileInputStream.<init>(FileInputStream.java:79)
	at java.io.FileReader.<init>(FileReader.java:41)

Open in new window

directoryTree.png
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39365567
Try removing the leading slash from that path, so...

private static final String fileName = "/psd/swtFile.txt";

should be...

private static final String fileName = "psd/swtFile.txt";
0
 

Author Comment

by:Tolgar
ID: 39365752
I tried it and I got rid of one of the errors. But I still get this error:

Exception in thread "main" java.lang.NullPointerException
	at com.a.b.c.Gui.readTxt(Gui.java:61)
	at com.a.b.c.ToolBarListener.handleEvent(ToolBarListener.java:41)
	at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
	at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
	at com.a.b.c.Gui.main(Gui.java:390)

Open in new window


This is what I have in those lines:

Gui.java:390

while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {      //LINE 390
                display.sleep();
            }
        }

Open in new window



ToolBarListener.java:41

  } else if (toolItem == Gui.item111) {
              Gui.readTxt();                   // LINE 41

Open in new window



Gui.java:61

    public static void readTxt() {
        txt.setText(rwFile.readFromFile());   // LINE 61
    }

Open in new window


Do you have any idea why I am getting this exception?
0
 
LVL 36

Expert Comment

by:mccarl
ID: 39365940
Do you have any idea why I am getting this exception?
Because at that point in your code (Gui.java : 61) either "txt" or "rwFile" are null. How are you declaring and initialising those variables?
0
 

Author Comment

by:Tolgar
ID: 39367076
@mcccarl: This is how I declare them:

    static Text txt;
    static RWFile rwFile = new RWFile();

Open in new window

0
 
LVL 36

Expert Comment

by:mccarl
ID: 39368717
Ok then, so I would say that most likely the NullPointerException is due to the "txt" variable being null (since rwFile is immediately intialised).

Where do you assign the variable "txt" a value? And does that ALWAYS happen before the user can click the toolbar button to read from the file into the Text field? (Perhaps if you can post the whole code, it might be quicker for us to debug rather than getting in tiny snippets)
0
 

Author Comment

by:Tolgar
ID: 39370387
I see.

This is the GUI.java file:


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;


public class Gui {
    
    static ToolItem item1;
    static ToolItem item11;
    static ToolItem item111;
    static ToolItem item1111;
    static ToolItem item12;
    static ToolItem item13;
    static ToolItem item23;
    static CTabFolder folder;
    static ToolItem item2;
    static ToolItem item22;
    static ToolItem item222;
    static ToolItem item3;
    static ToolItem item33;
    static ToolItem item333;
    static ToolItem item3333;
    static ToolItem item33333;
    static ToolItem item333333;
    static ToolItem item3333333;
    static ToolItem item33333333;
    static Text txt;

    
    private static ToolBarListener toolBarListener = new ToolBarListener();
    static RWFile rwFile = new RWFile();
    
    
    public static void swichTabChecks() {
        folder.setSelection(1);
    }
    public static void swichTabResults() {
        folder.setSelection(2);
    }
    public static void swichTabFiles() {
        folder.setSelection(0);
    }
    
    public static void readTxt() {
        txt.setText(rwFile.readFromFile());
    }
    
    public static void writeTxt() {
        rwFile.writeToFile(txt.getText());
    }
    
    public static void clearTxt() {
        txt.setText("");
    } 
    
    public static void selectFileFolders() {
        
    } 
    
    

    
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        
        toolBarListener.setShell(shell,display);
                  
        shell.setText("Development Desktop Tool");
        shell.setLayout(new GridLayout());
        // SWT.BOTTOM to show at the bottom
        folder = new CTabFolder(shell, SWT.TOP);
        GridData data = new GridData(SWT.FILL, GridData.FILL, true, true, 3, 3);
        folder.setLayoutData(data);
        CTabItem cTabItem1 = new CTabItem(folder, SWT.NONE);
        cTabItem1.setText("Files");
        CTabItem cTabItem2 = new CTabItem(folder, SWT.NONE);
        cTabItem2.setText("Checks");
        CTabItem cTabItem3 = new CTabItem(folder, SWT.NONE);
        cTabItem3.setText("Results");
       
     // -----------here starts TAB 1 -------------------- 
        Image image_Folder = new Image(display, "folder.png");
        Image image_shape_triangle = new Image(display, "shape_triangle.png");
        Image image_document = new Image(display, "document.png");
        Image image_delete = new Image(display, "delete.png");
        Image image_floppy_disk = new Image(display, "floppy_disk.png");
        Image image_navigate_right = new Image(display, "navigate_right.png");
       
        Composite compTab1 = new Composite(folder, SWT.NULL);
        GridLayout gridLayout1 = new GridLayout(3, false);
        compTab1.setLayout(gridLayout1);
        //compTab1.setLayoutData(new GridData());
        
        Composite compTB1 = new Composite(compTab1, SWT.NULL);
        GridLayout gL1 = new GridLayout(6, false);
        gL1.marginLeft = 0; gL1.marginRight = 0; gL1.horizontalSpacing = 0;
        compTB1.setLayout(gL1);
        GridData sepGD1 = new GridData(GridData.FILL_HORIZONTAL); sepGD1.horizontalSpan = 3; 
        compTB1.setLayoutData(sepGD1);
        compTB1.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        
                
        //1st group
        CustomToolbar custToolBar1 = new CustomToolbar(compTB1, SWT.FLAT, "Files");
        
        item1 = new ToolItem(custToolBar1.getToolBar(), SWT.NONE);
        item1.setImage(image_Folder); item1.setText("Select Files/Directories"); 
        item1.addListener(SWT.Selection, toolBarListener);
        
        item11 = new ToolItem(custToolBar1.getToolBar(), SWT.PUSH);
        item11.setImage(image_shape_triangle); item11.setText("Select Changelist"); 
        item11.addListener(SWT.Selection, toolBarListener);
                
        item111 = new ToolItem(custToolBar1.getToolBar(), SWT.PUSH);
        item111.setImage(image_document); item111.setText("Select FileList file"); 
        item111.addListener(SWT.Selection, toolBarListener);
        
        item1111 = new ToolItem(custToolBar1.getToolBar(), SWT.PUSH);
        item1111.setImage(image_delete); item1111.setText("Clear List"); 
        item1111.addListener(SWT.Selection, toolBarListener);
        
        //This is the separator
        Label verticalSepartor11 = new Label(compTB1, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD11 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor11.setLayoutData(sepGD11); verticalSepartor11.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
        //2nd group
        CustomToolbar custToolBar11 = new CustomToolbar(compTB1, SWT.FLAT, "Configuration");
        
        item12 = new ToolItem(custToolBar11.getToolBar(), SWT.NONE);
        item12.setImage(image_floppy_disk); item12.setText("Save"); 
        item12.addListener(SWT.Selection, toolBarListener);    
        
        
        //This is the separator
        Label verticalSepartor12 = new Label(compTB1, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD12 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor12.setLayoutData(sepGD12); verticalSepartor12.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
       
        //3rd group
        CustomToolbar custToolBar111 = new CustomToolbar(compTB1, SWT.FLAT, "Next Actions");
        
        item13 = new ToolItem(custToolBar111.getToolBar(), SWT.NONE);
        item13.setImage(image_navigate_right); item13.setText("Specify Checks"); 
        item13.addListener(SWT.Selection, toolBarListener);
        
        //This is the separator
        Label verticalSepartor13 = new Label(compTB1, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD13 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor13.setLayoutData(sepGD13); verticalSepartor13.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
        
        Label horSepartorTab1 = new Label(compTab1, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.HORIZONTAL);
        GridData sepGridDataTab1 = new GridData(GridData.FILL_HORIZONTAL); sepGridDataTab1.horizontalSpan = 3; 
        horSepartorTab1.setLayoutData(sepGridDataTab1);horSepartorTab1.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
               
        Composite compButtons1 = new Composite(compTab1, SWT.NULL);
        FillLayout fillLayout1 = new FillLayout();
        fillLayout1.type = SWT.HORIZONTAL;                  
        compButtons1.setLayout(fillLayout1);
       
        cTabItem1.setControl(compTab1);
        
// -----------here starts TAB 2 --------------------              
        Image image_folder_document = new Image(display, "folder_document.png");
        Image image_selection_delete_2 = new Image(display, "selection_delete_2.png");

       
        Composite compTab2 = new Composite(folder, SWT.NULL);
        GridLayout gridLayout2 = new GridLayout(2, false);
        compTab2.setLayout(gridLayout2);
        //compTab2.setLayoutData(new GridData());
        
        Composite compTB2 = new Composite(compTab2, SWT.NULL);
        GridLayout gL2 = new GridLayout(4, false);
        gL2.marginLeft = 0; gL2.marginRight = 0; gL2.horizontalSpacing = 0;
        compTB2.setLayout(gL2);
        GridData sepGD2 = new GridData(GridData.FILL_HORIZONTAL); sepGD2.horizontalSpan = 2; 
        compTB2.setLayoutData(sepGD2);
        compTB2.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        
        
        //1st group
        CustomToolbar custToolBar2 = new CustomToolbar(compTB2, SWT.FLAT, "Configuration");  
     
        item2 = new ToolItem(custToolBar2.getToolBar(), SWT.PUSH);
        item2.setImage(image_folder_document); item2.setText("Load saved check list"); 
        item22 = new ToolItem(custToolBar2.getToolBar(), SWT.PUSH);
        item22.setImage(image_selection_delete_2); item22.setText("Unselect all"); 
        item222 = new ToolItem(custToolBar2.getToolBar(), SWT.PUSH);
        item222.setImage(image_floppy_disk); item222.setText("Save selections"); 
        
        //This is the separator
        Label verticalSepartor21 = new Label(compTB2, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD21 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor21.setLayoutData(sepGD21); verticalSepartor21.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
            
        //2nd group
        CustomToolbar custToolBar22 = new CustomToolbar(compTB2, SWT.FLAT, "Next Actions");
                
        item23 = new ToolItem(custToolBar22.getToolBar(), SWT.PUSH);
        item23.setImage(image_navigate_right); item23.setText("Run checks"); 
        item23.addListener(SWT.Selection, toolBarListener); 
              
        //This is the separator
        Label verticalSepartor22 = new Label(compTB2, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD22 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor22.setLayoutData(sepGD22); verticalSepartor22.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
        
        Label horSepartorTab2 = new Label(compTab2, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.HORIZONTAL);
        GridData sepGridDataTab2 = new GridData(GridData.FILL_HORIZONTAL); sepGridDataTab2.horizontalSpan = 3; 
        horSepartorTab2.setLayoutData(sepGridDataTab2);horSepartorTab2.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
               
        Composite compButtons2 = new Composite(compTab2, SWT.NULL);
        FillLayout fillLayout2 = new FillLayout();
        fillLayout2.type = SWT.HORIZONTAL;                  
        compButtons2.setLayout(fillLayout2);
       
        cTabItem2.setControl(compTab2);
     // -----------here starts TAB 3 --------------------      
        Image image_breakpoint = new Image(display, "breakpoint.png");
        Image image_redo = new Image(display, "redo.png");
        Image image_gecko = new Image(display, "gecko.png");
        Image image_navigate_left2 = new Image(display, "navigate_left2_orange.png");
        Image image_exit = new Image(display, "exit.png");
        
        Composite compTab3 = new Composite(folder, SWT.NULL);
        GridLayout gridLayout3 = new GridLayout(3, false);
        compTab3.setLayout(gridLayout3);
        
        Composite compTB3 = new Composite(compTab3, SWT.NULL);
        GridLayout gL3 = new GridLayout(11, false);
        gL3.marginLeft = 0; gL3.marginRight = 0; gL3.horizontalSpacing = 0;
        compTB3.setLayout(gL3);
        GridData sepGD3 = new GridData(GridData.FILL_HORIZONTAL); sepGD3.horizontalSpan = 5; 
        compTB3.setLayoutData(sepGD3);
        compTB3.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_GRAY));
 
       
        //1st group
        CustomToolbar custToolBar3 = new CustomToolbar(compTB3, SWT.FLAT, "Configuration");   
       
        item3 = new ToolItem(custToolBar3.getToolBar(), SWT.PUSH);
        item3.setImage(image_floppy_disk); item3.setText("Save"); 
        item3.addListener(SWT.Selection, toolBarListener);

        //This is the separator
        Label verticalSepartor31 = new Label(compTB3, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD31 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor31.setLayoutData(sepGD31); verticalSepartor31.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
        //2nd group
        CustomToolbar custToolBar33 = new CustomToolbar(compTB3, SWT.FLAT, "Processing");             
        
        item33 = new ToolItem(custToolBar33.getToolBar(), SWT.PUSH);
        item33.setImage(image_breakpoint); item33.setText("Stop checks"); 
        item33.addListener(SWT.Selection, toolBarListener);
        
        item333 = new ToolItem(custToolBar33.getToolBar(), SWT.PUSH);
        item333.setImage(image_redo); item333.setText("Rerun failed"); 
        item333.addListener(SWT.Selection, toolBarListener);

        //This is the separator
        Label verticalSepartor32 = new Label(compTB3, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD32 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor32.setLayoutData(sepGD32); verticalSepartor32.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
       
        
        //3rd group
        //CustomToolbar custToolBar333 = new CustomToolbar(compTB3, SWT.FLAT, "Filtering");
        
        CSFilteringCheckBoxes checkBoxes = new CSFilteringCheckBoxes(compTB3, SWT.FLAT);
        
        //This is the separator
        Label verticalSepartor33 = new Label(compTB3, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD33 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor33.setLayoutData(sepGD33); verticalSepartor33.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
        
        //4th group
        CustomToolbar custToolBar3333 = new CustomToolbar(compTB3, SWT.FLAT, "Results");
        
        item33333 = new ToolItem(custToolBar3333.getToolBar(), SWT.PUSH);
        item33333.setImage(image_floppy_disk); item33333.setText("Save results"); 
        item33333.addListener(SWT.Selection, toolBarListener);       
        item333333 = new ToolItem(custToolBar3333.getToolBar(), SWT.PUSH);
        item333333.setImage(image_gecko); item333333.setText("Save to geck"); 
        item333333.addListener(SWT.Selection, toolBarListener);
      
        //This is the separator
        Label verticalSepartor34 = new Label(compTB3, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD34 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor34.setLayoutData(sepGD34); verticalSepartor34.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
               
        //5th group
        CustomToolbar custToolBar33333 = new CustomToolbar(compTB3, SWT.FLAT, "Next Actions");      
    
        item3333333 = new ToolItem(custToolBar33333.getToolBar(), SWT.PUSH);
        item3333333.setImage(image_navigate_left2); item3333333.setText("Start over"); 
        item3333333.addListener(SWT.Selection, toolBarListener);
        item33333333 = new ToolItem(custToolBar33333.getToolBar(), SWT.PUSH);
        item33333333.setImage(image_exit); item33333333.setText("Quit"); 
        item33333333.addListener(SWT.Selection, toolBarListener);
        
        //This is the separator
        Label verticalSepartor35 = new Label(compTB3, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.VERTICAL);
        GridData sepGD35 = new GridData(GridData.FILL_VERTICAL);
        verticalSepartor35.setLayoutData(sepGD35); verticalSepartor35.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
                              
        
        Composite compButtons3 = new Composite(compTab3, SWT.NULL);
        FillLayout fillLayout3 = new FillLayout();
        fillLayout3.type = SWT.HORIZONTAL;                  
        compButtons3.setLayout(fillLayout3);
       
        cTabItem3.setControl(compTab3);
       

        //This is the separator between the toolbar and the text box.
        Label horSepartorTab3 = new Label(compTab3, SWT.SEPARATOR | SWT.SHADOW_NONE | SWT.HORIZONTAL);
        GridData sepGridDataTab3 = new GridData(GridData.FILL_HORIZONTAL); sepGridDataTab3.horizontalSpan = 5; 
        horSepartorTab3.setLayoutData(sepGridDataTab3);horSepartorTab3.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_BLACK));
        
        // -----------here ends ALL TABS -------------------- 
        
        //This is the text box
        //GridData txtGridData = new GridData(GridData.FILL_BOTH); txtGridData.horizontalSpan = 2;
        //Text txt = new Text(compTab1, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        //txt.setLayoutData(txtGridData); 
        
        
        GridLayout groupLayout = new GridLayout();
        GridData groupGD = new GridData(GridData.FILL_BOTH);
        Group groupText = new Group(compTab1, SWT.NONE);
        groupText.setLayout(groupLayout);
        groupText.setLayoutData(groupGD);
        groupText.setText("Files in List");
       
        GridData txtGridData = new GridData(GridData.FILL_BOTH);
        Text txt = new Text(groupText, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
        TxtFocusListener txtFL = new TxtFocusListener(compTab1);
        txt.setLayoutData(txtGridData);
        txt.addFocusListener(txtFL);
        txt.setText(TxtFocusListener.introText); txt.setForeground(compTab1.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        

        
       
        shell.addShellListener(new ShellListener() {

            public void shellIconified(ShellEvent e) {
            }
            public void shellDeiconified(ShellEvent e) {
            }
            public void shellDeactivated(ShellEvent e) {
            }
            public void shellClosed(ShellEvent e) {
                System.out.println("Client Area: " + shell.getClientArea());
            }
            public void shellActivated(ShellEvent e) {
                int frameX = shell.getSize().x - shell.getClientArea().width;
                int frameY = shell.getSize().y - shell.getClientArea().height;
                shell.setSize(1200 + frameX, 1000 + frameY);
            }
        });
             
        shell.pack();
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
} 

Open in new window


And this is the RWFile.java:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class RWFile {
    
    private static final String fileName = "psd/swtFile.txt";
    
    public void writeToFile(String content) {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
            out.write(content);
            out.close();
        } catch (IOException e) {
            System.out.println("writeToFile problem:");
            e.printStackTrace();
        }
    }
    
    public String readFromFile() {
        StringBuffer sBuf = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));
            String line;
            while((line = br.readLine()) != null) {
                sBuf.append(line).append("\r\n");
            }
            br.close();
        } catch (IOException e) {
            System.out.println("readFromFile problem:");
            e.printStackTrace();
        }
        return sBuf.toString();
    }
}

Open in new window


This is the TolbarListener.java:

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolItem;


public class ToolBarListener implements Listener {
    
    private Shell shell;
    private Display display;

    @Override
    public void handleEvent(Event event) {
        ToolItem toolItem = (ToolItem) event.widget;
        if (toolItem == Gui.item1) {
            Gui.selectFileFolders();
        }
        else if (toolItem == Gui.item11) {
            ChangeListDlg chngListDlg = new ChangeListDlg(shell);
            chngListDlg.open();
        } else if (toolItem == Gui.item13) {
            Gui.swichTabChecks();
        } else if (toolItem == Gui.item23) {
            Gui.swichTabResults();
        } else if (toolItem == Gui.item3333333) {
            Gui.swichTabFiles();
        } else if (toolItem == Gui.item33333333){
              try {
                    if (!shell.isDisposed()) {
                        shell.close();
                        shell.dispose();
                    }
              } 
              finally {
                  display.dispose();
              }       
          } else if (toolItem == Gui.item111) {
              Gui.readTxt();
          } else if (toolItem == Gui.item12) {
              Gui.writeTxt();
          } else if (toolItem == Gui.item1111) {
              Gui.clearTxt();
          }
    }

    public void setShell(Shell shell, Display display) {
        this.shell = shell; 
        this.display = display;
      }

}

Open in new window


This is the CustomToolbar.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ToolBar;


public class CustomToolbar extends Composite {
    private ToolBar toolBar;
    private Label caption;
       
    public CustomToolbar(Composite parent, int style, String toolBarCaption) {
        super(parent, style);
        Font font = new Font(parent.getDisplay(),"Arial",10,SWT.ITALIC);
        final Color lightgray;
        lightgray = new Color (parent.getDisplay(), 140, 140, 140);
        
        setLayout(new GridLayout());
        toolBar = new ToolBar(this, SWT.FLAT);
        
        //center the icons on each toolbar
        GridData tbGD = new GridData(); tbGD.horizontalAlignment = SWT.CENTER;
        toolBar.setLayoutData(tbGD); 
        
        caption = new Label(this, SWT.HORIZONTAL | SWT.CENTER);
        caption.setText(toolBarCaption);
        caption.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); // center the caption 
        caption.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        caption.setForeground(lightgray);
        caption.setFont(font);
        toolBar.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
        setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GRAY));
    }

    public ToolBar getToolBar() {
        return toolBar;
    }

    public Label getCaption() {
        return caption;
    }

}

Open in new window


This is the ChangeListDlg.java:

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;


public class ChangeListDlg extends Dialog {

    public ChangeListDlg(Shell parent) {
        super(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
    }
    
    public String open() {
        Shell shell = new Shell(getParent(), getStyle());
        shell.setText("Select change list...");
        createContents(shell);
                
        shell.pack();
        shell.open();
        
     // Move the dialog to the center of the top level shell.
        Rectangle shellBounds = getParent().getBounds();
        Rectangle dialogSize = shell.getBounds();

        shell.setLocation(
          shellBounds.x + (shellBounds.width - dialogSize.width) / 2,
          shellBounds.y + (shellBounds.height - dialogSize.height) / 2 );
        
        Display display = getParent().getDisplay();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }
        return "5";
    }
    
    private void createContents(final Shell shell) {
        shell.setLayout(new GridLayout(2, true));

        Label label = new Label(shell, SWT.NONE);
        label.setText("Select from changelists: /examlple 123456 or 134567 or .../");
        GridData data = new GridData();
        data.horizontalSpan = 2;
        label.setLayoutData(data);
        
        final Text text = new Text(shell, SWT.BORDER);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        text.setLayoutData(data);
        // --------- second row -----------------
        Label lbl2 = new Label(shell, SWT.NONE);
        lbl2.setText("Or select by username and workspace:");
        GridData dtNote = new GridData();
        dtNote.horizontalSpan = 2;
        lbl2.setLayoutData(dtNote);
        
        Label label211 = new Label(shell, SWT.NONE);
        label211.setText("User:");
        GridData data211 = new GridData();
        label211.setLayoutData(data211);
        
        Label label212 = new Label(shell, SWT.NONE);
        label212.setText("Workspace:");
        GridData data212 = new GridData();
        label212.setLayoutData(data212);
        
        final Text text221 = new Text(shell, SWT.BORDER);
        GridData data221 = new GridData(GridData.FILL_HORIZONTAL);
        text221.setLayoutData(data221);
        
        final Combo combo222 = new Combo(shell, SWT.READ_ONLY);
        String items[] = { "Workspace # One", "Workspace # Two", "Workspace # Three" };
        combo222.setItems(items); combo222.select(2);
        GridData data222 = new GridData(GridData.FILL_HORIZONTAL);
        combo222.setLayoutData(data222);

        // ----------- the table -----------------
        Table table = new Table(shell, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
        table.setHeaderVisible(true);
        String[] titles = { "Change", "# Files", "Date / Time", "Description" };
        
            for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
                TableColumn column = new TableColumn(table, SWT.NULL);
                column.setText(titles[loopIndex]);
            }
        
          for (int loopIndex = 0; loopIndex < 7; loopIndex++) {
            TableItem item = new TableItem(table, SWT.NULL);
            item.setText(0, "change list " + loopIndex);
            item.setText(1, "" + 2 * loopIndex);
            item.setText(2, "01.07.2013 12:34");
            item.setText(3, "change list description : " + loopIndex);
            if (loopIndex % 2 == 0) item.setBackground(shell.getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
          }
          
          for (int loopIndex = 0; loopIndex < titles.length; loopIndex++) {
              table.getColumn(loopIndex).pack();
          }
          
        GridData data3 = new GridData(GridData.FILL_HORIZONTAL);
        data3.horizontalSpan = 2;
        table.setLayoutData(data3);
        
        // ----------- third row -----------------
        Button ok = new Button(shell, SWT.PUSH);
        ok.setText("Add files from this change list");
        data = new GridData(GridData.FILL_HORIZONTAL);
        ok.setLayoutData(data);
        ok.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent event) {
            shell.close();
          }
        });

        Button cancel = new Button(shell, SWT.PUSH);
        cancel.setText("Cancel");
        data = new GridData(GridData.FILL_HORIZONTAL);
        cancel.setLayoutData(data);
        cancel.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent event) {
            shell.close();
          }
        });

        shell.setDefaultButton(ok);
    }

}

Open in new window


Note: My code tries to read a file in the psd folder which locates in the root of the project. However, my main aim is to read any file that is on the machine. So, I want to be able to browse and select the file that I want to read.

I really appreciate for your help,
0
 
LVL 28

Expert Comment

by:dpearson
ID: 39391690
Posted possible solution in the continuation of this question here:
http://www.experts-exchange.com/Programming/Languages/Java/Q_28190298.html

Doug
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question