Question

I noticed very strange behavior of my program today. Basically I have JToolBar uder which is JScrollPane with JTable. Both inside JPanel inside JFrame. Every container using MigLayout.

Now, if I start app, this is its default look: enter image description here

But, if I move JToolBar and clip it back to its original position, now it looks like this:enter image description here

Suddenly there are no borders. I would preffer if there werent any at all at first place, but changing look of GUI is not good feature at all... Please if you know what is wrong, help :)

CODE:

public class Gui extends JFrame {

private static final long serialVersionUID = 1L;
private JPanel mainPnl = null;
private JToolBar toolbar = null;
private Session session = null;

public Gui(Session session) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
    } catch (ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    }

    this.session = session;

    setTitle("PRO2-Contact Manager v_0.1");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(800,420);
    setResizable(true);

    initMenu();
    initMainPnl();
    initToolbar();
    initTable();

    // KeyboardFocusManager manager =
    // KeyboardFocusManager.getCurrentKeyboardFocusManager();
    // manager.addKeyEventDispatcher(new MyDispatcher(aList));

    setLocationRelativeTo(null);
    setVisible(true);
}

private void initMenu() {
    JMenuBar menu = new JMenuBar();
    MenuListener ml = new MenuListener();

    JMenu file = new JMenu("File");
    file.setMnemonic(KeyEvent.VK_F);
    menu.add(file);

    JMenuItem exit = new JMenuItem("Exit");
    exit.setMnemonic(KeyEvent.VK_E);
    exit.addActionListener(ml);
    file.add(exit);

    JMenu help = new JMenu("Help");
    help.setMnemonic(KeyEvent.VK_H);
    menu.add(help);

    JMenuItem controls = new JMenuItem("Controls");
    controls.setMnemonic(KeyEvent.VK_C);
    controls.addActionListener(ml);
    help.add(controls);

    JMenuItem about = new JMenuItem("About");
    about.setMnemonic(KeyEvent.VK_A);
    about.addActionListener(ml);
    help.add(about);

    setJMenuBar(menu);
}

private void initMainPnl(){
    mainPnl = new JPanel(new MigLayout());

    add(mainPnl);
}

private void initToolbar() {
    toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.add(new JButton());
    mainPnl.add(toolbar,"wrap");
}

private void initTable() {
    MyTable table = new MyTable(new MyTableModel(this));
    JScrollPane sp = new JScrollPane(table);
    sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

    mainPnl.add(sp,"w 100%, h 100%");
}}
Was it helpful?

Solution

Technically, a JToolBar can be added to a container with any layout constraint. Automatic re-adding a floating toolBar to the container is supported only if for a BorderLayout. Instead of adding to the mainPanel, add it to the contentPane:

private void initToolbar() {
    toolbar = new JToolBar(JToolBar.HORIZONTAL);
    toolbar.add(new JButton());
    add(toolbar, BorderLayout.NORTH);
}

BTW: your code doesn't compile outside of your context - MyPanel, MyTableModel, MenuListener are local classes not available anywhere else plus it's missing a main method. For better help sooner, consider to supply a SSCCE in future :-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top