Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Swing bug? cannot set width of JToggleButton

843805Dec 1 2006 — edited Dec 3 2006
Hello,
Just wondered if this was a Swing bug. See also
bug 6349010.
The width of the JToggleButton cannot be set.
However the height can be set.
The important line is line 135 - and also 140.
Try changing the width of the button - it does not change.
thanks,
Anil

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.EventObject;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.JTree;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellEditor;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;

public class TreeUIFailed extends JPanel {
	  AnilTreeCellRenderer3 atcr;
	  AnilTreeCellEditor4 atce;
	  DefaultTreeModel treeModel;
	  JTree tree;
	  DefaultMutableTreeNode markedNode = null;
	  
	public TreeUIFailed() {
		  super(new BorderLayout());
			treeModel = new DefaultTreeModel(null);
			tree = new JTree(treeModel);		
		    tree.setEditable(true);
			tree.getSelectionModel().setSelectionMode(
					TreeSelectionModel.SINGLE_TREE_SELECTION);
			tree.setShowsRootHandles(true);
		    tree.setCellRenderer(atcr = new AnilTreeCellRenderer3());
		    tree.setCellEditor(atce = new AnilTreeCellEditor4(tree, atcr));
		    tree.setRowHeight(0);//TEMP - needed only if setting Win L&F
			JScrollPane scrollPane = new JScrollPane(tree);
			add(scrollPane,BorderLayout.CENTER);
	}
	public void setRootNode(DefaultMutableTreeNode node) {
		treeModel.setRoot(node);
		treeModel.reload();
	}
	  public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
//		  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		  TreeUIFailed tb = new TreeUIFailed();
		  tb.setPreferredSize(new Dimension(800,600));
		    JFrame frame = new JFrame("Tree Windows UI Failed");
		    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		    frame.setContentPane(tb);
		    frame.setSize(800, 600);
		    frame.pack();
		    frame.setVisible(true);
		    tb.populate();
	  }

	private void populate() {
		TextAreaNode3 r = new TextAreaNode3(this);
		 setRootNode(r);
			r.gNode.notes.addTab("0", null/* icon */, new JTextArea(2,25),
			"no menu!");
			r.gNode.notes.addTab("1", null/* icon */, new JTextArea(2,25),
			"no menu!");
			TextAreaNode3 a = new TextAreaNode3(this);
			a.gNode.notes.addTab("1", null/* icon */, new JTextArea(2,25),
			"no menu!");
		 treeModel.insertNodeInto(a, r, r.getChildCount());		
	}
}

class AnilTreeCellRenderer3 extends DefaultTreeCellRenderer{
	TreeUIFailed panel;
 DefaultMutableTreeNode currentNode;
 
  public AnilTreeCellRenderer3() {
	super();
}

public Component getTreeCellRendererComponent
   (JTree tree, Object value, boolean selected, boolean expanded,
   boolean leaf, int row, boolean hasFocus){
	TextAreaNode3 currentNode = (TextAreaNode3)value;
	NodeGUI4 gNode = (NodeGUI4) currentNode.gNode;
    return gNode.vBox;
  }
}
 
class AnilTreeCellEditor4 extends DefaultTreeCellEditor{
  DefaultTreeCellRenderer rend;
 
  public AnilTreeCellEditor4(JTree tree, DefaultTreeCellRenderer r){
    super(tree, r);
    rend = r;
  }
 
  public Component getTreeCellEditorComponent(JTree tree, Object value,
   boolean isSelected, boolean expanded, boolean leaf, int row){
    return rend.getTreeCellRendererComponent(tree, value, isSelected, expanded,
     leaf, row, true);
  }
 
  public boolean isCellEditable(EventObject event){
    return true;
  }
}
/**
 * this is done to keep gui separate from model - as in MVC.
 * not necessary.
 * @author juwo
 *
 */
class NodeGUI4 {
	JPanel notesPanel = new JPanel(new BorderLayout(), true);
	JTabbedPane notes = new JTabbedPane(JTabbedPane.RIGHT);
	final TreeUIFailed view;
	Box vBox = Box.createVerticalBox();
	Box hBox = Box.createHorizontalBox();
	final JTextArea textArea = new JTextArea( 1, 5 );
	JToggleButton toggleButton = new JToggleButton();

	NodeGUI4( TreeUIFailed view_ ) {
		this.view = view_;
		toggleButton.setAlignmentX(Component.CENTER_ALIGNMENT);
// BEGIN PROBLEM		
		toggleButton.setPreferredSize(new Dimension(200,toggleButton.getPreferredSize().height));
// END PROBLEM
		vBox.add(toggleButton);
		vBox.add(hBox);
		hBox.add( textArea );
		textArea.setBorder( BorderFactory.createMatteBorder( 0, 0, 1, 0, Color.GREEN ) );
		notesPanel.add(notes);
		hBox.add( notesPanel);
		hBox.setBorder( BorderFactory.createMatteBorder( 5, 5, 5, 5, Color.CYAN ) );

// THE FOLLOWING DOES NOT WORK EITHER!!!		
		//		halo.setPreferredSize(new Dimension(vBox.getPreferredSize().width,halo.getPreferredSize().height));
	}
}

class TextAreaNode3 extends DefaultMutableTreeNode {   
	NodeGUI4 gNode;
	TextAreaNode3(TreeUIFailed view_t) {	
		gNode = new NodeGUI4(view_t);		
	}
}

Comments

843785
In Java all calls to static method are statically bound (and only those calls).

All calls to non-static methods are dynamically bound in Java.

The code of your example only presents dynamic binding.
788165
Try to think of "binding" as when_ Java makes the connection between the name of your method and the code it executes.
With static binding (early), that association happens when you compile your program.
With dynamic binding (late), that association doesn't happen until the program is actually running.
843785
Is there any reason static binding couldn't be used when a final method is invoked?
843785
final methods cannot be overriden so they must be statically binded. are they ?
843785
NarutoUzumaki wrote:
final methods cannot be overriden so they must be statically binded. are they ?
javap says "invokevirtual"
788165
NarutoUzumaki wrote:
final methods cannot be overriden . . .
But they can be overloaded+.
843785
LazarusLong wrote:
NarutoUzumaki wrote:
final methods cannot be overriden . . .
But they can be overloaded+.
I'm oversexed. Does that count?
788165
DrLaszloJamf wrote:
LazarusLong wrote:
NarutoUzumaki wrote:
final methods cannot be overriden . . .
But they can be overloaded+.
I'm oversexed. Does that count?
I'm not sure how that helps the OP.
Am I missing something?
788165
+" . . . and there's no runtime polymorphism on them."+

Even in the case of method overloading?
843785
NarutoUzumaki wrote:
Well i have understood a little about static binding and dynamic binding but still missing a few puzzles.

Static binding happens when we call a static member of a class or an instance method that is private or when i implement method overloading.

Dynamic binding happens when i use Virtual Method Invocation or Override a method.
Static/dynamic binding refers to WHEN it's decided which method is to be called, at compile time (static binding) or at runtime (dynamic binding).

In principle any method that can be overriden is bound dynamically. This means any non-static, non-final or non-private method is bound dynamically. BUT due to clever optimization at runtime methods that are bound dynamically but aren't overridden are called as if they were statically bound.
843785
jverd wrote:
JoachimSauer wrote:
In Java all calls to static method are statically bound (and only those calls).

All calls to non-static methods are dynamically bound in Java.
I'm fairly certain that final and private methods are also statically bound, since, like static methods, they can't be overridden and there's no runtime polymorphism on them.
Final methods can still be dynamically bound (if they override a non-final method and the non-final method is called)

But other than that, this is the reason why I always have a bad feeling about posting that kind of high-level overview: I feel like I always forget some cases and therefore write illegitimate generalizations.
843785
LazarusLong wrote:
+" . . . and there's no runtime polymorphism on them."+

Even in the case of method overloading?
Overloading is always resolved at compile time (i.e. the exact signature of the method to be called is decided at compile time). Only Overriding is done at runtime (i.e. the exact method to be called from among the methods with the given signature is decided at runtime).
788165
JoachimSauer wrote:
LazarusLong wrote:
+" . . . and there's no runtime polymorphism on them."+

Even in the case of method overloading?
Overloading is always resolved at compile time (i.e. the exact signature of the method to be called is decided at compile time). Only Overriding is done at runtime (i.e. the exact method to be called from among the methods with the given signature is decided at runtime).
Understood.
Thank you.
1 - 13
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 30 2006
Added on Dec 1 2006
4 comments
1,114 views