Archive for noviembre, 2010

noviembre 20, 2010

Agregar botones a las pestañas de un JTabbedPane

run

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
/**
 * @author leyer
 */
public class JTapPe extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;

	public static final short _WIDTH =500;
	public static final short _HEIGTH=500; 
	
	protected JTabbedPane  tabbedPane=null;
	private JButton        button   =null;
	private class PANEL extends JPanel implements ActionListener{
	private static final long serialVersionUID = 1L;
	
	private String  title  =null;
	private JButton button =null;
	private JTapPe  tapPe  =null;
	public PANEL(String title, JButton tabButton, JTapPe jTapPe){
		this.title =title;
		this.button=tabButton;
		this.tapPe =jTapPe;
		button.addActionListener(this);
		}
	@Override
	public void actionPerformed(final ActionEvent e) {
		tapPe.removeTap(title);
		}
	}
	public JTapPe() {
		this.setSize(_WIDTH,_HEIGTH);
		tabbedPane=new JTabbedPane();	
		button=new JButton("Add");
		button.addActionListener(this);
		getContentPane().add(tabbedPane,BorderLayout.CENTER);
		getContentPane().add(button,BorderLayout.NORTH);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override public void run() {	
				new JTapPe().setVisible(true);
			}
		});
	}
	public boolean removeTap(String title) {
		int i = tabbedPane.getTabCount();
		for (int index = 0; index < i; index++) {
			String temp = tabbedPane.getTitleAt(index);
			if (temp.equals(title)) {
				tabbedPane.removeTabAt(index);
				return true;
			}
		}
		return false;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		  JButton tabButton = new JButton("Cerrar");
		  String title=String.valueOf(new Random().nextInt(101));
		  PANEL p=new PANEL(title,tabButton,this);
	      tabbedPane.addTab(title,null, p);
	      tabButton.setPreferredSize(new Dimension(120,20));
	      tabButton.setContentAreaFilled(false);
	      JPanel pnl = new JPanel();
	      pnl.setOpaque(false);
	      JLabel label=new JLabel(title);
	      pnl.add(label);
	      pnl.add(tabButton);
	      tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, pnl);
	      tabbedPane.setSelectedIndex(tabbedPane.getTabCount() - 1);
	}
}
noviembre 20, 2010

Agregando un JComboBox a un JTable

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
/**
 * @author leyer
 */
public class JComboBoxJTable extends JFrame{
	private static final long serialVersionUID = 1L;
	protected JTable table = null;
	private JScrollPane scrollPane=null;
	private class TABLE_MODEL extends DefaultTableModel{
		public static final String INITIAL_VALUE = "DATO 1";
		private static final long serialVersionUID = 1L;
		public TABLE_MODEL(){
			this.addColumn(COLUMN1);
			this.addColumn(COLUMN2);
			this.addColumn(COLUMN3);
		}
		public static final String COLUMN1="A";
		public static final String COLUMN2="B";
		public static final String COLUMN3="C";
		public void addData(String a,String b){
			final Object rowData[]={a,b,INITIAL_VALUE};
			this.addRow(rowData);
		}
	}
	private TABLE_MODEL model=new TABLE_MODEL();
	public static final String[] DATA = { "Dato 1", "Dato 2", "Dato 3", "Dato 4" };
	public JComboBoxJTable(){
		JComboBox comboBox = new JComboBox(DATA);
		DefaultCellEditor defaultCellEditor=new DefaultCellEditor(comboBox);
		table=new JTable(model);
		table.getColumnModel().getColumn(2).setCellEditor(defaultCellEditor);
		scrollPane=new JScrollPane(table);
		this.getContentPane().add(scrollPane);
		this.setSize(500,500);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
		addData();
	}
	public void addData(){
		model.addData("valor 1", "valor 2");
		model.addData("valor 3", "valor 4");
		model.addData("valor 5", "valor 6");
	}
	public static void main(String[] args) {
	SwingUtilities.invokeLater(new Runnable() {
		@Override
		public void run() {
		new JComboBoxJTable().setVisible(true);
			}
		});
	}
}
noviembre 13, 2010

JCheckBox en JTable

import java.awt.Component;
import javax.swing.DefaultCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
 
/**
 * @author leyer
 */
public class jcheckBoxJtable extends JFrame{
	
	private static final long serialVersionUID = 1L;
	class TABLE_MODEL extends DefaultTableModel{
		private static final long serialVersionUID = 1L;
		public TABLE_MODEL(){
			addColumn("A");
			addColumn("B");
			addColumn("C");
			addColumn("D");
			addColumn("F");
			addColumn("G");
			for(int index=0;index<100;index++){
			Object object6[]={new Boolean(true),"Index","Numero","Test","#",index};
			addRow(object6);
			}
		}
	}
	class CELL_RENDERER extends JCheckBox implements TableCellRenderer{
		private static final long serialVersionUID = 1L;

		public CELL_RENDERER(){
			setHorizontalAlignment(JLabel.CENTER);
		}
		@Override
		public Component getTableCellRendererComponent(JTable arg0,
				Object value, boolean arg2, boolean arg3, int arg4, int arg5) {
			setSelected((value != null && ((Boolean) value).booleanValue()));
			setBackground(arg0.getBackground());
			setHorizontalAlignment(JLabel.CENTER);
			return this;
		}
		
	}
	class CELL_EDITOR extends DefaultCellEditor{
		private static final long serialVersionUID = 1L;

		public CELL_EDITOR(JCheckBox checkBox) {
			super(checkBox);
			checkBox.setHorizontalAlignment(JLabel.CENTER);	
		}
	}
	static JTable table=null;
	
	public jcheckBoxJtable(){
		TABLE_MODEL name=new TABLE_MODEL();
	    table=new JTable(name);
		table.getColumn("A").setCellRenderer(new CELL_RENDERER());
		table.getColumn("A").setMaxWidth(130);
		table.getColumn("A").setCellEditor(new CELL_EDITOR(new JCheckBox()));
		JScrollPane jScrollPane=new JScrollPane(table);
		table.setRowSelectionAllowed(true);
		
		add(jScrollPane);
		setSize(500,500);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	public static void main(String args[]){
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new jcheckBoxJtable();	
			}
		});
	}
}
noviembre 13, 2010

JProgressBar en JTable

import java.awt.Component;
import java.util.EventObject;
import javax.swing.JFrame;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
* @autor Leyer
*/
public class progressFrame extends JFrame {
	private static final long serialVersionUID = 1L;
    class TABLE_MODEL extends DefaultTableModel{
		private static final long serialVersionUID = 1L;
		public TABLE_MODEL(){
			addColumn("Progress");
			addColumn("Archivo");
		}
		public void addRow(String value){
			Object rowData[]={new JProgressBar(),value};
			addRow(rowData);
		}
	}
    class CELL_RENDERER implements TableCellRenderer{
		@Override
		public Component getTableCellRendererComponent(JTable arg0,
				Object arg1, boolean arg2, boolean arg3, int arg4, int arg5) {
	    	try{
	        return (JProgressBar)arg1;
	    	}catch (Exception e) {}
	     return (JProgressBar)arg1;
		}
    }
	public progressFrame(){
		this.setSize(500,300);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		TABLE_MODEL mod=new TABLE_MODEL();
		JTable table=new JTable(mod);
		mod.addRow("dctr1.doc");
		mod.addRow("java.txt");
		mod.addRow("dctr3.doc");
		mod.addRow("dctr4.doc");
		mod.addRow("dctr5.doc");
		table.getColumnModel().getColumn(0).setCellRenderer(new CELL_RENDERER());
		table.getColumnModel().getColumn(0).setCellEditor(new TableCellEditor() {
			@Override
			public boolean stopCellEditing() {
				return false;
			}
			@Override
			public boolean shouldSelectCell(EventObject arg0) {
				return false;
			}
			@Override
			public void removeCellEditorListener(CellEditorListener arg0) {}
			@Override
			public boolean isCellEditable(EventObject arg0) {
	             return false;
			}
			@Override
			public Object getCellEditorValue() {
				return null;
			}
			@Override
			public void cancelCellEditing() {}
			@Override
			public void addCellEditorListener(CellEditorListener arg0) {	}	
			@Override
			public Component getTableCellEditorComponent(JTable table, Object value,
					boolean isSelected, int row, int column) {
				return null;
			}
		});
		JProgressBar progressBar1=(JProgressBar)table.getValueAt(0, 0);
		progressBar1.setStringPainted(true);
		progressBar1.setValue(10);
		table.repaint();
		JProgressBar progressBar2=(JProgressBar)table.getValueAt(1, 0);
		progressBar2.setStringPainted(true);
		progressBar2.setValue(45);
		table.repaint();
		JProgressBar progressBar3=(JProgressBar)table.getValueAt(2, 0);
		progressBar3.setStringPainted(true);
		progressBar3.setValue(25);
		table.repaint();
		JProgressBar progressBar4=(JProgressBar)table.getValueAt(3, 0);
		progressBar4.setStringPainted(true);
		progressBar4.setValue(15);
		table.repaint();
		JScrollPane scrollPane=new JScrollPane(table);
		getContentPane().add(scrollPane);
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				new progressFrame().setVisible(true);
			}
		});
	}
}