Java Gnome FAQ Advanced Widgets

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
(нарушало авторские права)
 
Строка 1: Строка 1:
-
In this part of the Java Gnome programming tutorial, we will introduce some more advanced widgets.
 
-
__TOC__
 
-
 
-
== TextView ==
 
-
 
-
<b>TexView</b> widget is used for displaying and editing multiline text.
 
-
TexView widget has also the MVC design. The TextView represents the view component and <b>TexBuffer</b> represents the model component. The TexBuffer is used to manipulate the text data. <b>TextTag</b> is an attribute that can be applied to the text. The <b>TextIter</b> represents a position between two characters in the text. All manipulation with the text is done using text iterators.
 
-
 
-
<source lang="java">
 
-
package com.zetcode;
 
-
 
-
import org.gnome.gdk.Event;
 
-
import org.gnome.gtk.Gtk;
 
-
 
-
import org.gnome.gtk.TextBuffer;
 
-
import org.gnome.gtk.TextIter;
 
-
import org.gnome.gtk.TextTag;
 
-
import org.gnome.gtk.TextView;
 
-
import org.gnome.gtk.Widget;
 
-
import org.gnome.gtk.Window;
 
-
import org.gnome.gtk.WindowPosition;
 
-
import org.gnome.gtk.WrapMode;
 
-
import org.gnome.pango.FontDescription;
 
-
import org.gnome.pango.Style;
 
-
 
-
/**
 
-
* ZetCode Java Gnome tutorial
 
-
*
 
-
* This program demonstrates the
 
-
* TextView widget.
 
-
*
 
-
* @author jan bodnar
 
-
* website zetcode.com
 
-
* last modified March 2009
 
-
*/
 
-
 
-
public class GTextView extends Window {
 
-
 
-
    public GTextView() {
 
-
   
 
-
        setTitle("TextView");
 
-
   
 
-
        initUI();
 
-
       
 
-
        connect(new Window.DeleteEvent() {
 
-
            public boolean onDeleteEvent(Widget source, Event event) {
 
-
                Gtk.mainQuit();
 
-
                return false;
 
-
            }
 
-
        });
 
-
   
 
-
        setDefaultSize(250, 200);
 
-
        setPosition(WindowPosition.CENTER);
 
-
        showAll();
 
-
    }
 
-
   
 
-
   
 
-
    public void initUI() {
 
-
       
 
-
        final TextView view;
 
-
        final FontDescription desc;
 
-
        final TextBuffer buffer;
 
-
        final TextTag colorText, italics, background, mleft;
 
-
        TextIter pointer;
 
-
 
-
        view = new TextView();
 
-
 
-
        desc = new FontDescription("Sans, 10");
 
-
        view.modifyFont(desc);
 
-
 
-
        buffer = new TextBuffer();
 
-
 
-
        colorText = new TextTag();
 
-
        colorText.setForeground("blue");
 
-
 
-
        italics = new TextTag();
 
-
        italics.setStyle(Style.ITALIC);
 
-
 
-
        background = new TextTag();
 
-
        background.setBackground("lightgray");
 
-
       
 
-
        mleft = new TextTag();
 
-
        mleft.setLeftMargin(8);
 
-
 
-
        pointer = buffer.getIterStart();
 
-
        buffer.insert(pointer, "Plain text \n");
 
-
        buffer.insert(pointer, "Colored text\n", new TextTag[] {colorText, mleft});
 
-
        buffer.insert(pointer, "Text with colored background\n", background);
 
-
        buffer.insert(pointer, "Text in italics", italics);
 
-
 
-
        view.setBuffer(buffer);
 
-
        view.setPaddingAboveParagraph(5);
 
-
        add(view);
 
-
       
 
-
    }
 
-
   
 
-
   
 
-
    public static void main(String[] args) {
 
-
        Gtk.init(args);
 
-
        new GTextView();
 
-
        Gtk.main();
 
-
    }   
 
-
}
 
-
</source>
 
-
 
-
The example shows some text with different  <b>TextTags</b> applied.
 
-
 
-
<source lang="java">
 
-
view = new TextView();
 
-
</source>
 
-
 
-
TextView widget is created.
 
-
 
-
<source lang="java">
 
-
desc = new FontDescription("Sans, 10");
 
-
view.modifyFont(desc);
 
-
</source>
 
-
 
-
We set the font for the <b>TextView</b> to be Sans 10.
 
-
 
-
<source lang="java">
 
-
colorText = new TextTag();
 
-
colorText.setForeground("blue");
 
-
</source>
 
-
 
-
A <b>TextTag</b> widget is created. This tag will set the color of the text to blue, if applied.
 
-
 
-
<source lang="java">
 
-
buffer.insert(pointer, "Text in italics", italics);
 
-
</source>
 
-
 
-
Here we apply an italics text tag for the specified text.
 
-
 
-
[[image: Java_Gnome_faq_textview.png| center]]
 
-
 
-
== ListView ==
 
-
 
-
In the following example, we use the <b>TreeView</b> widget  in a list view mode. The <b>ListStore</b> is used to store data.
 
-
 
-
<source lang="java">
 
-
 
-
package com.zetcode;
 
-
 
-
import org.gnome.gdk.Event;
 
-
import org.gnome.gtk.CellRendererText;
 
-
import org.gnome.gtk.DataColumn;
 
-
import org.gnome.gtk.DataColumnString;
 
-
import org.gnome.gtk.Gtk;
 
-
import org.gnome.gtk.ListStore;
 
-
import org.gnome.gtk.Statusbar;
 
-
import org.gnome.gtk.TreeIter;
 
-
import org.gnome.gtk.TreePath;
 
-
import org.gnome.gtk.TreeView;
 
-
import org.gnome.gtk.TreeViewColumn;
 
-
import org.gnome.gtk.VBox;
 
-
import org.gnome.gtk.Widget;
 
-
import org.gnome.gtk.Window;
 
-
import org.gnome.gtk.WindowPosition;
 
-
 
-
/**
 
-
* ZetCode Java Gnome tutorial
 
-
*
 
-
* This program demonstrates the
 
-
* TreeView widget in listview mode.
 
-
*
 
-
* @author jan bodnar
 
-
* website zetcode.com
 
-
* last modified March 2009
 
-
*/
 
-
 
-
public class GListView extends Window {
 
-
 
-
    private Actress[] actresses =
 
-
    {
 
-
        new Actress("Jessica Alba", "Pomona", 1981),
 
-
        new Actress("Sigourney Weaver", "New York", 1949),
 
-
        new Actress("Angelina Jolie", "Los Angeles", 1975),
 
-
        new Actress("Natalie Portman", "Jerusalem", 1981),
 
-
        new Actress("Rachel Weissz", "London", 1971),
 
-
        new Actress("Scarlett Johansson", "New York", 1984)
 
-
    };
 
-
 
-
    private Statusbar statusbar;
 
-
 
-
 
-
    public GListView() {
 
-
   
 
-
        setTitle("ListView");
 
-
       
 
-
        initUI();
 
-
       
 
-
        connect(new Window.DeleteEvent() {
 
-
            public boolean onDeleteEvent(Widget source, Event event) {
 
-
                Gtk.mainQuit();
 
-
                return false;
 
-
            }
 
-
        });
 
-
       
 
-
        setDefaultSize(350, 300);
 
-
        setPosition(WindowPosition.CENTER);
 
-
        showAll();
 
-
    }
 
-
   
 
-
   
 
-
    public void initUI() {
 
-
       
 
-
        final TreeView view;
 
-
        final ListStore model;
 
-
        TreeIter row;
 
-
        CellRendererText renderer;
 
-
        TreeViewColumn column;
 
-
 
-
        final DataColumnString nameCol;
 
-
        final DataColumnString placeCol;
 
-
        final DataColumnString yearCol;
 
-
       
 
-
       
 
-
        statusbar = new Statusbar();
 
-
       
 
-
        model = new ListStore(new DataColumn[] {
 
-
                nameCol = new DataColumnString(),
 
-
                placeCol = new DataColumnString(),
 
-
                yearCol = new DataColumnString(),
 
-
        });
 
-
 
-
       
 
-
        for (Actress act : actresses) {
 
-
            row = model.appendRow();
 
-
            model.setValue(row, nameCol, act.name);
 
-
            model.setValue(row, placeCol, act.place);
 
-
            model.setValue(row, yearCol, String.valueOf(act.year));
 
-
        }
 
-
 
-
        view = new TreeView(model);
 
-
 
-
        column = view.appendColumn();
 
-
        column.setTitle("Name");
 
-
        renderer = new CellRendererText(column);
 
-
        renderer.setText(nameCol);
 
-
 
-
        column = view.appendColumn();
 
-
        column.setTitle("Place");
 
-
        renderer = new CellRendererText(column);
 
-
        renderer.setText(placeCol);
 
-
 
-
        column = view.appendColumn();
 
-
        column.setTitle("Year");
 
-
        renderer = new CellRendererText(column);
 
-
        renderer.setText(yearCol);
 
-
       
 
-
        view.connect(new TreeView.RowActivated() {
 
-
 
-
            public void onRowActivated(TreeView treeView,
 
-
                        TreePath treePath,
 
-
                        TreeViewColumn treeViewColumn) {
 
-
                       
 
-
                final TreeIter row;
 
-
                final String place;
 
-
                final String name;
 
-
                final String year;
 
-
                final String text;
 
-
               
 
-
                row = model.getIter(treePath);
 
-
 
-
                place = model.getValue(row, placeCol);
 
-
                name = model.getValue(row, nameCol);
 
-
                year = model.getValue(row, yearCol);
 
-
 
-
                text = name + ", " + place + ", " + year;
 
-
 
-
                statusbar.setMessage(text);
 
-
            }
 
-
        });
 
-
 
-
        VBox vbox = new VBox(false, 0);
 
-
       
 
-
        vbox.packStart(view);
 
-
        vbox.packStart(statusbar, false, false, 0);
 
-
        add(vbox);
 
-
 
-
    }
 
-
   
 
-
    class Actress {
 
-
       
 
-
      public String name;
 
-
      public String place;
 
-
      public int year;
 
-
       
 
-
      Actress(String name, String place, int year) {
 
-
          this.name = name;
 
-
          this.place = place;
 
-
          this.year = year;
 
-
      }
 
-
    }
 
-
   
 
-
   
 
-
    public static void main(String[] args) {
 
-
        Gtk.init(args);
 
-
        new GListView();
 
-
        Gtk.main();
 
-
    }
 
-
}
 
-
</source>
 
-
 
-
In our example, we show a list of six actresses in the <b>TreeView</b> widget. Each of the rows shows the name, the place of born and the year of born for each of them.
 
-
 
-
<source lang="java">
 
-
    class Actress {
 
-
      public String name;
 
-
      public String place;
 
-
      public int year;
 
-
      ...
 
-
    }
 
-
 
-
</source>
 
-
 
-
The <b>Actress</b> class is used for storing data about an actress.
 
-
 
-
<source lang="java">
 
-
model = new ListStore(new DataColumn[] {
 
-
        nameCol = new DataColumnString(),
 
-
        placeCol = new DataColumnString(),
 
-
        yearCol = new DataColumnString(),
 
-
});
 
-
</source>
 
-
 
-
These lines declare the model to have three  string data columns.
 
-
 
-
<source lang="java">
 
-
for (Actress act : actresses) {
 
-
    row = model.appendRow();
 
-
    model.setValue(row, nameCol, act.name);
 
-
    model.setValue(row, placeCol, act.place);
 
-
    model.setValue(row, yearCol, String.valueOf(act.year));
 
-
}
 
-
</source>
 
-
 
-
The model is filled with data from the array of actresses.
 
-
 
-
<source lang="java">
 
-
column = view.appendColumn();
 
-
column.setTitle("Name");
 
-
renderer = new CellRendererText(column);
 
-
renderer.setText(nameCol);
 
-
</source>
 
-
 
-
A new column is created with "Name" title. Each column has a cell renderer.
 
-
The cell renderer is responsible for displaying data. In our case we have the most common cell renderer, the <b>CellRendererText</b>.
 
-
As the name suggests, it shows textual data in a <b>TreeViewColumn</b>.
 
-
 
-
<source lang="java">
 
-
row = model.getIter(treePath);
 
-
 
-
place = model.getValue(row, placeCol);
 
-
name = model.getValue(row, nameCol);
 
-
year = model.getValue(row, yearCol);
 
-
 
-
text = name + ", " + place + ", " + year;
 
-
 
-
statusbar.setMessage(text);
 
-
</source>
 
-
 
-
If we double click on an item, we display the whole row in the statusbar.
 
-
 
-
[[image: Java_Gnome_faq_listview.png| center]]
 
-
 
-
== Tree ==
 
-
 
-
In the last example of this chapter, we use the <b>TreeView</b> widget to show a hierarchical tree of data.
 
-
 
-
<source lang="java">
 
-
 
-
package com.zetcode;
 
-
 
-
import org.gnome.gdk.Event;
 
-
import org.gnome.gtk.CellRendererText;
 
-
import org.gnome.gtk.DataColumn;
 
-
import org.gnome.gtk.DataColumnString;
 
-
import org.gnome.gtk.Gtk;
 
-
import org.gnome.gtk.Statusbar;
 
-
import org.gnome.gtk.TreeIter;
 
-
import org.gnome.gtk.TreePath;
 
-
import org.gnome.gtk.TreeStore;
 
-
import org.gnome.gtk.TreeView;
 
-
import org.gnome.gtk.TreeViewColumn;
 
-
import org.gnome.gtk.VBox;
 
-
import org.gnome.gtk.Widget;
 
-
import org.gnome.gtk.Window;
 
-
import org.gnome.gtk.WindowPosition;
 
-
 
-
/**
 
-
* ZetCode Java Gnome tutorial
 
-
*
 
-
* This program demonstrates the
 
-
* TreeView widget in tree view mode.
 
-
*
 
-
* @author jan bodnar
 
-
* website zetcode.com
 
-
* last modified March 2009
 
-
*/
 
-
 
-
public class GTree extends Window {
 
-
 
-
    private DataColumnString dataCol;
 
-
 
-
    private TreeStore model;
 
-
    private TreeIter row;
 
-
    private TreeView view;
 
-
    private TreeIter rowChild;
 
-
 
-
    private TreeViewColumn column;
 
-
    private CellRendererText renderer;
 
-
 
-
    private Statusbar statusbar;
 
-
 
-
    public GTree() {
 
-
   
 
-
        setTitle("GTree");
 
-
       
 
-
        initUI();
 
-
       
 
-
        connect(new Window.DeleteEvent() {
 
-
            public boolean onDeleteEvent(Widget source, Event event) {
 
-
                Gtk.mainQuit();
 
-
                return false;
 
-
            }
 
-
        });
 
-
   
 
-
        setDefaultSize(350, 300);
 
-
        setPosition(WindowPosition.CENTER);
 
-
        showAll();
 
-
    }
 
-
   
 
-
    public void initUI() {
 
-
 
-
        statusbar = new Statusbar();
 
-
 
-
        model = new TreeStore(new DataColumn[] {
 
-
                dataCol = new DataColumnString(),
 
-
        });
 
-
       
 
-
        row = model.appendRow();
 
-
        model.setValue(row, dataCol, "Scripting languages");
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue(rowChild, dataCol, "PHP");       
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue(rowChild, dataCol, "Python");
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue(rowChild, dataCol, "PERL");
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue(rowChild, dataCol, "Ruby");
 
-
       
 
-
        row = model.appendRow();
 
-
        model.setValue(row, dataCol, "Compiling languages");
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue( rowChild, dataCol, "Java");       
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue( rowChild, dataCol, "C++");
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue( rowChild, dataCol, "C#");
 
-
        rowChild = model.appendChild( row );
 
-
        model.setValue( rowChild, dataCol, "C");
 
-
 
-
        view = new TreeView(model);
 
-
       
 
-
        column = view.appendColumn();
 
-
        column.setTitle("Programming Languages");
 
-
        renderer = new CellRendererText(column);
 
-
        renderer.setText(dataCol); 
 
-
   
 
-
        view.connect(new TreeView.RowActivated() {
 
-
 
-
            public void onRowActivated(TreeView treeView,
 
-
                        TreePath treePath,
 
-
                        TreeViewColumn treeViewColumn) {
 
-
                       
 
-
                final TreeIter row;
 
-
                final String text;
 
-
               
 
-
                row = model.getIter(treePath);
 
-
                text = model.getValue(row, dataCol);
 
-
 
-
                statusbar.setMessage(text);
 
-
            }
 
-
        });
 
-
       
 
-
        VBox vbox = new VBox(false, 0);
 
-
       
 
-
        vbox.packStart(view);
 
-
        vbox.packStart(statusbar, false, false, 0);
 
-
        add(vbox);
 
-
    }
 
-
   
 
-
    public static void main(String[] args) {
 
-
        Gtk.init(args);
 
-
        new GTree();
 
-
        Gtk.main();
 
-
    } 
 
-
}
 
-
</source>
 
-
 
-
This time we use the <b>TreeView</b> widget to show hierarchical  data.
 
-
 
-
<source lang="java">
 
-
model = new TreeStore(new DataColumn[] {
 
-
        dataCol = new DataColumnString(),
 
-
});
 
-
</source>
 
-
 
-
Here we declare the <b>TreeStore</b> to have one string data column.
 
-
 
-
<source lang="java">
 
-
model.setValue(row, dataCol, "Scripting languages");
 
-
</source>
 
-
 
-
We append a top level node.
 
-
 
-
<source lang="java">
 
-
rowChild = model.appendChild( row );
 
-
model.setValue(rowChild, dataCol, "PHP");
 
-
</source>
 
-
 
-
 
-
We append a child row for the top level node.
 
-
 
-
<source lang="java">
 
-
view = new TreeView(model);
 
-
</source>
 
-
 
-
We create the instance of the <b>TreeView</b> widget.
 
-
 
-
<source lang="java">
 
-
column = view.appendColumn();
 
-
column.setTitle("Programming Languages");
 
-
renderer = new CellRendererText(column);
 
-
renderer.setText(dataCol);
 
-
 
-
</source>
 
-
 
-
We create the column and the cell renderer.
 
-
 
-
<source lang="java">
 
-
row = model.getIter(treePath);
 
-
text = model.getValue(row, dataCol);
 
-
 
-
statusbar.setMessage(text);
 
-
</source>
 
-
 
-
Upon double click, we show the selected node in the statusbar.
 
-
 
-
[[image: Java_Gnome_faq_tree.png| center]]
 
-
 
-
In this chapter, we were talking about advanced Java Gnome widgets.
 
-
 
-
 
-
 
-
[[Категория:GTK+]]
 
-
[[Категория:Java]]
 

Текущая версия на 11:23, 7 апреля 2009