简体   繁体   中英

Having a hard time resizing a JTable

I've having a really hard time resizing my JTable, I attempted to make the left-column a set width, while having the right column fill out the rest of the table, and the left columns width will change, but it extends it's container (And therefor cuts off some of the data) Image below:

http://gyazo.com/6e928b55f699622470e05fe06f2d9a23 (Image type not supported??)

As you can see the "Us" is cut off of the word User .

Here's what I've tried to resize this, all of which have had absolutely zero effect.

setBounds(any, any, any, any) -- No effect
setMaximumSize(new Dimension(any, any)); --- No effect

Then I tried some cheap-hacks in the Panel that it's a child of:

childPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
int index = Utils.getComponentIndex((childPanel.add(friendsList)));
childPanel.getComponent(index).setBounds(any, any, any, any);

which also had no effect.

The one that's bothering me most is that setBounds() isn't working, becasue that's what I'm using for everything (null layout, first jframe application, and I like being able to use pixel-locations for everything).

Here's the code: ( Extends JPanel )

public SocialPanel() {
    this.setLayout(null);
    this.setBounds(650, 0, 150, 400);
    this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));


    friendsList = new FriendsList();
    ignoreList = new IgnoreList();

    JLabel title = new JLabel("Social Pane");
    title.setBounds(45, 3, 75, 25);

    childPanel = new JPanel();
    childPanel.setBounds(0, 30, 150, 325);
    childPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
    childPanel.add(friendsList);

    JButton showFriends = new JButton("F");
    showFriends.setBounds(15, 360, 50, 30);

    JButton showIgnore = new JButton("I");
    showIgnore.setBounds(80, 360, 50, 30);


    this.add(title);    
    this.add(childPanel);
    this.add(showFriends);
    this.add(showIgnore);
}

Then ofcourse, the friendslist.java ( Extends JTable )

public FriendsList() {
    this.setGridColor(Color.gray);
    this.setShowGrid(true);
    this.setDefaultRenderer(Object.class, new SocialCellRenderer());
    this.setMaximumSize(new Dimension(1, 1));
    DefaultTableModel dtm = (DefaultTableModel)getModel();
    dtm.addColumn("Username");
    dtm.addColumn("Status");

    getColumn("Username").setMinWidth(115);
    getColumn("Username").setMaxWidth(115);
    setRowHeight(20);

    addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            if(SwingUtilities.isRightMouseButton(e)) {
                JTable source = (JTable)e.getSource();
                int row = source.rowAtPoint(e.getPoint());
                int column = source.columnAtPoint(e.getPoint());
                if(!source.isRowSelected(row))
                    source.changeSelection(row, column, false, false);
                buildPopup(row, e.getX(), e.getY());
            }
        }
    });
}

Any idea how to change the size of the friends list? (Extends JTable)

Note: I have removed all "sizing" code from the friendslist, because I can't figure it out and wanted to give a "clean" solution, even without setting the bounds, it still pouplates at the same size. It's like trying to set the size manually is completely pointless.

This is a basic example of how it might be possible to generate a similar layout using layout managers. This example uses BorderLayout , FlowLayout and GridBagLayout

It also shows how to generate fixed width columns ;)

布局

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class TestLayout {

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

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JPanel content = new JPanel() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(200, 200);
                    }
                };

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(content);
                frame.add(new FriendsPanel(), BorderLayout.EAST);
                frame.add(new MessagePane(), BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class FriendsPanel extends JPanel {

        public FriendsPanel() {
            setLayout(new BorderLayout());
            JLabel header = new JLabel("Social Pane");
            header.setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(8, 8, 8, 8)));
            add(header, BorderLayout.NORTH);

            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("");
            model.addColumn("");

            model.addRow(new Object[]{"er3107", "Offline"});
            model.addRow(new Object[]{"er4360", "Online"});
            model.addRow(new Object[]{"er187", "Offline"});
            model.addRow(new Object[]{"er1040", "Online"});
            model.addRow(new Object[]{"er427", "Online"});
            model.addRow(new Object[]{"er4140", "Online"});
            model.addRow(new Object[]{"er835", "Offline"});
            model.addRow(new Object[]{"er2045", "Online"});
            model.addRow(new Object[]{"er4525", "Online"});
            model.addRow(new Object[]{"er4864", "Offline"});

            JTable table = new JTable(model);
            table.setPreferredScrollableViewportSize(new Dimension(100, 200));
            table.setFillsViewportHeight(true);

            Font font = table.getFont();
            FontMetrics fm = table.getFontMetrics(font);

            TableColumnModel cm = table.getColumnModel();
            TableColumn column = cm.getColumn(0);
            int width = fm.stringWidth("M") * 8;
            column.setWidth(width);
            column.setMaxWidth(width);
            column.setMinWidth(width);
            column.setPreferredWidth(width);

            column = cm.getColumn(1);
            column.setPreferredWidth(width);

            add(new JScrollPane(table));

            JButton btnF = new JButton("F");
            JButton btnI = new JButton("I");

            JPanel buttons = new JPanel();
            buttons.setBorder(new CompoundBorder(new EtchedBorder(), new EmptyBorder(8, 8, 8, 8)));
            buttons.add(btnF);
            buttons.add(btnI);
            add(buttons, BorderLayout.SOUTH);
        }

    }

    public class MessagePane extends JPanel {

        public MessagePane() {
            setLayout(new BorderLayout());

            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("");
            model.addColumn("");
            model.addColumn("");

            model.addRow(new Object[]{"rank", "user4916", "..."});
            model.addRow(new Object[]{"rank", "user2916", "..."});
            model.addRow(new Object[]{"rank", "user4471", "..."});
            model.addRow(new Object[]{"rank", "user4161", "..."});
            model.addRow(new Object[]{"rank", "user2048", "..."});
            model.addRow(new Object[]{"rank", "user3212", "..."});
            model.addRow(new Object[]{"Admin", "Chris", "Testing..."});

            JTable table = new JTable(model);
            table.setFillsViewportHeight(true);

            Font font = table.getFont();
            FontMetrics fm = table.getFontMetrics(font);

            table.setPreferredScrollableViewportSize(new Dimension(200, fm.getHeight() * 9));

            TableColumnModel cm = table.getColumnModel();
            TableColumn column = cm.getColumn(0);
            int width = fm.stringWidth("M") * 6;
            column.setWidth(width);
            column.setMaxWidth(width);
            column.setMinWidth(width);
            column.setPreferredWidth(width);

            width = fm.stringWidth("M") * 10;
            column = cm.getColumn(1);
            column.setWidth(width);
            column.setMaxWidth(width);
            column.setMinWidth(width);
            column.setPreferredWidth(width);

            add(new JScrollPane(table));

            JPanel buttons = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridx = 0;
            gbc.gridy = 0;
            gbc.weightx = 1;
            gbc.fill = GridBagConstraints.HORIZONTAL;
            buttons.add(new JTextField(5), gbc);
            gbc.gridx++;
            gbc.weightx = 0;
            buttons.add(new JButton("Send Chat"), gbc);
            add(buttons, BorderLayout.SOUTH);
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM