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

  • SEARCH
  • Home
  • 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 8498081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:18:20+00:00 2026-06-11T00:18:20+00:00

I am adding a table to my main GUI. It does show up and

  • 0

I am adding a table to my main GUI. It does show up and has the data it is suppose to show. But I feel like I have a big mess of code and it is not structured correctly. I am looking for someone that uses SWT a lot to help me put the right pieces of code in the right places.

Class A – Main GUI with TableViewer

Class B – (ArrayList) Data for table / Class B1 – DataModel for ArrayList Structure

Class A – has method for creating TableViewer

  //////////////////////////////////////////////////////////////////////////
  //                         createTableViewer()                          //
  //////////////////////////////////////////////////////////////////////////
private TableViewer createTableViewer(Composite parent) {
    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.BORDER);
    createColumns(parent, viewer);
    table = viewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // Layout the viewer
    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    viewer.setContentProvider(new ArrayContentProvider());
    *** Getting Array from Class B ***       
    viewer.setInput(AplotDataModel.getInstance().getArrayData());
    viewer.getControl().setLayoutData(gridData);
    return viewer;
}

Class A also has createColumns() method and a createTableViewerColumn() method.

 //////////////////////////////////////////////////////////////////////////
 //                         createColumns()                              //
 //////////////////////////////////////////////////////////////////////////
private void createColumns(final Composite parent, final TableViewer viewer) {
    String[] titles = { "ItemId", "RevId", "PRL", "Dataset Name", "EC Markup" };
    int[] bounds = { 150, 150, 100, 150, 100 };

    TableViewerColumn col = createTableViewerColumn(titles[0], bounds[0], 0);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            AplotDatasetData item = (AplotDatasetData) element;
            return item.getDataset().toString();
        }
    });

    col = createTableViewerColumn(titles[1], bounds[1], 1);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            AplotDatasetData item = (AplotDatasetData) element;
            return item.getRev().toString();
        }
    });

    col = createTableViewerColumn(titles[2], bounds[2], 2);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            AplotDatasetData item = (AplotDatasetData) element;
            return item.getPRLValue();
        }
    });

    col = createTableViewerColumn(titles[3], bounds[3], 3);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            AplotDatasetData item = (AplotDatasetData) element;
            return item.getDatasetName();
        }
    });

    col = createTableViewerColumn(titles[4], bounds[4], 4);
    col.setLabelProvider(new ColumnLabelProvider() {
        @Override
        public String getText(Object element) {
            AplotDatasetData item = (AplotDatasetData) element;
            return item.getECMarkupValue();
        }
    });
}

 //////////////////////////////////////////////////////////////////////////
 //                       createTableViewerColumn()                      //
 //////////////////////////////////////////////////////////////////////////
private TableViewerColumn createTableViewerColumn(String title, int bound, final int colNumber) {
    final TableViewerColumn viewerColumn = new TableViewerColumn(viewer, SWT.NONE);
    final TableColumn column = viewerColumn.getColumn();
    column.setText(title);
    column.setWidth(bound);
    column.setResizable(true);
    column.setMoveable(true);
    return viewerColumn;
}

Question 1: Is this the best practice to adding a table to my GUI class? Seems like a lot of code for the GUI class.

Question 2: Should the createColumns() method and the createTableViewerColumn() method be moved to Class B?

Question 3: My last column in the table is going to be a dropdown/combo box. So I am going to have to extend one class with EditingSupport. Should it be Class A or Class B?

Before I go any farther with this project I want to make sure I have it correctly structured.

  • 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-11T00:18:22+00:00Added an answer on June 11, 2026 at 12:18 am

    Answering this question is like answering which ice-cream flavor do you like 🙂

    Q & A

    Question 1: Is this the best practice to adding a table to my GUI
    class? Seems like a lot of code for the GUI class.

    If the code is less than its not a bad idea. But if –

    1. The code is large (for me if its more than 100 lines).
    2. Overall System is going to be more than some thousand of lines of code.
    3. Someone else is going to maintain it
    4. If I need plug-and-play nature, for example today Table is sufficient but in future I may use Grid.
    5. If I am doing some custom drawing (doing some custom painting on windows paint event) or using custom controls, THEN

    I normally prefer to sub-class the viewer/control. In this way I could maintain the separation between the bare-bone message pumping code, GUI controls and my data model.

    Question 2: Should the createColumns() method and the
    createTableViewerColumn() method be moved to Class B?

    No, You should not. As in your case the class B is your data model/supplier. The JFace programming model supports MVC architecture and if possible one should follow it. Suggested solution is to have a new class extending the TableViewer.

    Question 3: My last column in the table is going to be a
    dropdown/combo box. So I am going to have to extend one class with
    EditingSupport. Should it be Class A or Class B?

    I would suggest you to go for a new class and use that in the extended TableViewer. One benefit is that in case you are writing data back to some db then your viewer class remain db/persistence layer agnostic.

    Code Sample

    Below is a simple sample application. As you can see I can change my combo editor to text editor just by changing the OptionEditingSupport class. And also, the code looks clean and concise.

    Main Class

    package sample;
    
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    
    public class SampleApp 
    {
        public SampleApp(Shell shell)
        {
            AppPersonViewer personViewer = new AppPersonViewer(shell, SWT.BORDER|SWT.V_SCROLL|SWT.FULL_SELECTION);
            DataModel model = new DataModel(20);
            personViewer.setInput(model);
        }
    
        public static void main(String[] args) {
            Display display = new Display ();
            Shell shell = new Shell(display);
            shell.setLayout(new FillLayout());
            new SampleApp(shell);
            shell.open ();
    
            while (!shell.isDisposed ()) {
                if (!display.readAndDispatch ()) display.sleep ();
            }
    
            display.dispose ();
        }
    }
    

    Extended Table Viewer

    package sample;
    
    import org.eclipse.jface.viewers.ColumnLabelProvider;
    import org.eclipse.jface.viewers.TableViewer;
    import org.eclipse.jface.viewers.TableViewerColumn;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.GridData;
    import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.widgets.Table;
    
    public class AppPersonViewer extends TableViewer
    {
        public AppPersonViewer(Composite parent, int style) 
        {
            super(parent, style);
            Table table = getTable();
            GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
            table.setLayoutData(gridData);
            createColumns();
            table.setHeaderVisible(true);
            table.setLinesVisible(true);
            setContentProvider(new AppContentProvider());
        }
    
        private void createColumns()
        {
            String[] titles = { "First Name", "Second Name", "Age", "Country", "Likes SO" };
            int[] bounds = { 150, 150, 100, 150, 100 };
    
            TableViewerColumn column = createTableViewerColumn(titles[0], bounds[0], 0);
            column.setLabelProvider(new ColumnLabelProvider(){
                public String getText(Object element) {
                    if(element instanceof Person)
                        return ((Person)element).getFirst();
                    return super.getText(element);
                }
            });
    
            column = createTableViewerColumn(titles[1], bounds[1], 1);
            column.setLabelProvider(new ColumnLabelProvider(){
                public String getText(Object element) {
                    if(element instanceof Person)
                        return ((Person)element).getSecond();
                    return super.getText(element);
                }
            });
    
            column = createTableViewerColumn(titles[2], bounds[2], 2);
            column.setLabelProvider(new ColumnLabelProvider(){
                public String getText(Object element) {
                    if(element instanceof Person)
                        return ""+((Person)element).getAge();
                    return super.getText(element);
                }
            });
    
            column = createTableViewerColumn(titles[3], bounds[3], 3);
            column.setLabelProvider(new ColumnLabelProvider(){
                public String getText(Object element) {
                    if(element instanceof Person)
                        return ((Person)element).getCountry();
                    return super.getText(element);
                }
            });
    
            column = createTableViewerColumn(titles[4], bounds[4], 4);
            column.setLabelProvider(new ColumnLabelProvider(){
                public String getText(Object element) {
                    if(element instanceof Person)
                        return ((Person)element).getLikes();
                    return super.getText(element);
                }
            });
    
            column.setEditingSupport(new OptionEditingSupport(this));
        }
    
        private TableViewerColumn createTableViewerColumn(String header, int width, int idx) 
        {
            TableViewerColumn column = new TableViewerColumn(this, SWT.LEFT, idx);
            column.getColumn().setText(header);
            column.getColumn().setWidth(width);
            column.getColumn().setResizable(true);
            column.getColumn().setMoveable(true);
    
            return column;
        }
    }
    

    Content Provider

    package sample;
    
    import org.eclipse.jface.viewers.IStructuredContentProvider;
    import org.eclipse.jface.viewers.Viewer;
    
    public class AppContentProvider implements IStructuredContentProvider
    {
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
         */
        public Object[] getElements(Object inputElement) {
            if(inputElement instanceof DataModel)
                return ((DataModel)inputElement).getData().toArray();
            return null;
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.IContentProvider#dispose()
         */
        public void dispose() {
        }
    
        /* (non-Javadoc)
         * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
         */
        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
    }
    

    Cell Editor Support

    package sample;
    
    import org.eclipse.jface.viewers.CellEditor;
    import org.eclipse.jface.viewers.ColumnViewer;
    import org.eclipse.jface.viewers.ComboBoxCellEditor;
    import org.eclipse.jface.viewers.EditingSupport;
    import org.eclipse.jface.viewers.TableViewer;
    
    public class OptionEditingSupport extends EditingSupport 
    {
        private ComboBoxCellEditor cellEditor;
    
        public OptionEditingSupport(ColumnViewer viewer) {
            super(viewer);
            cellEditor = new ComboBoxCellEditor(((TableViewer)viewer).getTable(), new String[]{"Y", "N"});
        }
        protected CellEditor getCellEditor(Object element) {
            return cellEditor;
        }
        protected boolean canEdit(Object element) {
            return true;
        }
        protected Object getValue(Object element) {
            return 0;
        }
        protected void setValue(Object element, Object value) 
        {
            if((element instanceof Person) && (value instanceof Integer)) {
                Integer choice = (Integer)value;
                String option = (choice == 0? "Y":"N");
                ((Person)element).setLikes( option );
                getViewer().update(element, null);
            }
        }
    }
    

    Data Model

    package sample;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class DataModel 
    {
        private int samples;
        public DataModel(int samples) {
            this.samples = samples;
        }
    
        List<Person> getData()
        {
            List<Person> persons = new ArrayList<Person>();
            for(int i=0; i<samples; i++)
                persons.add(Person.createRandomPerson());
            return persons;
        }
    }
    

    The Person Entity

    package sample;
    
    import java.util.Random;
    
    public class Person 
    {
        private static final String[]   FIRST = {"Favonius", "Tim", "Brad", "Scott", "Linda"};
        private static final String[]   SECOND = {"Cruise", "Temp", "Abbey", "Adam", "Albert", "Thomas"};
        private static final String[]   COUNTRY = {"India", "USA", "Russia", "UK", "France", "Germany"};
        private static final int[]      AGE = {22, 23, 24, 25, 26, 27, 28, 29, 30};
    
        private static Random random = new Random(System.currentTimeMillis());
    
        private String first;
        private String second;
        private String country;
        private String likes;
    
        private int age;
    
        public Person(String first, String second, String country, String likes, int age) 
        {
            super();
            this.first = first;
            this.second = second;
            this.country = country;
            this.likes = likes;
            this.age = age;
        }
        public String getFirst() {
            return first;
        }
        public String getSecond() {
            return second;
        }
        public String getCountry() {
            return country;
        }
        public String getLikes() {
            return likes;
        }
        public int getAge() {
            return age;
        }
        public void setLikes(String likes) {
            this.likes = likes;
        }
        public static Person createRandomPerson(){
            return new  Person(FIRST[random.nextInt(FIRST.length)], 
                    SECOND[random.nextInt(SECOND.length)], COUNTRY[random.nextInt(COUNTRY.length)], 
                    "Y", AGE[random.nextInt(AGE.length)]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PyGTK GUI that has a gtk.Table. Pressing one button will generate
Currently have a table .. and need to start adding new data columns to
I'm adding a table dynamically to JQuery MObile UK Table goes on like this
I understand that when adding a column to a table containing data in SQL
So I have a table layout that Im adding rows to through code since
I have a main product which is installed using Wix and has its own
I have a problem with adding index to memory database attached to main database.
I have a table which contains sales order data (order number, product number, sales
I have a JTable where I have set table.setAutoCreateRowSorter(true); . My main objective is
I am writing a cursor to populate data in new table from main table

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.