IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 Java Discussion :

java, layout et jtable


Sujet :

Java

  1. #1
    Membre éclairé Avatar de Rniamo
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    508
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 508
    Par défaut java, layout et jtable
    bonjour,

    je développe une petite application avec GUI mais j'ai un soucis au niveau de la mise en forme, en gros je fais :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    public class gui extends JFrame {
         public gui() {
            setLayout(new BorderLayout());//new BoxLayout(getContentPane(),BoxLayout.PAGE_AXIS));
     
            tableModel=new TableModel(data,columnNames);
            table=new JTable(tableModel);
            table.setFillsViewportHeight(true);
     
            add(new JScrollPane(table),BorderLayout.NORTH);
     
    add(new JButton("bouton"),BorderLayout.SOUTH);
         }
    }
    le problème est que je voudrais :
    1) que le bouton prenne toutes la largeur
    2) que le jscrollpane prenne toutes la hauteur possible et que la jtable qui est dedans s'adapte en hauteur pour le suivre

  2. #2
    Membre Expert
    Avatar de supersnail
    Homme Profil pro
    Inscrit en
    Novembre 2006
    Messages
    1 719
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Novembre 2006
    Messages : 1 719
    Par défaut
    Je pense qu'un schéma serait plus clair que ta description...

  3. #3
    Membre éclairé Avatar de Rniamo
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    508
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 508
    Par défaut
    http://img406.imageshack.us/img406/9053/65045135rx5.png

    Quand je maximise le bouton reste petit et est seul en bas.

    edit : but à atteindre : http://pix.nofrag.com/d/4/9/7085a9ca...357331bcce.png

    edit 2 : code complet de Gui.java
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package algogen;
     
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.*;
    import javax.swing.table.AbstractTableModel;
    import javax.swing.table.TableCellRenderer;
     
    /**
     *
     * @author rniamo
     */
    public class Gui extends JFrame {
        private static final String TITLE="AlgoGen";
        private Population population;
     
        private TableModel tableModel;
        private JTable table;
        private JButton elitismeButton;
     
        private Object[][] createObjectTable() {
            Object[][] data=new Object[population.getNumber()][population.getSize()+2];
     
            for(int i=0;i<population.getNumber();i++) {
                data[i][0]=new String();
                data[i][0]=Integer.toString(i+1);
                for(int j=population.getSize()-1;j>=0;j--)
                    data[i][j+1]=new Integer(population.getChromosome(i).getBit(j));
                float c=(float)((float) population.getChromosome(i).getValue()/Math.pow(2, population.getChromosome(i).getSize()));
                data[i][population.getSize()+1]=new Color(c,c,c);
            }
     
            return data;
        }
     
        private String[] createColumnNames() {
            String[] columnNames=new String[population.getSize()+2];
     
            columnNames[0]="Chromosome";
            for(int i=population.getSize()-1;i>=0;i--)
                columnNames[population.getSize()-i]="Bit "+Integer.toString(i);
            columnNames[population.getSize()+1]="Color";
     
            return columnNames;
        }
     
        public Gui(int number, int size) {
            super();
     
            population=new Population(number, size);
     
            // fenêtre
            setTitle(TITLE);
            setLayout(new BoxLayout(getContentPane(),BoxLayout.PAGE_AXIS));
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            }
            catch (UnsupportedLookAndFeelException e) {}
            catch (ClassNotFoundException e) {}
            catch (InstantiationException e) {}
            catch (IllegalAccessException e) {}
     
            // table
            tableModel=new TableModel(createObjectTable(),createColumnNames());
            table=new JTable(tableModel);
            table.setFillsViewportHeight(true);
     
            table.getTableHeader().setBackground(Color.BLUE);
            table.getTableHeader().setForeground(Color.ORANGE);
     
            table.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    JPanel panel=new JPanel();
                    panel.setBackground(Color.BLUE);
                    JLabel label=new JLabel((String)value);
                    label.setForeground(Color.ORANGE);
                    panel.add(label);
                    panel.setToolTipText((String)value);
                    return panel;
                }
            });
            for(int i=1;i<table.getColumnCount();i++)
                table.getColumnModel().getColumn(i).setCellRenderer(new TableCellRenderer() {
                    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                        JLabel label=new JLabel(value.toString());
                        label.setHorizontalAlignment(JLabel.CENTER);
                        label.setToolTipText("Chromosome "+Integer.toString(row+1)+" | Bit "+Integer.toString(population.getSize()-column));
                        return label;
                    }
                });
            table.getColumnModel().getColumn(table.getColumnCount()-1).setCellRenderer(new TableCellRenderer() {
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    Color color=(Color) value;
                    JPanel panel=new JPanel();
                    panel.setBackground(color);
                    panel.setToolTipText(Integer.toString(population.getChromosome(row).getValue()));
                    return panel;
                }
            });
     
     
            add(new JScrollPane(table)); //, BorderLayout.NORTH);
     
            // bouton
            elitismeButton=new JButton("Elitisme");
            elitismeButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    population.elitisme();
                    refresh();
                }
            });
     
            add(elitismeButton);
     
            pack();
        }
     
        public void refresh() {
            tableModel.setData(createObjectTable());
            tableModel.setColumNames(createColumnNames());
            tableModel.fireTableDataChanged();
        }
     
        public Gui() {
            this(10,8);
        }
     
        private class TableModel extends AbstractTableModel {
            private Object[][] data;
            private String[] columnNames;
     
            public TableModel(Object[][] data, String[] columnNames) {
                this.data=data;
                this.columnNames=columnNames;
            }
     
            public int getRowCount() {
                return data.length;
            }
     
            public int getColumnCount() {
                return data[0].length;
            }
     
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (rowIndex>=data.length || columnIndex>=data[0].length) throw new NumberFormatException("Bad indices.");
                return data[rowIndex][columnIndex];
            }
     
            @Override
            public String getColumnName(int column) {
                if (column>=columnNames.length) throw new NumberFormatException("Bad column.");
                return columnNames[column];
            }
     
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
     
            public void setData(Object[][] newData){
                data=newData;
                super.fireTableDataChanged();
            }
     
            private void setColumNames(String[] newColumnNames) {
                columnNames=newColumnNames;
            }
        }
    }
    edit : problème de bouton réglé

  4. #4
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    18
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 18
    Par défaut
    Pour le bouton, il suffit de le mettre dans un JPanel qui a, par exemple, un GridLayout. Puis de mettre ce JPanel au Sud de ton BorderLayout.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    GridLayout layout = new GridLayout(1,1);
    JPanel panel = new JPanel();
    panel.setLayout(layout);
    panel.add(new JButton("bouton"));
    add(panel, BorderLayout.SOUTH);
    Pour la JTable, c'est moins gagné car, à ma connaissance, il n'y a pas de moyen pour qu'elle s'adapte en hauteur à son container (juste possible en largeur).

  5. #5
    Membre éclairé Avatar de Rniamo
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    508
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 508
    Par défaut
    ok, je passe en résolu alors.Merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [Swing] Afficher une JTable speciale en java
    Par benssj5 dans le forum Composants
    Réponses: 6
    Dernier message: 12/03/2013, 20h21
  2. [Java] Fond de JTable transparent
    Par Yann_69 dans le forum Composants
    Réponses: 2
    Dernier message: 05/06/2012, 12h58
  3. [Java] Rafraichir Tableau (JTable)
    Par SoGeek dans le forum Composants
    Réponses: 6
    Dernier message: 27/04/2010, 14h44
  4. [Print]Edition en java : JTable ou JasperReports
    Par calimero82 dans le forum Composants
    Réponses: 5
    Dernier message: 05/11/2004, 15h50

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo