Problemas ao manipular JTable e JCheckBox

1 resposta
Ewerton_Muniz
import java.awt.BorderLayout;

import javax.swing.JPanel;

import javax.swing.JFrame;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JTextField;

import javax.swing.JCheckBox;

import org.hsqldb.util.TableSorter;

public class TelaEscalonadorGui extends JFrame {

private JPanel jContentPane = null;

private JScrollPane ScrollProcessos = null;

private JTable TableListProcessos = null;

private JButton ButtonGeraProcessos = null;

private JLabel LabelNameProcesso = null;

private JTextField NomeProcesso = null;

private JCheckBox CheckBoxFilhos = null;

private JLabel LabelFilhos = null;

private TableSorter tableSorter = null; // @jve:decl-index=0:visual-constraint=""

/**
  • This is the default constructor
    */
    public TelaEscalonadorGui() {
    super();
    initialize();
    }

/**

  • This method initializes this
  • @return void
    */
    private void initialize() {
    this.setSize(911, 626);
    this.setContentPane(getJContentPane());
    this.setTitle(“JFrame”);
    }

/**

  • This method initializes jContentPane
  • @return javax.swing.JPanel
    */
    private JPanel getJContentPane() {
    if (jContentPane == null) {
    LabelFilhos = new JLabel();
    LabelFilhos.setBounds(new java.awt.Rectangle(420,60,102,31));
    LabelFilhos.setText(" Filhos Processo");
    LabelNameProcesso = new JLabel();
    LabelNameProcesso.setBounds(new java.awt.Rectangle(420,16,120,29));
    LabelNameProcesso.setText(" Nome do Processo:");
    jContentPane = new JPanel();
    jContentPane.setLayout(null);
    jContentPane.add(getScrollProcessos(), null);
    jContentPane.add(getButtonGeraProcessos(), null);
    jContentPane.add(LabelNameProcesso, null);
    jContentPane.add(getNomeProcesso(), null);
    jContentPane.add(getCheckBoxFilhos(), null);
    jContentPane.add(LabelFilhos, null);
    }
    return jContentPane;
    }

/**

  • This method initializes ScrollProcessos
  • @return javax.swing.JScrollPane
    */
    private JScrollPane getScrollProcessos() {
    if (ScrollProcessos == null) {
    ScrollProcessos = new JScrollPane();
    ScrollProcessos.setBounds(new java.awt.Rectangle(31,18,347,506));
    ScrollProcessos.setViewportView(getTableListProcessos());
    }
    return ScrollProcessos;
    }

/**

  • This method initializes TableListProcessos
  • @return javax.swing.JTable
    */
    private JTable getTableListProcessos() {
    if (TableListProcessos == null) {
    TableListProcessos = new JTable(30,6);
    TableListProcessos.setAutoCreateColumnsFromModel(true);
    TableListProcessos.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
    TableListProcessos.setName(“Lista de Processos”);
    JTable table = new JTable();
    JScrollPane scroller = new JScrollPane( table );
    table.getTableHeader().getColumnModel().getColumn(0).setHeaderValue(“Pid”);
    table.getTableHeader().getColumnModel().getColumn(1).setHeaderValue(“Nome”);
    table.getTableHeader().getColumnModel().getColumn(2).setHeaderValue(“Status”);
    table.getTableHeader().getColumnModel().getColumn(3).setHeaderValue(“Quantidade”);
    table.getTableHeader().getColumnModel().getColumn(4).setHeaderValue(“Prioridade”);

}

return TableListProcessos;
}

/**

  • This method initializes ButtonGeraProcessos
  • @return javax.swing.JButton
    */
    private JButton getButtonGeraProcessos() {
    if (ButtonGeraProcessos == null) {
    ButtonGeraProcessos = new JButton();
    ButtonGeraProcessos.setBounds(new java.awt.Rectangle(583,92,138,25));
    ButtonGeraProcessos.setText(“Gerar Processos”);
    }
    return ButtonGeraProcessos;
    }

/**

  • This method initializes NomeProcesso
  • @return javax.swing.JTextField
    */
    private JTextField getNomeProcesso() {
    if (NomeProcesso == null) {
    NomeProcesso = new JTextField();
    NomeProcesso.setBounds(new java.awt.Rectangle(554,17,171,2);
    }
    return NomeProcesso;
    }

/**

  • This method initializes CheckBoxFilhos
  • @return javax.swing.JCheckBox
    */
    private JCheckBox getCheckBoxFilhos() {
    if (CheckBoxFilhos == null) {
    CheckBoxFilhos = new JCheckBox();
    CheckBoxFilhos.setBounds(new java.awt.Rectangle(539,66,21,17));
    }
    return CheckBoxFilhos;
    }

/**

  • This method initializes tableSorter
  • @return org.hsqldb.util.TableSorter
    */
    private TableSorter getTableSorter() {
    if (tableSorter == null) {
    tableSorter = new TableSorter();
    }
    return tableSorter;
    }

}

Eu não sei manipular direito o JTable, estou com algumas dúvidas.
º - Usei o able.getTableHeader().getColumnModel().getColumn(0).setHeaderValue()

  • Não funcionou ele, tem algo de errado ou preciso fazer mais alguma coisa?
    2º - Tentei alterar o tamanho das colunas usando table.getTableHeader().getColumnModel().getColumn(4) que o objeto retornado tem um metodo, que acho que se chama setMaxWidth(), não funcinou. Creio que estou fazendo algo de errado, isso eu tenho certeza.
    3º - Tentei usa como este site mostrar http://cadeiras.iscte.pt/lei-lp/JTable2.pdf
  • Coloquei isso no construtor da tabela private JTable getTableListProcessos() , também não funcionou!
    4º - Como e que eu coloco dados na tabela, to me enrolando pra karamba nisso?
    5º - Não encontrei como se tira as linhas da tabela.
    6º - a Última como e que faço quando marcar o jcheckbox e abrar outro frame?

Ewerton Muniz

1 Resposta

danielbussade

Olá bem vindo ao GUJ, antes de postar mais algum tópico leia este aqui http://guj.com.br/posts/list/50115.java

Para os problemas com o JTable leia isto: http://www.guj.com.br/java.tutorial.artigo.140.1.guj
e depois isto: http://www.guj.com.br/java.tutorial.artigo.147.1.guj

Quando ao do JChekBox, faça assim:

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class Exemplo extends JFrame {
	private JCheckBox checkAbrir;
	
	public Exemplo() {
		this.checkAbrir= new JCheckBox("Abrir");
		Container container=this.getContentPane();
		container.add(checkAbrir);
		checkAbrir.addItemListener(new ItemListener () {

			public void itemStateChanged(ItemEvent arg0) {
				
				metodo();
			}
			
		});
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
	}
	
	public void metodo() {
		if( checkAbrir.isSelected() ){
			//Aqui você cria o objeto da classe 
			//que defini a tela da qual vc quer chamar 
			Exemplo e = new Exemplo();
			e.setVisible(true);
			
		}	
	}

}

public class Principal {
	public static void main(String[] args) {
		Exemplo e= new Exemplo();
		e.setVisible(true);
		
	}
	 
}

Att

Criado 2 de dezembro de 2007
Ultima resposta 2 de dez. de 2007
Respostas 1
Participantes 2