I have a program where my JFrame contains a tabbed pane with 3 tabs, I am wanting to make it so that when tab1/jpanel1 is doubled clicked it displays the contents of the JPanel on the second monitor. In my case the second monitor will be a large plasma screen (42 inch).

I'm not quite sure what I need to read up on or the keywords I should be focusing on, so if anyone knows where I should begin or the keywords that would lead me to what I'm looking for I would appreciate the insight.

Recommended Answers

All 6 Replies

Take a look at the GraphicsEnvironment API. You can get the available screen devices with the following

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screens = ge.getScreenDevices();

The GraphicsDevice class has a multi-screen example in the class documentation.

I feel like I'm a bit over my head on this one. I have been reading the documentation for a good 45 minutes and can't make a whole lot of sense of it.

My assumptions at this moment is that I want to get screen devices and pick which one I am wanting to display on the second monitor? If that is so, I must be doing something wrong because I get every GraphicDevice and throw it onto a JFrame and each of them are blank. (I was testing out the example they provided to see what the results would be)

Can you or anyone who might understand this area perhaps explain how it works a bit? I have been searching the net for the past 30 minutes and not finding much. I have been searching: GraphicsEnvironment, GraphicsDevice, and GraphicsConfiguration. The problem is that the only pages that come up are the API which use terminology I'm not familiar with. Which then leads me to look up the terminology and I can't seem to find any good definitions for some of the terms. (It is kind of a big circle of searching google at this point)

I have multiple questions, but I will hold off on them once I get a solid starting point.

I'll continue reading up on it, thank you for pointing me in the right direction Ezzaral

It depends somewhat on how your video card(s) are representing the screen devices to the system. You can get the bounds of a GraphicsConfiguration to determine where it is in your graphics space. If your monitors are returned as separate devices, you can figure out where the bounds of each are and where you need to put your components.

On my current system with three monitors, getScreenDevices() returns a single device with bounds that span the total area of all three - so to position something on the third one all I can really do is figure x0=2*width/3. Your system may be different and return each monitor as a separate device, each with it's own bounds.

It will likely take you a bit of experimentation and examination of what info your system is returning to nail down exactly how to isolate your target area.

commented: Glad to see there are people around here who are willing to explain things that are difficult to comprehend from the documentation. It is members like you that make communties/forums an enjoyable place. Thanks bud +1

Ah alright that makes more sense, thank you for explaining that. I'll have to do a bit of testing to see how it splits the screen devices (if it does at all).

My computer looks like it also puts both my monitor as a single screen device. Which might be an issue later on, since I want to make the jPanel take up the entire second monitor and none of the default monitor.

As for the other questions:

  • How would I grab the information located on the jPanel that I want to display on the second monitor? (is there a quick easy way to duplicate the panel)
  • Would I encounter issues with the fonts? (Seeing as the second monitor will be much larger than the original one)

>How would I grab the information located on the jPanel that I want to display on the second monitor? (is there a quick easy way to duplicate the panel)
I've put together a small example of how you might do that below. Perhaps it will give you a starting point.

>Would I encounter issues with the fonts? (Seeing as the second monitor will be much larger than the original one)
Yes, if you're using an enlarged image you will have pixellation effects.

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class PanelCopyTest extends javax.swing.JFrame {
    /** Mirror image panel - see class def below */
    ImagePanel imgPanel;

    public PanelCopyTest() {
        initComponents();
    }

    /** blah blah, ui setup */
    private void initComponents() {

        panMain = new javax.swing.JPanel();
        txt = new javax.swing.JTextField();
        btnCopy = new javax.swing.JButton();
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        panMain.setLayout(new java.awt.BorderLayout());

        txt.setText("Stuff");
        panMain.add(txt, java.awt.BorderLayout.CENTER);

        btnCopy.setText("Copy");
        btnCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCopyActionPerformed(evt);
            }
        });
        panMain.add(btnCopy, java.awt.BorderLayout.SOUTH);

        getContentPane().add(panMain, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>

     /** This is where the relevant stuff occurs */
    private void btnCopyActionPerformed(java.awt.event.ActionEvent evt) {
        if (imgPanel==null){
            // first time set up the mirror panel
            imgPanel = new ImagePanel();
            JFrame f = new JFrame();
            f.add(imgPanel);
            f.setBounds(0,0,getWidth(), getHeight());
            f.setVisible(true);
        }
        // create a buffered image to draw the source panel onto
        BufferedImage imgCopy = new BufferedImage(panMain.getWidth(), 
                panMain.getHeight(), BufferedImage.TYPE_INT_RGB);
        // paint the source panel onto the image 
        panMain.paintComponents(imgCopy.getGraphics());
        // push it to the mirroring panel
        imgPanel.setImage(imgCopy);
        imgPanel.repaint();
    }

    /** Simple panel that displays an image */
    class ImagePanel extends JPanel {
        Image img;

        public void setImage(Image img){
            this.img=img;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img!=null) {
                g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
            }
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PanelCopyTest().setVisible(true);
            }
        });
    }

    private javax.swing.JButton btnCopy;
    private javax.swing.JPanel panMain;
    private javax.swing.JTextField txt;
}

The first time you click "copy" it will create a mirror panel with the current contents of the main frame. Subsequent clicks of "copy" will replace the mirror image with a snapshot.

Thank you Ezzaral! You've been most helpful. I am very grateful that you commented your code. With a few simple modifications, this code can do exactly what I'll want it to do.

For the font issues, I might have to throw some changes in when the tab/panel is clicked.

I'll have to give this a test run when I get the chance (my desktop that I was doing the coding on crashed this past weeked)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.