Lily S
Lily S

Reputation: 245

How to display frame only when required & set frame to appear on top all the time?

My program will alert users when data (stock name & price) in the database matches with the data (stock name and price) from Yahoo Finance. With the help of HarryJoy i'm able to implement a pop up notification.

Problem is the pop up location on the screen is based on the database loop (int db). The frame will pop even if data doesn't match.

How can I set a counter or some method to pop up the frame only when database data = Yahoo Finance data? Also, my notification panels appears to be behind the main frame. How can make the panels appear on top of my main frame?

I hope I've given enough information, please ask if still unclear. Any guidance will be appreciated! Thanks!

In the database (using code sample only to display my explanation in order, source code below).

Object 1 = match
Object 2 = doesn't match (hence there's a gap)
Object 3 = match
Object 4 = match

Screenshot:

PopUp

Code:

 for (int db=0; db<= rowCount; db++) 
    {                   
        Object popSymbol = table.getModel().getValueAt(db, 0); 
        String popupSymbol = popSymbol.toString(); 

        Object popValue = table.getModel().getValueAt(db, 1);                          
        String str = popValue.toString(); 
        double popStockValue = Double.valueOf(str).doubleValue(); 

        String stockNameDB = popupSymbol;       
        StockPara stock = YahooStock.getInstance().getStockPrice(stockNameDB);
        double stockPriceDB = Math.round(stock.getPrice()); 

        final JFrame popUpFrame;
        final JPanel popupPanel;                    

        if (stockPriceDB == popStockValue)                      
        {
            String header = "Stock: "  + stock.getTicker() + " is now @ " +  stock.getPrice();          
            String message = "";  

            popUpFrame = new JFrame();
            popUpFrame.setSize(320,90);                         
            popUpFrame.setUndecorated(true);                                    
            popUpFrame.getContentPane().setLayout(null);    

            popupPanel = new JPanel();
            popupPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
            popupPanel.setBounds(0, 0, 320, 90);
            getContentPane().add(popupPanel);
            popupPanel.setBackground(new Color(255, 255, 224));
            popupPanel.setLayout(null);
            popUpFrame.add(popupPanel);                 

            // Other labels, images, etc. 

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Insets toolHeight = Toolkit.getDefaultToolkit().getScreenInsets(popUpFrame.getGraphicsConfiguration());
            popUpFrame.setLocation(screenSize.width - popUpFrame.getWidth(), screenSize.height - toolHeight.bottom - (popUpFrame.getHeight() * (db+1)));

        } 
    }

Upvotes: 1

Views: 298

Answers (2)

Guillaume Polet
Guillaume Polet

Reputation: 47608

To solve your issue quickly, simply create an additional counter (int counter = 0;). Then use that counter instead of db to position your popUpFrame and right after that increment counter.

int counter = 0; // Initiate outside de loop
...
for(int db=0; db<= rowCount; db++) {
    ...
    if (stockPriceDB == popStockValue) {
         ...
         popUpFrame.setLocation(screenSize.width - popUpFrame.getWidth(), 
            screenSize.height - toolHeight.bottom - (popUpFrame.getHeight() * (counter+1)));
         counter++;
    }
}

To bring your frame to the front use:

popUpFrame.toFront()

and to force it to be always on top (although I would really not recommend that because it is more annoying for the user than anything else):

popUpFrame.setAlwaysOnTop(true);

This is not necessarily supported by all platforms.

Upvotes: 1

11684
11684

Reputation: 7507

for on top issue, use

    notificationPanel.requestFocus();

Upvotes: 0

Related Questions