Category Archives: Swing

Determining the selected file in the JFileChooser

Introduction

The JFileChooser is a Swing component to choose files from the FileSystem. After you chose a file (or Directory you need to get the selected file back in your code. This example shows how it’s done.

JFileChooser
JFileChooser
JFileChooser chooser = new JFileChooser();

// Set the current directory to the 
// application's current directory
try {
    // Create a File object containing the
    // canonical path of the
    // desired file
    File f = new File(new File("filename.txt").getCanonicalPath());

    // Set the selected file
    chooser.setSelectedFile(f);
} catch (IOException e) {
}

// Show the dialog; wait until dialog is closed
chooser.showOpenDialog(frame);

// Get the currently selected file
File curFile = chooser.getSelectedFile();

 

Walking Views

Introduction

A very important part of the Swing text package is the View class. As the name suggests it represents a view of the text model, or a piece of the text model. It is this class that is responsible for the look of the text component. The view is not intended to be some completely new thing that one must learn, but rather is much like a lightweight component.

By default, a view is very light. It contains a reference to the parent view from which it can fetch many things without holding state, and it contains a reference to a portion of the model (Element). A view does not have to exactly represent an element in the model, that is simply a typical and therefore convenient mapping. A view can alternatively maintain a couple of Position objects to maintain its location in the model (i.e. represent a fragment of an element). This is typically the result of formatting where views have been broken down into pieces. The convenience of a substantial relationship to the element makes it easier to build factories to produce the views, and makes it easier to keep track of the view pieces as the model is changed and the view must be changed to reflect the model. Simple views therefore represent an Element directly and complex views do not.

This example shows you how to navigate trough the hierarchy of views.

// Create a text component
JTextComponent textComp = new JTextPane();

// Get the root view
View v = textComp.getUI().getRootView(textComp);

// Walk the view hierarchy
walkView(v, 0);
// Recursively walks a view hierarchy
public void walkView(View view, int level) {
    // Process view...

    // Get number of children views
    int n = view.getViewCount();

    // Visit the children of this view
    for (int i=0; i<n; i++) {
        walkView(view.getView(i), level+1);
     }
}

 

Enumerating the parameters in the Swing JTextPane

Introduction

JPanel
JPanel

The java.swing.jpanel is a generic lightweight container. For examples and task-oriented documentation for JPanel, see How to Use Panels, a section in The Java Tutorial. The example below shows how to enumerate over the paragraphs in the JTextPane.

JTextPane
JTextPane

 

 

 

 

// Create a text pane
JTextPane textPane = new JTextPane();

// Get section element 
Element section = textPane.getDocument().getDefaultRootElement();

// Get number of paragraphs.
// In a text pane, a span of characters
// terminated by single
// newline is typically called a paragraph.
int paraCount = section.getElementCount();

// Get index ranges for each paragraph
for (int i=0; i<paraCount; i++) {
    Element e = section.getElement(i);
    int rangeStart = e.getStartOffset();
    int rangeEnd = e.getEndOffset();
    try {
        String para = textPane.getText(rangeStart, rangeEnd-rangeStart);
    } catch (BadLocationException ex) {
    }
}

See also the Swing UITutorial.

Adding TabStops to the JTextPane

Introduction

This example shows how to create Tab Stops in a JTextPane. This is useful since the JTextPane doesn’t have this by default.

// Create a text pane
JTextPane textPane = new JTextPane();

// Create one of each type of tab stop
java.util.List list = new ArrayList();

// Create a left-aligned tab stop at 100 pixels from the left margin
float pos = 100;
int align = TabStop.ALIGN_LEFT;
int leader = TabStop.LEAD_NONE;
TabStop tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a right-aligned tab stop at 200 pixels
// from the left margin
pos = 200;
align = TabStop.ALIGN_RIGHT;
leader = TabStop.LEAD_NONE;
tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a center-aligned tab stop at 300 pixels
// from the left margin
pos = 300;
align = TabStop.ALIGN_CENTER;
leader = TabStop.LEAD_NONE;
tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a decimal-aligned tab stop at 400 pixels from the left margin
pos = 400;
align = TabStop.ALIGN_DECIMAL;
leader = TabStop.LEAD_NONE;
tstop = new TabStop(pos, align, leader);
list.add(tstop);

// Create a tab set from the tab stops
TabStop[] tstops = (TabStop[])list.toArray(new TabStop[0]);
TabSet tabs = new TabSet(tstops);

// Add the tab set to the logical style;
// the logical style is inherited by all paragraphs
Style style = textPane.getLogicalStyle();
StyleConstants.setTabSet(style, tabs);
textPane.setLogicalStyle(style);