Solved

iPadx

Posted on 2004-10-05
8
382 Views
Last Modified: 2010-03-31
Hi,

I quite like the iPad functionality in GridBagLayout as it means I can for example, set a JComboBox so that its minimum size will always be big enough to display all the text and not have "..."
  I'm using a different layout manager and I want this iPad functionality. I guess I can get it by specifying a fixed minimum size, but then I have to specify a fixed Dimension - with iPadx I could just do iPadx = 10;  and not have to worry about the component height, so if I'm never fixing any components height then it is just using the swing defaults which is what I want.

Badly worded question, but basically how do I have iPadx functionality using a different layout manager other than GridBagLayout??

Cheers,
Cathal.
0
Comment
Question by:cathalmchale
  • 5
  • 3
8 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 12225572
>> set a JComboBox so that its minimum size will always be big enough to display all the text and not have "..."
I think that if you put the JComboBox in a simple FlowLayout you'll automatically have this behaviour.
0
 

Author Comment

by:cathalmchale
ID: 12225861
>> I think that if you put the JComboBox in a simple FlowLayout you'll automatically have this behaviour.

perhaps, but if i want it to be a little bit bigger again eg, 5 or 10 pixels bigger (so that it doesnt appear squashed - a little bit of breathing room!)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12225921
That's something I solve by setting my own ListCellRenderer which has

           this.setBorder(new EmptyBorder(0,5,0,5));  // top, left, bottom, right

in it's constructor.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12226031
Forget the previous:

/*
 * ExtraSpaceListCellRenderer.java
 *
 */

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

public class ExtraSpaceListCellRenderer extends DefaultListCellRenderer {
   
    /** Creates a new instance of LocalizedListCellRenderer */
    public ExtraSpaceListCellRenderer() {
    }
   
    public Component getListCellRendererComponent(
                                JList list, Object value, int index,
                                boolean isSelected, boolean cellHasFocus) {
        String displayString = "";
        if (value != null)
            displayString = "  " + value.toString() + "  ";
        return super.getListCellRendererComponent(
                list, displayString, index, isSelected, cellHasFocus);
    }    
}

and

/*
 * ComboDemo.java
 *
 */

import javax.swing.*;

public class ComboDemo extends javax.swing.JPanel {

    private javax.swing.JComboBox theComboBox;
   
    /** Creates new form ComboDemo */
    public ComboDemo() {
        initComponents();
       
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement("Element one");
        model.addElement("Element two");
        model.addElement("Element eleven");
        model.addElement("This is a rather long one");
        theComboBox.setModel(model);
       
        theComboBox.setRenderer( new ExtraSpaceListCellRenderer() );
    }
   
    private void initComponents() {
        theComboBox = new javax.swing.JComboBox();
        add(theComboBox);
    }

   
    public static void main(String args[]) {
        // Set the windows Look & Feel
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) { }

        JFrame f = new JFrame("Demo");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add( new ComboDemo() );
        f.setSize(500,500);
        f.setLocationRelativeTo(null);
        f.show();
    }
}

Remark: This is in the assumption that with "bigger" you meant wider (and not higher)
0
 

Author Comment

by:cathalmchale
ID: 12226032
i added this code but it doesnt seem to have had any effect ??

opCombo.setRenderer(new DefaultListCellRenderer());
((DefaultListCellRenderer)opCombo.getRenderer()).setBorder(new EmptyBorder(0,20,0,20));
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12226134
I've re-read your question:
>>I guess I can get it by specifying a fixed minimum size, but then I have to specify a fixed Dimension -
>>with iPadx I could just do iPadx = 10;  and not have to worry about the component height,
>>so if I'm never fixing any components height then it is just using the swing defaults which is what I want.

Try this in the above code:

    public ComboDemo() {
        initComponents();
       
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        model.addElement("Element one");
        model.addElement("Element two");
        model.addElement("Element eleven");
        model.addElement("This is a rather long one");
        theComboBox.setModel(model);

        Dimension d = theComboBox.getPreferredSize();
        d.setSize( 250, d.getHeight() );
        theComboBox.setMinimumSize(d);
        theComboBox.setPreferredSize(d);
    }
0
 

Author Comment

by:cathalmchale
ID: 12226515
thankyou,
they both work - i quite like the renderer solution, i just modified the constructor a little.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12226558
Thanks :)
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 85
VB Script to add site to Java Exception List 4 79
oracle 11g 23 84
maven module vs maven project 3 23
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

821 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