ardayigit
ardayigit

Reputation: 167

JTable width layout

I'm creating a GUI interface to interact with the database of a warehouse. The application needs to add items to the database, update them and show them. I have some tables in the database and for each table, I want to create a JPanel, and put them in a cardlayout, so I can navigate between them with JMenu items. Each JPanel has the same form. In the top, there is a box with textfields, comboboxes etc. to add an item in the table. Under the box, I have a JTable with 1 row and under that, I have a JTable in a JScrollPane to show the content of the table. Each column needs to have a width of 150, except the last one (width=100, it will contain a JButton for modifications). I use the first JTable as a filter (for example if the first column contains '1', then the second JTable will only show items with an ID starting by '1'). I don't know how to choose correct layouts for different JPanels. For the moment, each JPanel has a BorderLayout and each component is placed in the center. But the problem is that I can't choose the width of each column.

Upvotes: 0

Views: 829

Answers (2)

ardayigit
ardayigit

Reputation: 167

Well, finally I've solved my own problem. Actually, I need to put the JTable in a JPanel, and the JPanel in a JScrollPane. Here is the code :

public class Brouillon extends JFrame {

    public Brouillon() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

        JPanel jpContent = new JPanel(); 
        JPanel jpCards = new JPanel(); 
        jpContent.setLayout(new BoxLayout(jpContent, BoxLayout.PAGE_AXIS)); 
        CardLayout clSelect = new CardLayout(); 
        jpCards.setLayout(clSelect); 
        JButton jbTest = new JButton(); 
        jbTest.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                clSelect.next(jpCards); 
            }
        });
        jpContent.add(jbTest); 
        JPanel jpContainer1 = new JPanel(); 
        JTable jtData1 = new JTable(new Object[2][3], new String[] {"1", "2", "3"}); 
        jtData1.getColumnModel().getColumn(0).setPreferredWidth(150); 
        jtData1.getColumnModel().getColumn(1).setPreferredWidth(150); 
        jtData1.getColumnModel().getColumn(2).setPreferredWidth(50); 
        JPanel jpTemp1 = new JPanel(); 
        jpTemp1.setLayout(new BorderLayout()); 
        jpTemp1.add(jtData1.getTableHeader(), BorderLayout.NORTH); 
        jpTemp1.add(jtData1, BorderLayout.CENTER); 
        JScrollPane jspData1 = new JScrollPane(jpTemp1); 
        jpContainer1.add(jspData1); 

        JPanel jpContainer2 = new JPanel(); 
        JTable jtData2 = new JTable(new Object[2][7], new String[] {"1", "2", "3", "4", "5", "6", "7"}); 
        jtData2.getColumnModel().getColumn(0).setPreferredWidth(150); 
        jtData2.getColumnModel().getColumn(1).setPreferredWidth(150); 
        jtData2.getColumnModel().getColumn(2).setPreferredWidth(150); 
        jtData2.getColumnModel().getColumn(3).setPreferredWidth(150); 
        jtData2.getColumnModel().getColumn(4).setPreferredWidth(150); 
        jtData2.getColumnModel().getColumn(5).setPreferredWidth(150); 
        jtData2.getColumnModel().getColumn(6).setPreferredWidth(50); 
        JPanel jpTemp2 = new JPanel(); 
        jpTemp2.setLayout(new BorderLayout());
        jpTemp2.add(jtData2, BorderLayout.CENTER); 
        jpTemp2.add(jtData2.getTableHeader(), BorderLayout.NORTH); 
        JScrollPane jspData2 = new JScrollPane(jpTemp2); 
        jpContainer2.add(jspData2); 
        jpCards.add(jpContainer2); 
        jpCards.add(jpContainer1); 
        jpContent.add(jpCards); 


        this.getContentPane().add(jpContent); 

        this.pack(); 
        this.setVisible(true); 
    }

    public static void main(String[] args) {
        new Brouillon(); 
    }

}

Upvotes: 1

Sirsendu
Sirsendu

Reputation: 283

For jTable, you can set the column size. Possibly the below example may help. Please let me know if this is what you are looking for!

TableColumn column = null;
for (int i = 0; i < 3; i++) {
    column = table.getColumnModel().getColumn(i);
    if (i == 2) {
        column.setPreferredWidth(100); //sport column is bigger
    } else {
        column.setPreferredWidth(50);
    }
}    

Upvotes: 0

Related Questions