Example usage for javax.swing JPanel setOpaque

List of usage examples for javax.swing JPanel setOpaque

Introduction

In this page you can find the example usage for javax.swing JPanel setOpaque.

Prototype

@BeanProperty(expert = true, description = "The component's opacity")
public void setOpaque(boolean isOpaque) 

Source Link

Document

If true the component paints every pixel within its bounds.

Usage

From source file:Main.java

public static void main(String... args) throws Exception {
    JPanel panel = new JPanel();
    panel.setOpaque(true);
    panel.setBackground(Color.RED);

    java.net.URL url = new java.net.URL("http://www.java2s.com/style/download.png");
    ImageIcon image = new ImageIcon(url);
    JLabel label = new JLabel("LABEL", image, JLabel.RIGHT);
    panel.add(label);/*  w  w  w .  ja v  a2 s  .c o  m*/

    JOptionPane.showMessageDialog(null, panel, "Modified JOptionPane : ", JOptionPane.PLAIN_MESSAGE);
}

From source file:Main.java

public static void main(String... args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel contentPane = new JPanel();
    contentPane.setOpaque(true);
    contentPane.setBackground(Color.WHITE);
    contentPane.setLayout(null);/* w  w  w .j a v a 2s .com*/

    JLabel label = new JLabel("This JPanel uses Absolute Positioning", JLabel.CENTER);
    label.setSize(300, 30);
    label.setLocation(5, 5);

    JButton button = new JButton("USELESS");
    button.setSize(100, 30);
    button.setLocation(95, 45);

    contentPane.add(label);
    contentPane.add(button);

    frame.setContentPane(contentPane);
    frame.setSize(310, 125);
    frame.setLocationByPlatform(true);
    frame.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String[] columns = { "Name", "Age" };

    Object[][] content = { { "R", new Integer(24) }, { "A", new Integer(25) }, { "J", new Integer(30) },
            { "A", new Integer(32) }, { "S", new Integer(27) } };

    JTable table = new JTable(content, columns);
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel jPanel = new JPanel(new GridLayout(2, 0));
    jPanel.setOpaque(true);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    jPanel.add(new JScrollPane(table));
    /* Add the panel to the JFrame */
    frame.add(jPanel);// w w  w.j  a  va2  s. com
    /* Display the JFrame window */
    frame.pack();
    frame.setVisible(true);

    table.print();
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel p = new JPanel();
    p.setOpaque(true);
    p.setLayout(new FlowLayout());
    p.add(good);/*from  w  w w .j  ava2s . c o  m*/

    f.add(p, BorderLayout.CENTER);
    f.add(resultLabel, BorderLayout.SOUTH);
    good.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ev) {
            resultLabel.setText("Working . . .");
            good.setEnabled(false);
            Thread worker = new Thread() {
                public void run() {
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException ex) {
                    }
                    SwingUtilities.invokeLater(new Runnable() {
                        public void run() {
                            resultLabel.setText("Ready");
                            good.setEnabled(true);
                        }
                    });
                }
            };
            worker.start(); // So we don't hold up the dispatch thread.
        }
    });
    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String... args) {
    JPanel contentPane;/*from  w  ww .j a  v  a 2s  . com*/
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    contentPane = new JPanel();
    contentPane.setLayout(new GridBagLayout());

    JPanel centerPanel = new JPanel();
    centerPanel.setOpaque(true);
    centerPanel.setBackground(Color.CYAN);

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.weightx = 1.0;
    gbc.weighty = 0.9;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    gbc.fill = GridBagConstraints.BOTH;

    contentPane.add(centerPanel, gbc);

    JButton leftButton = new JButton("Left");
    JButton rightButton = new JButton("Right");
    gbc.gridwidth = 1;
    gbc.gridy = 1;
    gbc.weightx = 0.3;
    gbc.weighty = 0.1;

    contentPane.add(leftButton, gbc);

    gbc.gridx = 1;
    gbc.weightx = 0.7;
    gbc.weighty = 0.1;

    contentPane.add(rightButton, gbc);

    frame.setContentPane(contentPane);
    frame.pack();
    frame.setLocationByPlatform(true);
    frame.setVisible(true);

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from   www  .j  av a2 s . c o m*/
        bg = ImageIO.read(new URL("http://www.java2s.com/style/download.png"));
    } catch (Exception ex) {
        System.out.println(ex);
    }

    JPanel tabPanel = new JPanel(new GridBagLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 300);
        }
    };
    JPanel buttons = new JPanel(new GridLayout(4, 1, 15, 15));
    buttons.setOpaque(false);
    for (int i = 0; i < 4; i++) {
        buttons.add(new JButton("Button"));
    }
    tabPanel.add(buttons);

    JTabbedPane tabPane = new JTabbedPane();
    tabPane.add("Panel with Bachground", tabPanel);

    JFrame frame = new JFrame();
    frame.setContentPane(tabPane);
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:Capture.java

public static void main(String[] args) {
    JFrame capture = new JFrame();
    capture.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Toolkit kit = Toolkit.getDefaultToolkit();
    final Dimension d = kit.getScreenSize();
    capture.setSize(d);/*  w  w w.jav a  2  s  .c  o m*/

    Rectangle rect = new Rectangle(d);
    try {
        Robot robot = new Robot();
        final BufferedImage image = robot.createScreenCapture(rect);
        image.flush();
        JPanel panel = new JPanel() {
            public void paintComponent(Graphics g) {
                g.drawImage(image, 0, 0, d.width, d.height, this);
            }
        };
        panel.setOpaque(false);
        panel.prepareImage(image, panel);
        panel.repaint();
        capture.getContentPane().add(panel);
    } catch (Exception e) {
        e.printStackTrace();
    }

    capture.setVisible(true);
}

From source file:Main.java

public static void setJPanelOpaque(JPanel p, boolean opaque) {
    p.setOpaque(opaque);
    for (int iComp = 0; iComp < p.getComponentCount(); iComp++) {
        if (p.getComponent(iComp) instanceof JPanel) {
            setJPanelOpaque((JPanel) p.getComponent(iComp), opaque);
        }// w  w  w  . j  a v  a2  s. c  om
    }
}

From source file:Main.java

/**
 * Put a series of {@link JComponent}s in a {@link JPanel} which has a {@link FlowLayout}.
 *
 * @param position/*from  w  w w.  j  a  va 2 s .  c om*/
 *         Position of components, left, right, or center justified (for example, {@link
 *         java.awt.FlowLayout#CENTER)}
 * @param opaque
 *         True if the returned JPanel should be opaque
 * @param firstComponent
 *         The first {@link JComponent} to add
 * @param remainingComponents
 *         The rest of the {@link JComponent}s to add
 * @return A {@link JPanel} with a {@link FlowLayout} which contains all the passed {@link JComponent}s.
 */
public static JPanel putComponentsInFlowLayoutPanel(int position, boolean opaque, JComponent firstComponent,
        JComponent... remainingComponents) {
    JPanel flowLayoutPanel = new JPanel(new FlowLayout(position));
    flowLayoutPanel.setOpaque(opaque);
    flowLayoutPanel.add(firstComponent);
    for (JComponent component : remainingComponents) {
        flowLayoutPanel.add(component);
    }
    return flowLayoutPanel;
}

From source file:Main.java

public static void createAndShowGUI(final String titile, final JPanel pane) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame(titile);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            pane.setOpaque(true);
            frame.setContentPane(pane);/*from  w  ww . jav  a  2 s  . c o m*/
            frame.pack();
            // frame.setSize(width, height);
            frame.setVisible(true);
            moveToCenter(frame);
        }
    });

}