Solved

iPadx

Posted on 2004-10-05
8
363 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
Comment Utility
>> 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
Comment Utility
>> 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
Comment Utility
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
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cathalmchale
Comment Utility
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
Comment Utility
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
Comment Utility
thankyou,
they both work - i quite like the renderer solution, i just modified the constructor a little.
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 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

9 Experts available now in Live!

Get 1:1 Help Now