Fat JButtons in GridLayout

I am adding buttons to a panel with GridLayout, but they appear small... I Set the space between them 5 pixels both vertically and horizontally... Let me give you a comparison:

This is how it looks:

http://i32.photobucket.com/albums/d7/thepicturesofnayon/untitled.jpg

this is how I want it to look:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/doc-files/GridLayout-1.gif

Why do my buttons look so thin and long instead of squareish? Can somebody please help?

Thanks.

[513 byte] By [nayon_7a] at [2007-11-27 7:30:18]
# 1
They're resizing to fit the container. Consider either setting a maximum size for the buttons or using a different layout manager. I don't use GridLayout a whole lot because it doesn't give you a whole lot of control over how things resize.Joe
Joe_ha at 2007-7-12 19:10:29 > top of Java-index,Java Essentials,Java Programming...
# 2
Which layout manager do you suggest?
nayon_7a at 2007-7-12 19:10:29 > top of Java-index,Java Essentials,Java Programming...
# 3
with gridlayout, all the space of the panel should be usedmaybe your code is wrong somewhere
calvino_inda at 2007-7-12 19:10:29 > top of Java-index,Java Essentials,Java Programming...
# 4

Here is the code in its entirety... since its all linked I cannot post a specific part of it..

import java.util.*;

import javax.swing.*;

import javax.swing.event.ChangeEvent;

import javax.swing.event.ChangeListener;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.FileOutputStream;

import java.io.PrintStream;

public class MainPanel extends JFrame

{

private static final long serialVersionUID = 1;

JTextField lolfield;

ArrayList<JTextArea> lolarea;

int areaat;

ArrayList<Integer> datacount;

JPanel panel2;

JPanel panel3;

JPanel panel4;

JPanel labelpanel;

int panelsize;

String sfd;

JLabel enteredit;

JMenuItem noobmode;

JMenuItem editmode;

JMenuBar themenu;

JMenu filemenu;

JPanel mainP;

LButton selectedbutton;

boolean restarting;

JScrollPane scroll;

String password;

JTabbedPane thetabs;

ImageIcon hp;

JFrame thisFrame;

class LButton extends JButton{

public LButton( String text ){

super( text );

this.addActionListener(

new ActionListener(){

public void actionPerformed(ActionEvent arg3) {

if( selectedbutton != null )

selectedbutton.setFocusPainted(false);

selectedbutton = ( LButton )arg3.getSource();

selectedbutton.setFocusPainted(true);

}

}

);

}

}

public MainPanel()

{

super("Hewlett-Packard Knowledge Base");

setSize(1024,768);

restarting = false;

lolarea = new ArrayList<JTextArea>();

datacount = new ArrayList<Integer>();

datacount.add( 0 );

areaat = 0;

hp = new ImageIcon( "hp.gif" );

setIconImage( hp.getImage() );

password = "test";

try

{

//Tell the UIManager to use the platform look and feel

UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

}

catch(Exception e)

{

//Do nothing

}

selectedbutton = null;

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final Container content = getContentPane();

content.setLayout(new BorderLayout());

final JPanel panel3 = new JPanel(new FlowLayout());

JPanel panel;

JButton addbutton = new JButton("Add");

JButton removebutton = new JButton("Remove");

panel4 = new JPanel( new BorderLayout() );

panel4.add( panel3, BorderLayout.SOUTH );

panel2 = new JPanel( new BorderLayout() );

panel2.setName("Introduction");

JButton clear = new JButton( "Add&Clear" );

JButton reset = new JButton("Reset");

thisFrame = this;

enteredit = new JLabel( "Enter Editing Mode from menu to modify data" );

clear.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

sfd = lolfield.getText();

String testlol = lolarea.get(areaat).getText();

if ( testlol.equals( "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" ) ){

lolarea.get(areaat).setText("");

}

if( ! sfd.equals("\n") && ! sfd.equals( "") ){

sfd = sfd + " ?;

lolarea.get(areaat).append( sfd + "\n");

datacount.set(areaat, (datacount.get( areaat ) + 1));

labelpanel.add( new LButton( sfd ) );

}

lolfield.setText("");

labelpanel.revalidate();

thisFrame.repaint();

}

});

JButton edit = new JButton("Edit");

edit.addActionListener(

new ActionListener(){

public void actionPerformed( ActionEvent e ){

selectedbutton.setText( lolfield.getText() + " ? );

}

}

);

reset.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JOptionPane jop = new JOptionPane("Wait!");

int resettest = jop.showConfirmDialog(thisFrame, "Are you sure?");

if (resettest == 0 ){

lolarea.get(areaat).setText("");

datacount.set(areaat, 0);

thetabs.getChangeListeners()[0].stateChanged(new ChangeEvent( thetabs ) );

}

}

});

addbutton.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

//JOptionPane.showMessageDialog(parentComponent, "BUTTON PRESSED!");

sfd = lolfield.getText();

String testlol = lolarea.get(areaat).getText();

if ( testlol.equals( "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" ) ){

lolarea.get(areaat).setText("");

}

if( ! sfd.equals("\n") && ! sfd.equals( "") ){

sfd = sfd + " ?;

lolarea.get(areaat).append( sfd + "\n");

datacount.set(areaat, (datacount.get( areaat ) + 1));

labelpanel.add( new LButton( sfd ) );

}

labelpanel.revalidate();

thisFrame.repaint();

//theList.add(sfd);

}

});

panel3.add(clear);

panel3.add(addbutton);

panel3.add(edit);

removebutton.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

if( selectedbutton != null ){

String texttodel = selectedbutton.getText();

labelpanel.remove( selectedbutton );

lolarea.get(areaat).getText().replace( texttodel, "" );;

datacount.set(areaat, datacount.get(areaat) - 1 );

selectedbutton = null;

labelpanel.revalidate();

thisFrame.repaint();

}

}

}

);

JButton get = new JButton( "Get" );

get.addActionListener( new ActionListener(){

public void actionPerformed( ActionEvent e ){

if( selectedbutton != null )

lolfield.setText( selectedbutton.getText().substring(0, (selectedbutton.getText().length() - 2 ) ) );

}

}

);

panel3.add(get);

panel3.add(removebutton);

JButton savebutton = new JButton("Save");

savebutton.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

FileOutputStream out;

PrintStream p;

try { // Create a new file output stream

// connected to "myfile.txt"

out = new FileOutputStream("saveddata.txt"); // Connect print stream to the output stream

p = new PrintStream( out );

for(int j = 0; j < lolarea.size(); j++){

p.println("" + thetabs.getTitleAt(j));

p.println ( lolarea.get(j).getText() );

p.println("-");

}

p.close();

}

catch (Exception ex) { System.err.println ("Error writing to file");

}

}

});

panel3.add(savebutton);

panel3.add(reset);

JButton newtab = new JButton("New Tab");

newtab.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JPanel a = new JPanel(( new BorderLayout ()));

a.add( labelpanel );

thetabs.addTab( "New Tab",a);

panelsize = thetabs.getTabCount();

areaat = panelsize - 1;

lolarea.add( new JTextArea() );

datacount.add(0);

thetabs.setSelectedIndex(areaat);

lolarea.get(areaat).setEditable(false);

lolarea.get(areaat).setText( "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );

labelpanel.revalidate();

thisFrame.repaint();

}

});

panel3.add( newtab );

JButton nametab = new JButton("Name Tab");

nametab.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

sfd = lolfield.getText();

thetabs.setTitleAt( thetabs.getSelectedIndex() , sfd);

}

});

panel3.add( nametab );

JButton deltab = new JButton("Delete Tab");

deltab.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

if( thetabs.getTabCount() == 1){

JOptionPane afaeg = new JOptionPane();

afaeg.showMessageDialog(thisFrame, "There is already one tab left, use clear instead");

}

else{

thetabs.remove( areaat );

lolarea.remove( areaat );

datacount.remove(areaat);

panelsize--;

if( areaat == 0 ){

areaat = 0;

thetabs.setSelectedIndex(areaat);

System.out.println( "deltab areaat " + areaat );

thetabs.getChangeListeners()[0].stateChanged(new ChangeEvent( thetabs ) );

}

else{

areaat = 0;

thetabs.setSelectedIndex(areaat);

}

}

}

});

panel3.add( deltab );

lolarea.add( new JTextArea());

lolarea.get(areaat).setEditable(false);

lolarea.get(areaat).setText( "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );

//

//panel2.add( new JScrollPane(lolarea.get(areaat)), BorderLayout.NORTH);

//

//

labelpanel = new JPanel();

//BoxLayout layout = new BoxLayout( labelpanel, BoxLayout.Y_AXIS );

GridLayout layout = new GridLayout( 10, 4, 5, 5 );

labelpanel.setLayout( layout );

scroll = new JScrollPane();

labelpanel.setBackground( Color.WHITE );

//scroll.add(labelpanel );

panel2.add( labelpanel, BorderLayout.NORTH );

//

JPanel bigone = new JPanel(new BorderLayout());

thetabs = new JTabbedPane();

thetabs.add( panel2 );

//bigone.add( panel2, BorderLayout.LINE_START );

//content.add( panel2, BorderLayout.CENTER );

thetabs.addChangeListener(

new ChangeListener(){

public void stateChanged(ChangeEvent arg0) {

if(!restarting){

areaat = thetabs.getSelectedIndex();

System.out.println( areaat );

int ctr = 0;

String label = "";

label = lolarea.get(areaat).getText();

String transferred = "";

labelpanel.removeAll();

for( int q = 0; q < datacount.get( areaat ); q++ ){

while( label.charAt(ctr) != '? ){

transferred = label.substring( 0,( ctr ) );

ctr++;

}

if( ! transferred.equals("") )

transferred = transferred + "?;

label = label.substring(ctr + 1);

labelpanel.add( new LButton( transferred ) );

transferred = "";

ctr = 0;

}

JPanel a = new JPanel(( new BorderLayout ()));

a.add( labelpanel );

thetabs.setComponentAt(areaat, a);

labelpanel.revalidate();

thisFrame.repaint();

}

}

}

);

//bigone.add(panel2, BorderLayout.CENTER);

bigone.add(thetabs, BorderLayout.NORTH);

panel = new JPanel(new BorderLayout());

lolfield = new JTextField( "Enter Text Here..." ,10);

panel4.add( lolfield, BorderLayout.NORTH );

//panel.add( clear, BorderLayout.CENTER );

bigone.add(panel, BorderLayout.CENTER);

//content.setBackground( Color.BLUE );

//bigone.setBackground( Color.BLUE );

themenu = new JMenuBar();

//themenu.setBackground( Color.CYAN );

content.add( themenu, BorderLayout.NORTH );

filemenu = new JMenu( "File" );

JMenuItem load = new JMenuItem( "Load" );

JMenuItem about = new JMenuItem( "About" );

editmode = new JMenuItem( "Enter Editing Mode" );

JMenu helpmenu = new JMenu("Help");

JMenuItem usage = new JMenuItem("Usage");

usage.addActionListener(

new ActionListener(){

public void actionPerformed( ActionEvent e ){

JOptionPane aqq = new JOptionPane( "Usage" );

aqq.showMessageDialog(thisFrame, "<HTML><b>Normal Mode:</b></HTML>\nIn this mode you can view entries and tabs, to view the entries in a tab, simply click that tab.\n<HTML><b>Editing Mode:</HTML></b>\nIn this mode, you can add new data and tabs. Function of buttons:\nAdd&Clear: Adds the data in the field to the list and clears the field of that data.\nAdd: Adds the data to the field, but does not retain it in the text box.\nEdit: When a data is selected by clicking and Edit is pressed, the data in the field is copied into the data in the list.\nGet: Gets the data of the selected list item into the field.\nRemove: Removes the selected data from the list.\nSave: Saves the whole list and tabs to a file.\nReset: Clears the current tab.\nNew Tab: Creates a new tab.\nName Tab: Names the current tab with the value in the field.\nDelete Tab: Deletes the current tab and its contents.");

}

}

);

content.add( enteredit, BorderLayout.PAGE_END );

editmode.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JOptionPane abo = new JOptionPane("Wait");

if( password.equals( abo.showInputDialog(thisFrame, "Enter the password to continue"))){

content.remove( enteredit );

filemenu.remove( editmode );

filemenu.add( noobmode );

content.add(panel4, BorderLayout.PAGE_END);

panel4.revalidate();

thisFrame.repaint();

}

else{

abo.showMessageDialog(thisFrame, "Wrong Password!");

}

}

});

noobmode = new JMenuItem( "Exit Editing Mode" );

noobmode.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

filemenu.remove( noobmode );

filemenu.add( editmode );

content.remove( panel4 );

content.add( enteredit, BorderLayout.SOUTH );

panel4.revalidate();

thisFrame.repaint();

}

});

about.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JOptionPane abo = new JOptionPane("About");

abo.showMessageDialog(thisFrame, "Hewlett-Packard Knowledge Base Alpha 0.0\nDesigned and made by H. Noyan Tokg鰖oğlu for Hewlett-Packard Co.\n", "About", 1, hp);

}

});

//about listener

load.addActionListener(

new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

JOptionPane abo = new JOptionPane("Wait!!");

abo.showMessageDialog(thisFrame, "Not functional yet");

}

});

JMenuItem newm = new JMenuItem("New");

newm.addActionListener( new ActionListener(){

public void actionPerformed( ActionEvent e ){

JOptionPane jop = new JOptionPane("Wait!");

int newres = jop.showConfirmDialog(thisFrame, "Are you sure?");

if( newres == 0 ){

restarting = true;

lolarea.clear();

datacount.clear();

labelpanel.removeAll();

lolarea.add( new JTextArea() );

datacount.add(0);

JPanel a = new JPanel(( new BorderLayout ()));

a.add( labelpanel );

thetabs.removeAll();

thetabs.add( a, "Introduction");

panelsize = 1;

areaat = 0;

restarting = false;

thetabs.setSelectedIndex(areaat);

lolarea.get(areaat).setEditable(false);

lolarea.get(areaat).setText( "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" );

labelpanel.revalidate();

thisFrame.repaint();

}

}

}

);

filemenu.add( newm );

filemenu.add( load );

filemenu.add(editmode);

helpmenu.add( about );

helpmenu.add( usage );

themenu.add( filemenu );

themenu.add( helpmenu );

content.add( bigone, BorderLayout.CENTER );

}

}

nayon_7a at 2007-7-12 19:10:29 > top of Java-index,Java Essentials,Java Programming...
# 5

> Here is the code in its entirety... since its all linked I cannot post a specific part of it..

We don't want to see all your code we want to see the code related to the problem. For example the code relating to the ActionListeners has nothing to do with the layout of a component so it is not necessary.

So start by writing your code so that you get the layout correct. Then once the layout is correct you can add the code for the ActionListeners. Don't write the whole program up front without test each step as you go. If you need to make changes then you've just wasted lots of time.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 19:10:29 > top of Java-index,Java Essentials,Java Programming...