java - Why can't I add an EmptyBorder to a custom JComponent? -
my code unable set border of custom jcomponent object new emptyborder(50, 50, 50, 50)
. looks on monitor (as can see, works jbutton not custom jcomponent):
import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class sandbox { public static void main(string[] args) { // instantiate components needed. jframe frame = new jframe(); jpanel panel = new jpanel(); frame.add(panel); widecomponent wc = new widecomponent(); jbutton button = new jbutton("test"); // add borders emptyborder empty1 = new emptyborder(50, 50, 50, 50); wc.setborder(empty1); emptyborder empty2 = new emptyborder(50, 50, 50, 50); button.setborder(empty2); // add stuff panel panel.add(wc); panel.add(button); // add panel frame, show frame frame.add(panel); frame.setvisible(true); frame.pack(); } } class widecomponent extends jcomponent { final int width = 500; final int height = 150; // drawing method public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.blue); g.fillrect(0, 0, width, height); } // jcomponent sizing methods public dimension getpreferredsize() { return new dimension(width, height); } public dimension getmaximumsize() { return getpreferredsize(); } public dimension getminimumsize() { return getpreferredsize(); } }
your borders working fine -- it's empty border being painted blue because you're painting entire component , border part of component. example:
import java.awt.*; import javax.swing.*; import javax.swing.border.*; public class sandbox { public static void main(string[] args) { jframe frame = new jframe(); frame.setdefaultcloseoperation(jframe.dispose_on_close); jpanel panel = new jpanel(); frame.add(panel); widecomponent wc = new widecomponent(); jbutton button = new jbutton("test"); wc.setborder(createborder()); button.setborder(createborder()); panel.add(wc); panel.add(button); frame.add(panel); frame.setvisible(true); frame.pack(); } private static border createborder() { border innerborder = borderfactory.createlineborder(color.red); int eb = 50; border outerborder = borderfactory.createemptyborder(eb, eb, eb, eb); border comboborder = borderfactory.createcompoundborder(outerborder, innerborder); return comboborder; } } class widecomponent extends jcomponent { final int width = 500; final int height = 150; public widecomponent() { setopaque(true); } // drawing method public void paintcomponent(graphics g) { super.paintcomponent(g); g.setcolor(color.blue); g.fillrect(0, 0, width, height); } // jcomponent sizing methods public dimension getpreferredsize() { return new dimension(width, height); } public dimension getmaximumsize() { return getpreferredsize(); } public dimension getminimumsize() { return getpreferredsize(); } }
if want blank border around widecomponent, nest inside jpanel, , give border nesting jpanel. instance, code above changed addition of:
jpanel nestingjpanel = new jpanel(); nestingjpanel.add(new widecomponent()); nestingjpanel.setborder(createborder()); panel.add(wc); panel.add(button); panel.add(nestingjpanel);
Comments
Post a Comment