Solved

Button's Size in JSplitPane

Posted on 2006-07-24
8
515 Views
Last Modified: 2008-02-26
I built a frame with JSplitPane and I added two JButton (s) to the right JPanel and JTextArea left JPanel.

My problem is how to change the size of the button because I want the two buttons have same size; I tried:

button.setSize(int,int);

but, it did not work.

I played with it by a text method which is not appropriate one:

button1.setText("Press me");

button2.setText("Do not press me");


-------->


button1.setText("      Press me      ");

button2.setText("Do not press me");


The last two buttons have somehow same size.
0
Comment
Question by:javamate06
  • 4
  • 3
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17167317
>>button1.setText(“      Press me      ”);

That's actually quite a good kludge ;-) Otherwise you need to choose the correct layout manager
http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html
0
 

Author Comment

by:javamate06
ID: 17167509
OK...

which layout do you think it will do my job?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17167536
You might use a BoxLayout. Have a look at that one
0
 

Author Comment

by:javamate06
ID: 17167572
Do you mean that I can use BoxLayout with JSplitPane

OR

BoxLayout for the button and another BoxLayout for the JList?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 17167694
0
 

Author Comment

by:javamate06
ID: 17173740

I chose for one of my JPanel Boxlayout but the problem when I add the buttons or labels they aligned to the left top corner I tried to change the size and location but no change in view, why this problem is faced?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 40 total points
ID: 17173925
>>I chose for one of my JPanel

Let's call that panel1. Use *two* panels and set the box layout on panel2. Do the following

panel1.setLayout(new BorderLayout());
panel1.add(panel2, BorderLayout.CENTER);
0
 

Author Comment

by:javamate06
ID: 17174937

I am trying to use CardLayout and in my list "JList" the JScrollPane did not appear and instead of appearing the size of frame is increase the size. So,

How to make the size of the frame fixed and the ScrollPane is appear instead of it?

I tried these to methods but nothing change.

frame.setSize(new Dimension(100,100));
frame.setResizable(false);


-----THIS IS MY JAVA FILE, I TAKE IT FROM java.sun.com-----
-----------------------------------------------------------------------

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;

public class CardLayoutDemo implements ItemListener {
    JPanel cards; //a panel that uses CardLayout
    final static String BUTTONPANEL = "JPanel with JButtons";
    final static String TEXTPANEL = "JPanel with JTextField";
    static ArrayList arrayList = new ArrayList();

    public void addComponentToPane(Container pane) {
             
        JPanel listPane = new JPanel();
       
        JList list = new JList(new Vector(arrayList));
        JScrollPane scrollPane = new JScrollPane(list);
        listPane.add(scrollPane);

     
        JPanel card1 = new JPanel();
        card1.add(new JButton("Button 1"));
        card1.add(new JButton("Button 2"));
        card1.add(new JButton("Button 3"));

        JPanel card2 = new JPanel();
        card2.add(new JTextField("TextField", 20));

        //Create the panel that contains the "cards".
        cards = new JPanel(new CardLayout());
        cards.add(card1, BUTTONPANEL);
        cards.add(card2, TEXTPANEL);

        pane.add(listPane, BorderLayout.PAGE_START);
        pane.add(cards, BorderLayout.CENTER);
    }

    public void itemStateChanged(ItemEvent evt) {
        CardLayout cl = (CardLayout)(cards.getLayout());
        cl.show(cards, (String)evt.getItem());
    }

   
    private static void createAndShowGUI() {
       

        //Create and set up the window.
        JFrame frame = new JFrame("CardLayoutDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
       
       

        //Create and set up the content pane.
        CardLayoutDemo demo = new CardLayoutDemo();
        demo.addComponentToPane(frame.getContentPane());

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
   
      arrayList.add("Computer");
      arrayList.add("Java");
      arrayList.add("this line is long line and I want to display it with scrollPane to make the frame size fixed");
        //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();
            }
        });
    }
}

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
what is the difference between "sudo su" and "su - root" 6 106
Java / Linux and Regular Expressions 11 71
servlet  URL Rewriting 1 28
nextBoolean(double p) for Random class 3 35
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now