Solved

iPadx

Posted on 2004-10-05
8
392 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb on wildfly 5 30
Which non-HTML GUI front end to use with Java? 3 24
Glassfish admin console not working 1 34
Java syntax, or is it Selenium 6 30
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

828 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