• Home   /  
  • Archive by category "1"

The Bibliography Change Title Bar

Download the sample database

Access 2007 databases have several built-in properties that define the behavior of the database. To learn more about those properties in a given database, you can use the Properties property. For example, open the sample database, ChangeAccessCaption.accdb, and load the module basProperties in the VBA editor. Click in the ListProperties procedure, which contains the following code.

If you click in the procedure and press F5 to run it, it generates output to the Immediate window that lists all of the properties of the current database.

Figure 1. ListProperties outputs a list of properties in the database


You can copy this code into any database and then run the code to review the properties for that database. The code loops through the complete set of database properties and displays a name and value for each property.

Note

Some database properties have values that cannot be printed this easily. Those values generate an error when the code attempts to print them. In those cases, the code disregards the error and prints the property name without printing the property value. In other words, you end up with a complete list of property names, but not necessarily a complete list of property values.

To set the text in the title bar of the main Access window, you must set the AppTitle property for the database. However, Access 2007 does not include the AppTitle property by default when you create a new database, so you must first create the property.

That work has already been done in the sample form, frmSetAccessCaption, which is included in the sample database, ChangeAccessCaption.accdb. The form displays the current Access caption; that is, the text in the title bar of the main Access window. To change that text, type a new value in the New Access Caption text box and then click the Set New Caption button. To revert to the original text in the title bar, click the Reset Caption button when you are done.

Figure 2. Clicking Set New Caption modifies the main Access title bar


To include similar functionality in your applications, follow these steps:

  1. Import the module basCaption from the sample database, ChangeAccessCaption.accdb; that module supplies the necessary procedures.

  2. To retrieve the current Access caption, call the GetTitleBar function that is in basCaption. If GetTitleBar is unable to retrieve the AppTitle property of the database, then it assumes that the current caption is "Microsoft Access". Call the function by using code that is similar to the following example.

    Dim caption AsString caption = GetTitleBar()
  3. To set a new Access caption, call the SetTitleBar method and pass it a string that holds your new caption, similar to the following code. (You cannot set the title to be empty; if you pass an empty string you will receive an error message.)

If you examine the contents of the basCaption module, you find two public procedures, SetTitleBar and GetTitleBar, and two private procedures, SetDBProperty and CreateDBProperty. The SetTitleBar procedure calls SetDBProperty, passing in the property name, type, and value, and it lets SetDBProperty do the work (you can use SetDBProperty to set any database property). It then calls the Application.RefreshTitleBar method to refresh the Access title bar.

The GetTitleBar method looks similar to the following code.

This procedure turns off error handling, and then attempts to retrieve the AppTitle property of the current database. If the property does not exist, the attempt to retrieve it returns an error. The code checks for the error, and either returns the value of the AppTitle property or the standard title, "Microsoft Access", if could not retrieve the AppTitle property.

The tricky code occurs in the two private procedures. For example, the SetDBProperty procedure contains the following code.

To do its job, the procedure attempts to set the value of a database property.

If the attempt succeeds, the procedure returns True. If the attempt fails, the code jumps to the procedure's error handler.

If the error number indicates that a particular property was not found, the code calls the CreateDBProperty method, passing in the property name, type, and value. If a different error occurred, the procedure fails.

The CreateDBProperty method contains the following code.

Basically, the procedure calls the CreateProperty method of the database, specifying the property name, type, and value. It then appends the new property to the Properties collection, and refreshes the collection.

Although it takes a bit of effort to create the necessary AppTitle property in a new database, once you have the basCaption module in your application, it is simple to let the code do the work for you.

If you want to call the SetDBProperty and CreateDBProperty methods yourself, note that each procedure requires you to supply a property type. When you specify that parameter, use one of the DAO.DataTypeEnum values, such as dbText, dbDate, dbDecimal, and so on. Use the Object Browser in the VBA editor to help you find the appropriate value.

Sub ListProperties() OnErrorResumeNextDim prp As DAO.PropertyForEach prp In CurrentDb.Properties Debug.Print prp.Name, Debug.Print prp.Value, Debug.Print Next prp EndSub
PublicSub SetTitleBar(varTitle AsVariant) SetDBProperty "AppTitle", dbText, varTitle & ""' If you want the titlebar to change right now,' refresh it. Application.RefreshTitleBar EndSub
PublicFunction GetTitleBar() AsStringDim strTitle AsStringDim db As DAO.Database Set db = CurrentDb() ' Retrieve the current databases's AppTitle property.OnErrorResumeNext strTitle = db.Properties("AppTitle") ' Doesn't exist? Return standard title:If Err.Number <> 0 Then GetTitleBar = "Microsoft Access"Else GetTitleBar = strTitle EndIfEndFunction
PrivateSub CreateDBProperty( _ strProperty AsString, _ intType As DataTypeEnum, _ varSetting AsVariant) OnErrorGoTo HandleErr Dim db As Database Set db = CurrentDb() ' Create property, denote type, and set initial value.Dim prp As DAO.PropertySet prp = db.CreateProperty( _ strProperty, intType, varSetting) ' Append Property object to Properties collection. db.Properties.Append prp db.Properties.Refresh ExitHere: ExitSub HandleErr: MsgBox Err.Number & ": " & vbCrLf & Err.Description Resume ExitHere EndSub
db.Properties(strProperty) = varSetting db.Properties.Refresh
If Err.Number = 3270 ThenCall CreateDBProperty(strProperty, intType, varSetting) SetDBProperty = TrueElse MsgBox Err.Number & ": " & vbCrLf & Err.Description SetDBProperty = FalseEndIf
PrivateSub CreateDBProperty( _ strProperty AsString, _ intType As DataTypeEnum, _ varSetting AsVariant) OnErrorGoTo HandleErr Dim db As Database Set db = CurrentDb() ' Create property, denote type, and set initial value.Dim prp As DAO.PropertySet prp = db.CreateProperty( _ strProperty, intType, varSetting) ' Append Property object to Properties collection. db.Properties.Append prp db.Properties.Refresh ExitHere: ExitSub HandleErr: MsgBox Err.Number & ": " & vbCrLf & Err.Description Resume ExitHere EndSub
' Create property, denote type, and set initial value.Dim prp As DAO.PropertySet prp = db.CreateProperty( _ strProperty, intType, varSetting) ' Append Property object to Properties collection. db.Properties.Append prp db.Properties.Refresh

By: Geertjan Wielenga| Product Manager

There are many ways to customize a NetBeans Platform application's title in the title bar. Below, I try to list all of them, together with all related references. One typical added requirement is that the title should change dynamically, upon selection of something different in the application. For example, in the IDE the current project's name appears in the IDE's title bar. That's also quite easy to do.import org.openide.modules.ModuleInstall;
public class Installer extends ModuleInstall {
@Override
public void restored() {
System.setProperty("netbeans.buildnumber", "");
}
}

The above comes from an interview with Emilian Bold. The most often heard solution is to go here in an application's branding folder, in the Files window in the IDE:

In the above file you'll find keys/value like this:

CTL_MainWindow_Title=DemoApp {0}
CTL_MainWindow_Title_No_Project=DemoApp {0}

Simply remove the {0} at the end of the values and you'll not see a build number in the application.

Another approach is to grab the application's frame and change its title:

public class Installer extends ModuleInstall {
@Override
public void restored() {
WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
public void run() {
JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
String title = NbBundle.getMessage(Installer.class, "LBL_TITLE");
frame.setTitle(title);
}
});
}
}

The code above could be anywhere at all, not necessarily in the Installer class. Note that above we're referring to a bundle file that is in the same package as our class. In that bundle file, the value of the key "LBL_TITLE" (or whatever the name of your key is) sets the title of the application. This means you could have many different titles in the bundle and then dynamically switch them at runtime from the code, as done above.

However, there's more you can do with bundle files, as the code completion for indicates:

So, based on the current selection, you can pass something into the argument that will tweak the title of the title bar, as shown below. In this case, a listener is set on an Explorer Manager so that the currently selected node in an explorer view determines the content of the title bar:

em.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(final PropertyChangeEvent evt) {
WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
public void run() {
JFrame frame = (JFrame) WindowManager.getDefault().getMainWindow();
String title = NbBundle.getMessage(DemoTopComponent.class, "LBL_TITLE", em.getSelectedNodes()[0].getDisplayName());
frame.setTitle(title);
}
});
}
});

In the file, which is in the same package as where the above class is found, I have the following entry:

LBL_TITLE=Selected: {0}

The {0} is a placeholder for the first additional argument sent by the in my Java code above. Now, whenever I choose a new node in the explorer view, the title changes in the title bar, as indicated below:

Up to 4 different objects can be passed into the argument of , only the first of which must be a string. The others could be any object, even an array of objects. For example, it could be like this in the file:

LBL_TITLE=Selected: {0} {1} {2} {3} {4}

The whole value above is a string, so you could put other strings in between the 4 placeholders, such as shown here with some random characters thrown in:

LBL_TITLE=Selected: {0} -- {1} / {2} and {3} --- {4}

This is not useful only in the context of title bars, but is a good example of its applicability in that case.

Two other useful pieces for working with the title bar are Netbeans Platform Branding and version info and Branding custom version info into NetBeans RCP apps and the missing Bundle.Properties file.

One thought on “The Bibliography Change Title Bar

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *