📄 jpageelement.java
字号:
package com.component.pagination;
/*
* > I have a JFrame Window and a JLabel on it with a long text. The problem
> is that the text of the JLabel does not fit the size of the window -
> the value for the width of the window is to less. But I want not to
> make the window wider. How can I make it that the text of the JLabel is
> automatically divided when the end of the window width is reached and
> the remaining text will be placed in the next line?
That will not be easy with a JLabel. You an easily create a JLabel that
spans multiple lines (using HTML tags), but getting it to wrap is
something else.
Consider using one of the multi-line JTextComponent subclasses. You can
change the opacity, colors, font, and other properties to make it look the
same as the the JLabel.
*/
/*
* JLabel for output
Why using JLabel for output is usually bad
It's possible to change the text of a JLabel, although this is not generally a good idea after
the user interface is already displayed. For output JTextField is often a better choice.
The use of JLabel for output is mentioned because some textbooks display output this way.
Here are some reasons not to use it.
* Can't copy to clipboard. The user can not copy text from a JLabel, but can from a JTextField.
* Can't set background. Changing the background of individual components probably isn't a good
idea, so this restriction on JLabels is not serious. You can change the background of a
JTextField, for better or worse.
* Text length. This is where there are some serious issues. You can always see the entire text
in a JTextField, altho you might have to scroll it it's long. There are several
possibilities with a JLabel. You may either not see all of the long text in a JLabel,
or putting long text into a JLabel may cause the layout to be recomputed, resulting
in a truly weird user experience.
*/
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import org.jdesktop.swingx.JXPanel;
import com.component.Cab2bHyperlink;
import com.component.Cab2bLabel;
import com.component.WindowUtilities;
/**
* Each JPageElement should know its pageIndex(String) and index in that page.
* This two index is needed for Creating an PageSelectionEvent and throwing it to JPagination.
*
* JPagination can then maintain its SelectionModel properly.
*
* @author chetan_bh
*/
public class JPageElement extends JXPanel implements ActionListener, PropertyChangeListener{
/**
* An <code>Cab2bHyperlink</code> for this composite component.
*/
private Cab2bHyperlink hyperlink;
/**
* A <code>JCheckBox</code> for enabling selections on the page elements.
* displayed in a page panel.
*/
private JCheckBox checkBox;
/**
* A <code>JLabel</code> to display the description associated with each page elements.
*/
private Cab2bLabel descriptionLabel;
private PageElement pageElement;
/**
* A boolean to indicate whether this component is selectable or not.
*/
private boolean isSelectable;
/**
*
*/
private PageElementIndex elementIndex;
private JPagination pagination;
public JPageElement(JPagination pagination, PageElement pageElement, boolean isSelectable, PageElementIndex pageElementIndex)
{
this.pagination = pagination;
this.pageElement = pageElement;
this.isSelectable = isSelectable;
this.elementIndex = pageElementIndex;
initGUI();
initListeners();
//this.setPreferredSize(new Dimension(220,((int)(this.getPreferredSize().getHeight()+10))));
this.setPreferredSize(new Dimension(976,((int)(this.getPreferredSize().getHeight()+10))));
// or
//initGUIWithGridBagLayoutLayout();
}
private void initListeners()
{
if(pagination != null)
pagination.addPropertyChangeListener("isSelectable", this);
}
private void initGUI()
{
this.removeAll();
this.setLayout(new RiverLayout(0, 0));
//hyperlink = new JXHyperlink();
hyperlink = new Cab2bHyperlink();
hyperlink.setText(pageElement.getDisplayName());
hyperlink.setUserObject(pageElement); // getUserObject()
descriptionLabel = new Cab2bLabel(pageElement.getDescription());
if(isSelectable && pagination != null)
{
checkBox = new JCheckBox();
checkBox.addActionListener(this);
boolean isSelected = pagination.
getPageSelectionModel().
isSelectedIndex(elementIndex.getMainPageIndex(), //elementIndex.getPageIndex(),
elementIndex.getIndexInPage());
if(isSelected)
checkBox.setSelected(true);
this.add("br", checkBox);
}else
{
this.add("br" , new JLabel(" "));
}
this.add("tab", hyperlink);
this.add("br", new JLabel(" "));
this.add("tab", descriptionLabel);
}
private void initGUIWithGridBagLayoutLayout()
{
System.out.println("JPageElement :: initGUIWithGridBagLayoutLayout()");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
this.setLayout(gbl);
//hyperlink = new JXHyperlink();
hyperlink = new Cab2bHyperlink();
hyperlink.setText(pageElement.getDisplayName());
//hyperlink.setU
descriptionLabel = new Cab2bLabel(pageElement.getDescription());
if(isSelectable)
{
checkBox = new JCheckBox();
checkBox.addActionListener(this);
boolean isSelected = pagination.
getPageSelectionModel().
isSelectedIndex(elementIndex.getMainPageIndex(), //elementIndex.getPageIndex(),
elementIndex.getIndexInPage());
if(isSelected)
checkBox.setSelected(true);
constraints.gridx = 2;
constraints.gridy = 0;
gbl.addLayoutComponent(checkBox, constraints);
this.add(checkBox);
}else
{
JLabel dummyLabel = new JLabel(" ");
constraints.gridx = 2;
constraints.gridy = 0;
constraints.anchor = GridBagConstraints.LINE_START;
gbl.addLayoutComponent(dummyLabel, constraints);
this.add(dummyLabel);
}
constraints.gridx = 3;
constraints.gridy = 0;
constraints.gridheight = 1;
constraints.gridwidth = 7;
constraints.anchor = GridBagConstraints.LINE_START;
gbl.addLayoutComponent(hyperlink, constraints);
this.add(hyperlink);
JLabel dummyLabel = new JLabel(" ");
constraints.gridx = 2;
constraints.gridy = 1;
constraints.weightx = 0;
gbl.addLayoutComponent(dummyLabel, constraints);
this.add(dummyLabel );
constraints.gridx = 3;
constraints.gridy = 1;
constraints.gridheight = 1;
constraints.gridwidth = 7;
constraints.weightx = 0.7;
gbl.addLayoutComponent(descriptionLabel, constraints);
this.add(descriptionLabel);
}
/**
* Returns true if this is selected, else false.
* @return true if selected
*/
public boolean isSelected()
{
return checkBox.isSelected();
}
/**
* Capture events happening on JPageElements CheckBox
* Create a PageSelectionEvent and fire an selectionChangedEvent to JPagination.
*/
public void actionPerformed(ActionEvent e) {
PageSelectionEvent selectionEvent = new PageSelectionEvent(this, elementIndex);
pagination.fireSelectionValueChanged(selectionEvent);
}
public void setHyperlinkActionListener(ActionListener ae)
{
hyperlink.addActionListener(ae);
}
/**
* Property Change Listener, listens for specific changes in JPagination property change like "isSelectable" property.
*/
public void propertyChange(PropertyChangeEvent propChangeEvent)
{
String propertyName = propChangeEvent.getPropertyName();
Object newValue = propChangeEvent.getNewValue();
if(propertyName.equals("isSelectable"))
{
Boolean newBoolValue = (Boolean)newValue;
if(newBoolValue)
{
isSelectable = newBoolValue;
initGUI();
this.revalidate();
}else
{
isSelectable = newBoolValue;
this.remove(checkBox);
this.revalidate();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
PageElement pageElement = new PageElementImpl();
pageElement.setDisplayName("Participant");
pageElement.setDescription("Participant is a person participating in a Tissue Collection Protocol");
JPagination pagination = new JPagination();
JPageElement pageElementPanel1 = new JPageElement(pagination, pageElement, true, new PageElementIndex("1",1));
JPageElement pageElementPanel2 = new JPageElement(pagination, pageElement, false, new PageElementIndex("1",2));
WindowUtilities.showInFrame(pageElementPanel1, "");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -