Beranda > Tips Java-Netbeans > Membuat Tabbed Pane Seperti Netbeans

Membuat Tabbed Pane Seperti Netbeans

wah, setelah cari sana-sini, search gunakan google. untuk mencari gimana caranya membuat Tabbed Pane dialog seperti yang ada di NetBeans 6.5, akhirnya dapat juga. Eh malahan adanya di Tutorial Netbeans🙂

Ok gini nih Tabbed Pane ala Netbeans (abis gak tau istilahnya)

tab

Hahahaha. pake tombol x untuk close kan tab. keren kan.

Ok gini caranya

1. Buat Project Baru (tentunya Netbeans)
2. Buat 1 (Satu) java class dengan nama ButtonTabComponent
3. Copy + Paste kan coding dibawah

import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import java.awt.*;
import java.awt.event.*;

/**
 * Component to be used as tabComponent;
 * Contains a JLabel to show the text and
 * a JButton to close the tab it belongs to
 */
public class ButtonTabComponent extends JPanel {
    private final JTabbedPane pane;

    public ButtonTabComponent(final JTabbedPane pane) {
        //unset default FlowLayout’ gaps
        super(new FlowLayout(FlowLayout.LEFT, 0, 0));
        if (pane == null) {
            throw new NullPointerException(“TabbedPane is null”);
        }
        this.pane = pane;
        setOpaque(false);
       
        //make JLabel read titles from JTabbedPane
        JLabel label = new JLabel() {
            public String getText() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                }
                return null;
            }
        };
       
        add(label);
        //add more space between the label and the button
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        //tab button
        JButton button = new TabButton();
        add(button);
        //add more space to the top of the component
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
    }

    private class TabButton extends JButton implements ActionListener {
        public TabButton() {
            int size = 17;
            setPreferredSize(new Dimension(size, size));
            setToolTipText(“close this tab”);
            //Make the button looks the same for all Laf’s
            setUI(new BasicButtonUI());
            //Make it transparent
            setContentAreaFilled(false);
            //No need to be focusable
            setFocusable(false);
            setBorder(BorderFactory.createEtchedBorder());
            setBorderPainted(false);
            //Making nice rollover effect
            //we use the same listener for all buttons
            addMouseListener(buttonMouseListener);
            setRolloverEnabled(true);
            //Close the proper tab by clicking the button
            addActionListener(this);
        }

        public void actionPerformed(ActionEvent e) {
            int i = pane.indexOfTabComponent(ButtonTabComponent.this);
            if (i != -1) {
                pane.remove(i);
            }
        }

        //we don’t want to update UI for this button
        public void updateUI() {
        }

        //paint the cross
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g.create();
            //shift the image for pressed buttons
            if (getModel().isPressed()) {
                g2.translate(1, 1);
            }
            g2.setStroke(new BasicStroke(2));
            g2.setColor(Color.BLACK);
            if (getModel().isRollover()) {
                g2.setColor(Color.MAGENTA);
            }
            int delta = 6;
            g2.drawLine(delta, delta, getWidth() – delta – 1, getHeight() – delta – 1);
            g2.drawLine(getWidth() – delta – 1, delta, delta, getHeight() – delta – 1);
            g2.dispose();
        }
    }

    private final static MouseListener buttonMouseListener = new MouseAdapter() {
        public void mouseEntered(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(true);
            }
        }

        public void mouseExited(MouseEvent e) {
            Component component = e.getComponent();
            if (component instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) component;
                button.setBorderPainted(false);
            }
        }
    };
}

4. Tambahkan 1(satu) buah JFrame
5. Tambahkan juga component Swing JTabbed Pane diberi nama pane dan satu buah Button dengan nama tambahTab
6. Pada Tombol Action (bole pake Swing.Action atau juga Klik Dua kali pada jButton Anda) tambahkan code dibawah

        int nTab;

        nTab = pane.getTabCount();

        String title = “Tab ” + nTab;
        pane.add(title, new JLabel(title));

        pane.setTabComponentAt(nTab, new ButtonTabComponent(pane));

        pane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        pane.setSelectedIndex(nTab);

7. Sudah selesai, tinggal dijalankan.

untuk sementara codingnya di copy pass aja. soalnya saya belum bisa mengupload kan source code.
tapi nanti kalo da bisa akan saya upload.

  1. Februari 6, 2009 pukul 16:54

    mantap🙂

  2. e12x
    Februari 8, 2009 pukul 02:54

    🙂 keren abis…

  3. Februari 8, 2009 pukul 06:17

    Thx, atas commentnya. wah makin semangat nih buat posting-posting. hahahaa……

  4. Andre
    Juli 14, 2009 pukul 08:48

    intinya cukup tambahkan sebuah panel pada sebuah posisi yang ditentukan di sebuah JTabbledPane khan ?

  1. No trackbacks yet.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: