2
I have a query screen, and I would like to define the size of the columns, because some columns take up more space than necessary. I thought that if I made a comparison with the title of the column, although it might not be the most oriented way to do this, I could do several "if" defining the sizes I wanted.
To reinforce understanding, the problem would be this, I would have for example columns such as, Code, Parents, Acronym and Status. Code would not need to have a column larger than 50, while parents at least should be 80. symbol and status no more than 60.
Below I will put a very simple example, just for understanding.
Tela Sistema:
package telas;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
public class TelaSistema extends JFrame implements ActionListener {
public static JDesktopPane jdp = new JDesktopPane();
public JMenuBar jmb = new JMenuBar();
public JMenu jmCadastros = new JMenu("Cadastros");
public JMenuItem jmiE = new JMenuItem("Uma tela qualquer");
public TelaSistema() {
setExtendedState(MAXIMIZED_BOTH);
setTitle("Sistema");
getContentPane().add(jdp);
setJMenuBar(jmb);
jmb.add(jmCadastros);
adicionaJMenuItem(jmCadastros, jmiE);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
private void adicionaJMenuItem(JMenu menu, JMenuItem item) {
menu.add(item);
item.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == jmiE) {
TelaCadastroA a = new TelaCadastroA();
jdp.add(a);
}
}
public static void main(String args[]) {
TelaSistema telaSistema = new TelaSistema();
}
}
Tela Cadastro:
package telas;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
public class TelaCadastroA extends JInternalFrame implements ActionListener {
public JButton jbConsultar = new JButton("Consultar");
public JPanel jpBotoes = new JPanel();
public TelaCadastroA() {
super("");
adicionaBotao(jbConsultar);
setSize(500, 500);
getContentPane().add("South", jpBotoes);
jpBotoes.setLayout(new GridLayout(1, 1));
Dimension tamanhoTela = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((tamanhoTela.width - getWidth()) / 2, ((tamanhoTela.height - getHeight())) / 2);
setVisible(true);
}
private void adicionaBotao(JButton botao) {
jpBotoes.add(botao);
botao.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == jbConsultar) {
consultar();
}
}
public void consultar() {
TelaConsultar.getTela("Consulta", new String[]{
"Código", "Pais", "Sigla", "Status"
}, new DefaultTableCellRenderer[]{null, null, new CellRenderMonetario(), null}, this);
}
class CellRenderMonetario extends DefaultTableCellRenderer {
private DecimalFormat df = new DecimalFormat(",##0.00");
@Override
public Component getTableCellRendererComponent(JTable tabela, Object valor, boolean isSelected, boolean temFocu, int linha, int coluna) {
if (valor == null) {
valor = 0;
}
if (valor instanceof String) {
setText(df.format(Double.parseDouble((String) valor)) + " ");
} else {
setText(df.format(valor) + " ");
}
setHorizontalAlignment(SwingConstants.RIGHT);
return this;
}
}
}
Query screen:
package telas;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JInternalFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class TelaConsultar extends JInternalFrame implements InternalFrameListener, MouseListener {
private static TelaConsultar telaConsultar = null;
private String sql;
private String[] titulos;
private TelaCadastroA tela;
private JTable tabela;
private JScrollPane jsp;
private DefaultTableModel dtm = new DefaultTableModel();
private String proc;
public TelaConsultar(String tituloJanela, String[] titulosColuna, DefaultTableCellRenderer[] renderizadores, TelaCadastroA tela) {
super(tituloJanela, true, true, false, false);
this.sql = sql;
this.titulos = titulosColuna;
this.tela = tela;
tabela = new JTable() {
@Override
public boolean isCellEditable(int linha, int coluna) {
return false;//Esta tabela não pode ser editada pois é uma consulta
}
};
tabela.setModel(dtm); //Define o modelo da tabela
tabela.getTableHeader().setReorderingAllowed(false);
for (int i = 0; i < titulosColuna.length; i++) {
dtm.addColumn(titulosColuna[i]);
}
jsp = new JScrollPane(tabela);
getContentPane().add(jsp);
preencher();
//pack();
setSize(650, 400);
setMinimumSize(new Dimension(getWidth(), getHeight()));
centralizaTelaConsulta();
setVisible(true);
tabela.addMouseListener(this);
addInternalFrameListener(this);
TableColumnModel tableModel = tabela.getColumnModel();
if (titulosColuna.equals("Código")) {
for (int i = 0; i < titulosColuna.length; i++) {
tableModel.getColumn(i).setMinWidth(30);
tableModel.getColumn(i).setMaxWidth(50);
}
}
else
{
for (int i = 0; i < titulosColuna.length; i++) {
tableModel.getColumn(i).setMinWidth(100);
}
}
for (int i = 0; i < tableModel.getColumnCount(); i++) {
if (renderizadores[i] != null) {
tableModel.getColumn(i).setCellRenderer(renderizadores[i]);
}
}
}
public static void getTela(String tituloJanela, String[] titulos, DefaultTableCellRenderer[] renderizadores, TelaCadastroA tela) {
if (telaConsultar == null) {
telaConsultar = new TelaConsultar(tituloJanela, titulos, renderizadores, tela);
TelaSistema.jdp.add(telaConsultar);
}
TelaSistema.jdp.setSelectedFrame(telaConsultar);
TelaSistema.jdp.moveToFront(telaConsultar);
}
private void preencher() {
for (int i = 0; i < 10; i++) {
dtm.addRow(new Object[]{"" + i, "Brasil - " + i});
}
}
public void centralizaTelaConsulta() {
Dimension tamanhoTela = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((tamanhoTela.width - getWidth()) / 2, ((tamanhoTela.height - getHeight())) / 2);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void internalFrameOpened(InternalFrameEvent e) {
}
@Override
public void internalFrameClosing(InternalFrameEvent e) {
}
@Override
public void internalFrameClosed(InternalFrameEvent e) {
TelaSistema.jdp.remove(telaConsultar);
telaConsultar = null;
}
@Override
public void internalFrameIconified(InternalFrameEvent e) {
}
@Override
public void internalFrameDeiconified(InternalFrameEvent e) {
}
@Override
public void internalFrameActivated(InternalFrameEvent e) {
}
@Override
public void internalFrameDeactivated(InternalFrameEvent e) {
}
@Override
public void mouseClicked(MouseEvent me) {
}
}
Here’s the question, each screen can have a different amount of columns and in different positions. So, if I could get the name, it would be perfect.
– Gustavo Santos
Man, I tried to understand how I would apply your code.
– Gustavo Santos
Why downvote? The code is perfectly functional, only testing and checking.
– user28595
gave right your solution !
– Gustavo Santos
@Gustavosantos the questioning was directed to who gave the downvote, but unfortunately it is a waste of time, I will have to report the moderation. Anyway, test there, I tested in your code, it worked, I just don’t know if it was the way I expected.
– user28595
worked yes, just explain me this question of setPreferredWidth, what it does ? And about downvote, it is the thing of envious people even.
– Gustavo Santos
The
setPreferredWidth
indicates which table is the preferred size to configure the column. When you let the EDT define the size of the table, it uses the preferredsize to set the components sizes.– user28595
then I should always indicate in it the value equal to setMaxWidth() ?
– Gustavo Santos
@Gustavosantos actually I used maxwidth ai because the screen is using a layout manager, and it is he who defines the distribution of component sizes, so I have set the 3 measures, pro layout manager understand and configure the sizes correctly.
– user28595