Java/GWT/CSS

Материал из Java эксперт
Перейти к: навигация, поиск

Link control with CSS

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class GWTClient implements EntryPoint{
   public void onModuleLoad() {
    final Label loginPrompt = new Label("Please Log In");
    final Grid grid = new Grid(3, 2);
    final Label namePrompt = new Label("Name");
    final TextBox nameTextbox = new TextBox();
    final Label passwordPrompt = new Label("Password:");
    final PasswordTextBox passwordTextbox = new PasswordTextBox();
    final Button button = new Button("Login");
    RootPanel.get().clear();
    loginPrompt.addStyleName("loginPrompt");
    loginPrompt.addStyleName("loginPrompt2");    
    nameTextbox.addStyleName("nameField");
    passwordTextbox.addStyleName("passwordField");
    grid.setWidget(0, 0, namePrompt);
    grid.setWidget(0, 1, nameTextbox);
    grid.setWidget(1, 0, passwordPrompt);
    grid.setWidget(1, 1, passwordTextbox);
    grid.setWidget(2, 1, button);
    
    RootPanel.get().add(loginPrompt);
    RootPanel.get().add(grid);
  }
}





Returns a List of Element objects that have the specified CSS class name

/*
 * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.ru>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.gwtwidgets.client.util;
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
/**
 * 
 * @author rhanson
 */
public class SearchUtils
{
    /**
     * Returns a List of Element objects that have the specified CSS class name.
     * 
     * @param element Element to start search from
     * @param className name of class to find
     * @return
     */
    public static List findElementsForClass (Element element, String className)
    {
        ArrayList result = new ArrayList();
        recElementsForClass(result, element, className);
        return result;
    }
    private static void recElementsForClass (ArrayList res, Element element, String className)
    {
        String c;
        
        if (element == null) {
            return;
        }
        c = DOM.getAttribute(element, "className");
        
        if (c != null) {
            String[] p = c.split(" ");
            
            for (int x = 0; x < p.length; x++) {
                if (p[x].equals(className)) {
                    res.add(element);
                }
            }
        }
        
        for (int i = 0; i < DOM.getChildCount(element); i++) {
            Element child = DOM.getChild(element, i);
            recElementsForClass(res, child, className);
        }
    }
    
}





Use Default StyleSheet

package com.jexp.gwt.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
public class GWTClient implements EntryPoint {
  public void onModuleLoad() {
    Button b = new Button("Click me", new ClickListener() {
      public void onClick(Widget sender) {
         DialogBox dlg = new MyDialog();
         dlg.center();
      }
    });
    RootPanel.get().add(b);
  }
}
class MyDialog extends DialogBox implements ClickListener {
  public MyDialog() {
    setText("Sample DialogBox");
    Button closeButton = new Button("Close", this);
    HTML msg = new HTML("<center>A standard dialog box component.</center>",true);
    DockPanel dock = new DockPanel();
    dock.setSpacing(4);
    dock.add(closeButton, DockPanel.SOUTH);
    dock.add(msg, DockPanel.NORTH);
    dock.add(new Image("images/yourImage.jpg"), DockPanel.CENTER);
    dock.setCellHorizontalAlignment(closeButton, DockPanel.ALIGN_RIGHT);
    dock.setWidth("100%");
    setWidget(dock);
  }
  public void onClick(Widget sender) {
    hide();
  }
}
//Your.css
.gwt-DialogBox {
  border: 2px outset;
  background-color: white;
}

//GWTClient.gwt.xml
<module>
  <inherits name="com.google.gwt.user.User"/>
  <entry-point class="com.jexp.gwt.client.GWTClient"/>
  <stylesheet src="your.css"/>
</module>