如何获取JSpinner最小值和最大值以设置随机值

时间:2016-06-03 21:33:39

标签: java swing jspinner

我有一个函数,它将使用JSpinner.setValue()在微调器的范围内分配一个随机值。当然,我需要知道微调器的最小值和最大值,这些值已经分配。此时,我在整个微调器中循环以确定这些值。有没有更好的方法来找到它们?像

这样的东西
DECLARE @tblProducts TABLE  
    (
    ProductName varchar(100)
    , SalesYear INT
    , Revenue MONEY
    )

INSERT INTO @tblProducts
SELECT 'Toys'        ,2012       ,1000
UNION SELECT 'Toys'       , 2013       ,1500
UNION SELECT 'Toys'      ,  2014       ,3000
UNION SELECT 'Chocolates',  2012       ,1000
UNION SELECT 'Chocolates',  2013        ,900
UNION SELECT 'Chocolates',  2014       ,1500
UNION SELECT 'PlayingKit',  2012       ,3000
UNION SELECT 'PlayingKit',  2013       ,1500
UNION SELECT 'PlayingKit',  2014       ,5000

;WITH CTE_IsGrowthYear
AS
(
    --Anchor 
    SELECT 
        *,
        1 As IsGrowth
        FROM @tblProducts P
        WHERE P.SalesYear = 
            (SELECT MIN(SalesYear) FROM @tblProducts P2 WHERE P2.ProductName = P.ProductName)  --NOTE:  First year is always a growth year
    UNION ALL
    --Recursive
    SELECT P.*,
        1 As IsGrowth
        FROM CTE_IsGrowthYear IG
            INNER JOIN @tblProducts P
                ON IG.ProductName = P.ProductName
            WHERE IG.SalesYear = P.SalesYear - 1  --Next year after anchor or recursive year
            AND P.Revenue > IG.Revenue  --Did revenue grow in subsequent year?
)
SELECT 
    C.ProductName
    , T.CountYears --Years with recurring growth, every single year
    FROM CTE_IsGrowthYear C
        LEFT OUTER JOIN 
            (
                SELECT P.ProductName, 
                    COUNT(DISTINCT SalesYear) as CountYears  --How many years are required for growth
                    FROM @tblProducts P
                    GROUP BY P.ProductName
            ) T --Total Number of Years             
            ON C.ProductName = T.ProductName
    GROUP BY C.ProductName, T.CountYears
    HAVING SUM(C.IsGrowth) = T.CountYears  --How many years had actual, recurring growth

我已经阅读了类似的问题,包括这个问题:JSpinner get Max Min Value,但它没有回答我的问题。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

我假设你在谈论数值。在这种情况下,您应该使用NumberSpinnerModel - 此类已经有您请求的方法:getMaximum()getMinimum()

JSpinner.getModel()会返回SpinnerModel,因此您必须将其投放到SpinnerNumberModel。如果你真的在你的微调器中使用这个模型,那么这是当然的 - 否则会失败。

答案 1 :(得分:0)

这里是从JSpinner获取最小/最大值的示例,其中JSpinner的最小/最大值已设置为Double值。

// Get Minimum and Maximum values from jSpinner's Model 
    double max = (Double) ((SpinnerNumberModel)    jSpinner.getModel()).getMaximum();//MAX_ALLOWED
    double min = (Double) ((SpinnerNumberModel) jSpinner.getModel()).getMinimum();//LOWEST_ALLOWED