java使用jgoodies_java – JGoodies HashMap

博主在构建图表程序中遇到问题,试图使用JGoodies进行数据绑定,但不清楚如何将HashMap上的数据绑定到数据系列模型。DataSeriesModel包含HashMap数据点,通过PropertyChangeSupport实现属性变更通知。DataSeriesViewModel作为JGoodies的PresentationModel,包装DataSeriesModel。DataSeries是抽象类,扩展JComponent,用于创建不同视图。目前的问题是如何在BuilderDataSeries中,当HashMap中的数据点发生变化时,同步更新BuilderDataPoint组件。
摘要由CSDN通过智能技术生成

我正在尝试使用演示模型构建图表程序.对于像字符串或数字这样的简单类型,使用JGoodies进行数据绑定相对容易.但我无法弄清楚如何在hashmap上使用它.

我将尝试解释图表的工作原理以及我的问题是什么:

图表由DataSeries组成,DataSeries由DataPoints组成.我想拥有一个数据模型,并能够在同一模型上使用不同的视图(例如条形图,饼图,……).它们每个都包含三个类.

例如:

DataPointModel:保存数据模型(值,标签,类别)

DataPointViewModel:扩展JGoodies PresentationModel.包装DataPointModel并保存视图属性,如字体和颜色.

DataPoint:抽象类,扩展JComponent.不同的视图必须子类化并实现自己的ui.

绑定和创建数据模型很简单,但我不知道如何绑定我的数据系列模型.

package at.onscreen.chart;

import java.beans.PropertyChangeListener;

import java.beans.PropertyChangeSupport;

import java.beans.PropertyVetoException;

import java.util.Collection;

import java.util.HashMap;

import java.util.Iterator;

public class DataSeriesModel {

public static String PROPERTY_DATAPOINT = "dataPoint";

public static String PROPERTY_DATAPOINTS = "dataPoints";

public static String PROPERTY_LABEL = "label";

public static String PROPERTY_MAXVALUE = "maxValue";

/**

* holds the data points

*/

private HashMap dataPoints;

/**

* the label for the data series

*/

private String label;

/**

* the maximum data point value

*/

private Double maxValue;

/**

* the model supports property change notification

*/

private PropertyChangeSupport propertyChangeSupport;

/**

* default constructor

*/

public DataSeriesModel() {

this.maxValue = Double.valueOf(0);

this.dataPoints = new HashMap();

this.propertyChangeSupport = new PropertyChangeSupport(this);

}

/**

* constructor

* @param label - the series label

*/

public DataSeriesModel(String label) {

this.dataPoints = new HashMap();

this.maxValue = Double.valueOf(0);

this.label = label;

this.propertyChangeSupport = new PropertyChangeSupport(this);

}

/**

* full constructor

* @param label - the series label

* @param dataPoints - an array of data points

*/

public DataSeriesModel(String label, DataPoint[] dataPoints) {

this.dataPoints = new HashMap();

this.propertyChangeSupport = new PropertyChangeSupport(this);

this.maxValue = Double.valueOf(0);

this.label = label;

for (int i = 0; i < dataPoints.length; i++) {

this.addDataPoint(dataPoints[i]);

}

}

/**

* full constructor

* @param label - the series label

* @param dataPoints - a collection of data points

*/

public DataSeriesModel(String label, Collection dataPoints) {

this.dataPoints = new HashMap();

this.propertyChangeSupport = new PropertyChangeSupport(this);

this.maxValue = Double.valueOf(0);

this.label = label;

for (Iterator it = dataPoints.iterator(); it.hasNext();) {

this.addDataPoint(it.next());

}

}

/**

* adds a new data point to the series. if the series contains a data point with same id, it will be replaced by the new one.

* @param dataPoint - the data point

*/

public void addDataPoint(DataPoint dataPoint) {

String category = dataPoint.getCategory();

DataPoint oldDataPoint = this.getDataPoint(category);

this.dataPoints.put(category, dataPoint);

this.setMaxValue(Math.max(this.maxValue, dataPoint.getValue()));

this.propertyChangeSupport.firePropertyChange(PROPERTY_DATAPOINT, oldDataPoint, dataPoint);

}

/**

* returns the data point with given id or null if not found

* @param uid - the id of the data point

* @return the data point or null if there is no such point in the table

*/

public DataPoint getDataPoint(String category) {

return this.dataPoints.get(category);

}

/**

JGoodies 是一个 Java 库,它提供了一些工具和组件,用于创建美观和易于使用的界面。在 JGoodies 中,有一个名为 JGoodies Forms 的布局工具,它可以帮助您创建漂亮的网格布局。 下面是使用 JGoodies Forms 美化表格的步骤: 1. 导入 JGoodies 库 您可以从 JGoodies 官方网站(https://www.jgoodies.com)下载 JGoodies 库,并将其添加到您的项目中。 2. 创建表格模型 使用 JTable 类创建一个表格模型,并将其添加到 JScrollPane 中,以便用户可以滚动表格。 ```java JTable table = new JTable(data, columnNames); JScrollPane scrollPane = new JScrollPane(table); ``` 3. 创建表格布局 使用 JGoodies Forms 创建表格布局,并将表格模型添加到布局中。 ```java FormLayout layout = new FormLayout("fill:pref:grow", "fill:pref:grow"); CellConstraints cc = new CellConstraints(); JPanel panel = new JPanel(layout); panel.add(scrollPane, cc.xy(1, 1)); ``` 4. 设置表格样式 使用 JTable 类的 setXXX 方法设置表格的样式,例如: ```java table.setRowHeight(30); table.setFont(new Font("Arial", Font.PLAIN, 14)); table.getTableHeader().setFont(new Font("Arial", Font.BOLD, 14)); ``` 5. 显示表格 将表格布局添加到 JFrame、JDialog 或其他容器中,并显示它们。 ```java JFrame frame = new JFrame("Table with JGoodies"); frame.setContentPane(panel); frame.pack(); frame.setVisible(true); ``` 通过这些步骤,您可以使用 JGoodies Forms 美化表格,并使它们看起来更漂亮和易于使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值