1
How do I make mine JTable
is displayed on all tabs inside the panels? I’m only able to display in the "Day 5 tab".
import java.awt.Container;
import java.sql.ResultSet;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
public class NewClass extends JFrame{
ResultSet rst = null;
public Container content;
public JPanel jpDespesas, jpGerenciar, jpCofre, jpDia5, jpDia20, jpDia25;
public JLabel jlTotal, jlTitulo, jlVencimento, jlValor, jlSemana, jlCofre;
public JTextField jtTotal, jtTitulo, jtVencimento, jtValor, jtSemana;
public JButton jbCalcular, jbSalvar, jbEditar, jbExcluir, jbTotal, jbCofre;
public JTable jtbDespesas;
public JTabbedPane jtb;
public NewClass() {
super("Controle financeiro");
setLayout(null);
setSize(680, 350);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content = getContentPane();
criaTela();
}
public void criaTela() {
//CRIANDO JPANEL PRINCIPAL E SETANDO PROPRIEDADES
jpDespesas = new JPanel();
jpDespesas.setLayout(null);
jpDespesas.setBorder(BorderFactory.createTitledBorder("Resumo mensal"));
jpDespesas.setBounds(20, 15, 340, 315);
add(jpDespesas);
//CRIANDO PAINEL COM ABAS
jtb = new JTabbedPane();
jtb.setBounds(15, 20, 310, 145);
jpDespesas.add(jtb);
jpDia5 = new JPanel();
jpDia5.setLayout(null);
jpDia5.setBounds(20, 20, 100, 170);
jtb.add("Dia 5", jpDia5);
jpDia20 = new JPanel();
jpDia20.setLayout(null);
jpDia20.setBounds(20, 20, 250, 170);
jtb.add("Dia 20", jpDia20);
jpDia25 = new JPanel();
jpDia25.setLayout(null);
jpDia25.setBounds(20, 20, 250, 170);
jtb.add("Dia 25", jpDia25);
//CRIANDO O JTABLE
jtbDespesas = new JTable();
//SETANDO A QUANTIDADE DE COLUNAS E SEUS RESPECTIVOS TÍTULOS
jtbDespesas.setModel(new DefaultTableModel(new Object[][]{}, new String[]{"Título", "Vencimento", "Valor"}));
//SETANDO A LARGURA DE CADA COLUNA
jtbDespesas.getColumnModel().getColumn(0).setPreferredWidth(100);
jtbDespesas.getColumnModel().getColumn(1).setPreferredWidth(50);
jtbDespesas.getColumnModel().getColumn(2).setPreferredWidth(50);
//ADICIONANDO LINHAS
DefaultTableModel dtm = (DefaultTableModel) jtbDespesas.getModel();
dtm.addRow(new Object[]{});
dtm.addRow(new Object[]{});
dtm.addRow(new Object[]{});
dtm.addRow(new Object[]{});
dtm.addRow(new Object[]{});
dtm.addRow(new Object[]{});
dtm.addRow(new Object[]{});
JScrollPane jp = new JScrollPane();
jp.setBounds(5, 10, 295, 100);
jp.setViewportView(jtbDespesas);
jpDia5.add(jp);
}
public static void main(String[] args) {
new NewClass().setVisible(true);
}
}
I would like it to be the same table for practicality, so that my code is not so extensive, but the important thing is that it appears in all guides
– Rogério Simsen
So why not add the table in the other panels? If not add, it will not even appear.
– user28595
I already tried, but then the table keeps appearing only in one of the guides. When I add it to all the panels, it is shown only in the "Day 25" tab, it is as if the table were jumping from panel to panel until the last, which in this case is the "Day 25" tab. Will the only solution is to create a table for each panel?
– Rogério Simsen
Why are you using absolute layout? It’s getting in the way
– user28595
to be able to position the components freely
– Rogério Simsen
Bad idea this, should avoid using absolute layout, unless you have some need and know well what you are doing. He is the one who is resulting this problem of his code. Can I suggest something without this absolute layout? If you are giving problem, you have to remove and try to learn in the most practical and less complicated way.
– user28595
I’m using the absolute layout to be able to have more control over the components and position where I want, because in the other layouts the definitions of positions are already defined, I do not work very well, but suggests me something else please, I need to solve this problem
– Rogério Simsen
See the answer below. I recommend that you visit and read the links, they will be important for your learning.
– user28595