Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created March 5, 2013 18:02
Show Gist options
  • Save resarahadian/5092519 to your computer and use it in GitHub Desktop.
Save resarahadian/5092519 to your computer and use it in GitHub Desktop.
Image Header Column Table
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
@SuppressWarnings("serial")
public class FrameTabel extends JFrame
{
private JPanel contentPane;
private JTable tabel;
DefaultTableModel tabelModel;
String pelanggan[] = {"ID","Nama","Alamat"};
String dataPlg[][] = {{"001","Resa","Yogyakarta"},{"002","Candra","Klaten"},{"003","Ayu","Jakarta"},{"004","Dita","Purwokerto"}};
ImageIcon imgIcon;
private JButton btnInformasiPelanggan;
/**
* Create the frame.
*/
public FrameTabel()
{
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 625, 380);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
setLocationRelativeTo(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(44, 12, 510, 221);
contentPane.add(scrollPane);
tabelModel = new DefaultTableModel(dataPlg,pelanggan);
tabel = new JTable();
tabel.setModel(tabelModel);
imgIcon = new ImageIcon("src/Gambar/ID.png");
setIcon(tabel, 0, imgIcon, "ID");
imgIcon = new ImageIcon("src/Gambar/builder.png");
setIcon(tabel, 1, imgIcon, "Nama");
imgIcon = new ImageIcon("src/Gambar/alamat.png");
setIcon(tabel, 2, imgIcon, "Alamat");
scrollPane.setViewportView(tabel);
btnInformasiPelanggan = new JButton("Informasi Pelanggan");
btnInformasiPelanggan.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent act)
{
int pilih = tabel.getSelectedRow();
if(pilih<0)
{
return;
}
String id = (String) tabelModel.getValueAt(pilih, 0);
String nama = (String) tabelModel.getValueAt(pilih, 1);
String alamat = (String) tabelModel.getValueAt(pilih, 2);
JOptionPane.showMessageDialog(null,"No.ID : " + id + " \n " +"Nama : " + nama + " \n " + "Alamat : " + alamat ,"Pesan",JOptionPane.INFORMATION_MESSAGE,new ImageIcon("src/Gambar/bukuPesan.png"));
}
});
btnInformasiPelanggan.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/Pesan.png"));
btnInformasiPelanggan.setBounds(189, 255, 225, 50);
contentPane.add(btnInformasiPelanggan);
JLabel lblBg = new JLabel("");
lblBg.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/kdeHijau.png"));
lblBg.setBounds(0, 0, 623, 353);
contentPane.add(lblBg);
}
public void setIcon(JTable tabel,int kol_index,ImageIcon i,String nama)
{
tabel.getTableHeader().getColumnModel().getColumn(kol_index).setHeaderRenderer(new PictureRender());
tabel.getColumnModel().getColumn(kol_index).setHeaderValue(new textIcon(nama, i));
}
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
/*try
{
com.jtattoo.plaf.mcwin.McWinLookAndFeel.setTheme("Large-Font", "Java Swing", "");
UIManager.setLookAndFeel("com.jtattoo.plaf.mcwin.McWinLookAndFeel");
FrameTabel frame = new FrameTabel();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}*/
try
{
FrameTabel frame = new FrameTabel();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableCellRenderer;
@SuppressWarnings("serial")
public class PictureRender extends DefaultTableCellRenderer
{
public Component getTableCellRendererComponent(JTable tabel,Object obj,boolean isSelected,boolean hasFocus,int baris,int kolom)
{
textIcon ti = (textIcon) obj;
if(obj == ti)
{
setIcon(ti.imgIcon);
setText(ti.text);
}
setBorder(UIManager.getBorder("TableHeader.cellBorder"));
setHorizontalAlignment(JLabel.CENTER);
return this;
}
}
import javax.swing.ImageIcon;
public class textIcon
{
String text;
ImageIcon imgIcon;
public textIcon(String txt,ImageIcon i)
{
// TODO Auto-generated constructor stub
text = txt;
imgIcon = i;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment