-1

When i increase the window size to full, my components goes back to standard layout(jtable,button1,button2,button3) and so on. so i wonder if my code is right and how i can decrease the window size.

    JTabbedPane jtabbed = new JTabbedPane(JTabbedPane.TOP);


JPanel panel=new JPanel();
tabellinnhold = new DefaultTableModel(defaulttabell,kolonnenavn);
posttabell = new JTable(tabellinnhold);
rullefelt = new JScrollPane(posttabell);

koble = new JButton("koble til");
lukke = new JButton("lukke");
hente = new JButton("Hente data");
avslutt = new JButton("Avslutt");  
panel.add(rullefelt,BorderLayout.CENTER);
panel.add(koble,BorderLayout.SOUTH);
panel.add(lukke,BorderLayout.SOUTH);
panel.add(hente,BorderLayout.SOUTH);
panel.add(avslutt,BorderLayout.SOUTH);
//action listener
koble.addActionListener(this);
lukke.addActionListener(this);
hente.addActionListener(this);
avslutt.addActionListener(this);
jtabbed.add("se post",panel);
add(jtabbed);

//////////////////////////////////////////////////

Grensesnitt p = new Grensesnitt();


p.setDefaultCloseOperation(EXIT_ON_CLOSE);
p.GUIcode();
p.setTitle("title");
p.setSize(500,700);
p.setVisible(true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height - to show how the extra space should be used. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. 4) A single blank line of white space in source code is all .. – Andrew Thompson Apr 12 '19 at 15:30
  • 2
    .. that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 5) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 6) `p.setSize(500,700);` is a guess. The actual size needed is given by `p.pack()`. – Andrew Thompson Apr 12 '19 at 15:30

1 Answers1

2
JPanel panel=new JPanel();
...
panel.add(rullefelt,BorderLayout.CENTER);
panel.add(koble,BorderLayout.SOUTH);
panel.add(lukke,BorderLayout.SOUTH);
panel.add(hente,BorderLayout.SOUTH);
panel.add(avslutt,BorderLayout.SOUTH);

The default layout manager for a JPanel is a FlowLayout which will simply display all the components on a single line.

You can't just use the BorderLayout constraints and expect it to work.

If you want to use a BorderLayout then the code should be:

//JPanel panel=new JPanel();
JPanel panel=new JPanel( new BorderLayout() );

Also, you can't add 4 components to the "SOUTH" of the BorderLayout. You can only add a single component. So you need to create a child panel and add your components to that first:

JPanel south = new JPanel();
south.add(koble);
south.add(lukke);
south.add(hente);
south.add(avslutt);
panel.add(south, Borderlayout.SOUTH);

Read the section from the Swing tutorial on Using Layout Manager for more information and working examples to get you started.

Keep a link to the tutorial handy for examples of all Swing basics.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • @Beefy_Carrot, glad it helped. Don't forget to "accept" the answer by clicking on the checkmark, so people know the problem has been solved. – camickr Apr 12 '19 at 16:43