Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
EmptyBorder seems not working
Jack Kenshin
Greenhorn
Posts: 13
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm trying to put some space between left jpanel and right jpanel using EmptyBorder. But, It seems to not work. Is there any where may be skipped?
import java.awt.*; import java.awt.event.*; import java.io.IOException; import java.net.InetAddress; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.border.EtchedBorder; class PanelDemo { JLabel jlab; InetAddress ip = InetAddress.getByName("10.0.0.32"); //Demo test = new Demo(ip , 4001); String response; JButton jbtnUpArrowFirst; JButton jbtnDownArrowFirst; JButton jbtnUpArrowSec; JButton jbtnDownArrowSec; JButton jbtnUpArrowThird; JButton jbtnDownArrowThird; JLabel jlblLedUpFirst; JLabel jlblLedDownFirst; JLabel jlblLedUpSec; JLabel jlblLedDownSec; JLabel jlblLedUpThird; JLabel jlblLedDownThird; private JComponent ui = null; private Insets buttonMargin = new Insets(10,10,10,10); PanelDemo() throws IOException { InitComponents(); } private void InitComponents() { if (ui != null) return; ui = new JPanel(new BorderLayout(4, 4)); ui.setBorder(new EmptyBorder(4, 4, 4, 4)); int gap = 5; JPanel jpnl = new JPanel(new GridLayout(2, 4, gap, gap)); jpnl.setBorder(new EmptyBorder(0, 0, 0, 50)); jpnl.setOpaque(true); jpnl.setBorder( BorderFactory.createTitledBorder("1 - 2")); JPanel jpnl2 = new JPanel(new GridLayout(2, 2, gap, gap)); jpnl2.setOpaque(true); jpnl2.setBorder( BorderFactory.createTitledBorder("Vertical - Horizontal")); JPanel jpnl3 = new JPanel(new GridLayout(2, 2, gap, gap)); jpnl3.setOpaque(true); jpnl3.setBorder( BorderFactory.createTitledBorder("Test Title")); JFrame jfrm = new JFrame("Use Two JPanels"); jfrm.setLocationByPlatform(true); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jfrm.setMinimumSize(jfrm.getSize()); jbtnUpArrowFirst = setMyButton("/Users/Jack/Desktop/up.png", "upfirst"); jbtnUpArrowSec = setMyButton("/Users/Jack/Desktop/up.png", "upsec"); jbtnUpArrowThird = setMyButton("/Users/Jack/Desktop/up.png", "upthird"); jbtnDownArrowFirst = setMyButton("/Users/Jack/Desktop/down.png", "downfirst"); jbtnDownArrowSec = setMyButton("/Users/Jack/Desktop/down.png", "downsec"); jbtnDownArrowThird = setMyButton("/Users/Jack/Desktop/down.png", "downthird"); jlblLedUpFirst = setMyIcons("/Users/Jack/Desktop/ledOff.png", "upledfirst"); jlblLedUpSec = setMyIcons("/Users/Jack/Desktop/ledOff.png", "upledsec"); jlblLedUpThird = setMyIcons("/Users/Jack/Desktop/ledOff.png", "upledthird"); jlblLedDownFirst = setMyIcons("/Users/Jack/Desktop/ledOff.png", "downledfirst"); jlblLedDownSec = setMyIcons("/Users/Jack/Desktop/ledOff.png", "downledsec"); jlblLedDownThird = setMyIcons("/Users/Jack/Desktop/ledOff.png", "downledthird"); // Add the buttons and label to the panel. jpnl.add(jbtnUpArrowFirst); jpnl.add(jlblLedUpFirst); jpnl.add(jbtnUpArrowSec); jpnl.add(jlblLedUpSec); jpnl.add(jbtnDownArrowFirst); jpnl.add(jlblLedDownFirst); jpnl.add(jbtnDownArrowSec); jpnl.add(jlblLedDownSec); jpnl2.add(jbtnUpArrowThird); jpnl2.add(jlblLedUpThird); jpnl2.add(jbtnDownArrowThird); jpnl2.add(jlblLedDownThird); ui.add(jpnl, BorderLayout.CENTER); ui.add(jpnl2, BorderLayout.LINE_END); ui.add(jpnl3, BorderLayout.PAGE_END); /*jbtnUpArrowFirst.addActionListener(e -> { try { test.send("ANTSW=VERT\r\n"); //if (test.recv().equalsIgnoreCase("")) } catch (IOException e1) { e1.printStackTrace(); } }); jbtnDownArrowFirst.addActionListener(e -> { try { test.send("ANTSW=HORZ\r\n"); } catch (IOException e1) { e1.printStackTrace(); } });*/ jfrm.setContentPane(getUI()); jfrm.pack(); jfrm.setVisible(true); } public JComponent getUI() { return ui; } private JButton setMyButton(String path,String text) { JButton b = new JButton(text, new ImageIcon(path)); //Font f = b.getFont(); //b.setFont(f.deriveFont(f.getSize()*3f)); b.setVerticalTextPosition(SwingConstants.BOTTOM); b.setHorizontalTextPosition(SwingConstants.CENTER); b.setMargin(buttonMargin); return b; } private JLabel setMyIcons(String path, String text) { JLabel jlbl = new JLabel(text, new ImageIcon(path), SwingConstants.CENTER); jlbl.setVerticalTextPosition(SwingConstants.BOTTOM); jlbl.setHorizontalTextPosition(SwingConstants.CENTER); return jlbl; } public static void main(String args[]) { EventQueue.invokeLater(() -> { try { new PanelDemo(); } catch (IOException e) { e.printStackTrace(); } }); } }
Jack Kenshin
Greenhorn
Posts: 13
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
My absent-mindedness. Because of lack of ability to edit my posts, I'm wiring the post as new answer.
JPanel jpnl = new JPanel(new GridLayout(2, 4, gap, gap)); jpnl.setBorder(new EmptyBorder(0, 0, 0, 50)); jpnl.setOpaque(true); jpnl.setBorder(BorderFactory.createTitledBorder("1 - 2"));
The issue is here. Now, how can I use both emptyborder and titledborder together?
Jack Kenshin
Greenhorn
Posts: 13
posted 7 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Solution for ones who would encounter that CompoundBorder is cut off for the intention to use two component together.
Example usage
//jpnl.setBorder(new EmptyBorder(0, 0, 0, 50)); jpnl.setOpaque(true); //jpnl.setBorder(BorderFactory.createTitledBorder("1 - 2")); jpnl.setBorder( new CompoundBorder(new EmptyBorder(0, 0, 0, 50), BorderFactory.createTitledBorder("1 - 2")));
Campbell Ritchie
Marshal
Posts: 79151
377
posted 7 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Well done posting the solution
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Why is my UI broken?
Frames
how to create a search engine?
Lottery GUI
More...