hi,
I am working on the GUI. There are four buttons. I wanted the button's width to be just enough to show the text inside the button and not "...." or bigger than required. How should I edit my code, which is shown below?
Thanks in advance
regards
/*
* GridBagLayoutDemo.java is a 1.4 application that requires no other files.
*/
import java.awt.*;
import javax.swing.*;
public class GridBagLayoutDemo {
final static boolean shouldFill = true;
final static boolean shouldWeightX = true;
final static boolean RIGHT_TO_LEFT = false;
public static void addComponentsToPane(Container pane) {
if (RIGHT_TO_LEFT) {
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
GridBagLayout gbl = new GridBagLayout();
container.setLayout(gbl);
gbl.layoutContainer(container);
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL; //natural height, maximum width
recv = new JEditorPane();
recv.setEditorKit(new HTMLEditorKit());
recv.setEditable(false);
JScrollPane pane
= new JScrollPane(recv,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
c.ipady = 200; //make this component tall
c.weightx = 1.0;
c.weighty = 1.0; //request any extra vertical space
c.gridwidth = 8;
c.gridx = 0;
c.gridy = 0;
container.add(pane, c);
to_reply = new JCheckBox("Reply to latest message", true);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4;
container.add(to_reply, c);
type = new JTextArea();
type.setFont(new Font("Arial",Font.PLAIN,11));
type.setLineWrap(true);
JScrollPane typepane
= new JScrollPane(type,
JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
c.ipady = 50;
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 6;
container.add(typepane, c);
send = new JButton("Send");
c.fill = GridBagConstraints.NONE;
c.gridx = 5;
c.gridy = 2;
c.ipady = 10;
c.anchor = GridBagConstraints.LINE_END;
container.add(send, c);
String num_msg_just_inserted = "Newly inserted messages : 0";
num_msg_inserted = new JLabel(num_msg_just_inserted);
c.gridx = 0;
c.weightx = 1.0;
c.gridy = 3;
//c.gridwidth = 2;
container.add(num_msg_inserted, c);
see = new JButton("See");
see.setBounds(235,220,65,20);
see.setEnabled(false);
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
c.ipadx = 2;
c.ipady = 2;
c.gridx = 1;
c.gridy = 3;
container.add(see, c);
previous_msgs = new previous_messages(thisframe);
previous_msgs.setBounds(10,200,220,20);
msgPosiArray = new msgPositionArray();
c.weightx = 1.0;
c.gridx = 0;
c.gridy = 4;
c.gridwidth = 4;
container.add(previous_msgs, c);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create and set up the window.
JFrame frame = new JFrame("GridBagLayoutDemo");
this.setSize(550,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Set up the content pane.
addComponentsToPane(frame.getContentPane());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}