开发者

Java table formatting

开发者 https://www.devze.com 2023-02-09 07:23 出处:网络
I’im programing in Java with Netbeans. I have a table who list patients. I read the information in XML file.

I’im programing in Java with Netbeans.

I have a table who list patients. I read the information in XML file. Here you have the code:

package digiscope;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

/**
 *
 * @author Daniel
 */
public class TabelaPaciente extends javax.swing.JPanel {


 String numero_processo;
    int localizador = 0;

    /** Creates new form TabelaPaciente */

    public TabelaPaciente() {
        initComponents();
        load_table();
    }

    public void setNumeroProc(String numero) {
        numero_processo = numero;
    }

    public String getNumeroProc() {
        return numero_processo;
    }

    public int getLocalizador() {
        return localizador;
    }

    String[][] concat(String[][] A, String[][] B) {
        String[][] C = new String[A.length + B.length][A.length + B.length];
        System.arraycopy(A, 0, C, 0, A.length);
        System.arraycopy(B, 0, C, A.length, B.length);

        return C;
    }

    private void load_table() {

        File dir;
        String[] filenames;

        //dir = new File(System.getProperty("user.dir")+ "/XML");
        dir = new File(System.getProperty("user.dir") + "/Processos");

        filenames = dir.list();

        String[][] table_final = new String[][]{};

        for (int i = 0; i < filenames.length; i++) {

            String n_proc = filenames[i];//filenames[i].substring(0,filenames[i].length()-4);

            //System.out.println(filenames[i]);

            Ler_XML xml = new Ler_XML(n_proc);
            String nome_utente = xml.getNome();

            String[][] line = new String[][]{
                {n_proc, nome_utente, null, "ir para formulario"}
            };

            table_final = concat(table_final, line);
        }

        tabela_pacientes.setModel(new javax.swing.table.DefaultTableModel(
                table_final,
                new String[]{
                    "Process Nº", "Name", "Date", "Form state"
                }));
    }



    void executa_accao(int column, int row) {

        if (column == 3) {
            Object numero_proc = tabela_pacientes.getValueAt(row, 0);

            localizador = 1;

            numero_processo = numero_proc.toString();

            JanelaPrincipal cont = (JanelaPrincipal) this.getParent() //    Internal Pane
                    .getParent() // ???
                    .getParent() // ???
                    .getParent() // ???
                    .getParent();   //  Frame

            cont.loadPanel(new Formulario(1));

            Preencher_FormPaciente new_xml = new Preencher_FormPaciente(numero_processo, 1);

        }

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabela_pacientes = new javax.swing.JTable();
        btnMenu1 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(1000, 550));

        tabela_pacientes.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent event) {
                int row = tabela_pacientes.rowAtPoint(event.getPoint());
                int column = tabela_pacientes.columnAtPoint(event.getPoint());
                if (row >= 0 && column >= 0) {
                    executa_accao(column, row);
                }
            }
        });
        jScrollPane1.setViewportView(tabela_pacientes);

        btnMenu1.setBackground(new java.awt.Color(255, 153, 0));
        btnMenu1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        btnMenu1.setText("MENU");
        btnMenu1.setPreferredSize(new java.awt.Dimension(105, 75));
        btnMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnMenu1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMenu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(885, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1000, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnMenu1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 446, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void btnMenu1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JanelaPrincipal cont = (JanelaPrincipal) this.getParent() //    Internal Pane
                    .getParent() // ???
                    .getParent() // ???
                    .getParent() // ???
                    .getParent();   //  Frame
            cont.loadPanel(new Menu());
    }                                        

    // Variables declaration - do not modify
    private javax.swing.JButton btnMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela_pacientes;
    // End of variables declaration
}

I would like to know if it’s possible to increase the letter size? You can see here a preview of the table.

Java table formatting

Thank you for your collaborati开发者_运维百科on.

Best regards.


In many cases a table will use multiple renderers to render the data. One for Strings, Numbers, Dates etc. The default renderers use the Font of the table to render the text. To change the font for all renderers you can use:

Font font = table.getFont();
table.setFont( font.deriveFont(24.0f) );
table.setRowHeight(24);

You can do this for the table header as well.


The display of a cell in a JTable is controlled by the TableCellRenderer. The default implementation extends from JLabel which has methods to control font. For example...

DefaultTableCellRenderer newRenderer = new DefaultTableCellRenderer();
Font oldFont = newRenderer.getFont();
Font bigFont = new Font(oldFont.getName(),oldFont.getStyle(),24);
newRenderer.setFont(bigFont);
table.setDefaultRenderer(Object.class,newRenderer);
0

精彩评论

暂无评论...
验证码 换一张
取 消