List of usage examples for javax.swing JPanel add
public Component add(Component comp)
From source file:List.java
public static void main(String[] args) { final DefaultListModel model = new DefaultListModel(); final JList list = new JList(model); JFrame f = new JFrame(); f.setTitle("JList models"); model.addElement("A"); model.addElement("B"); model.addElement("C"); model.addElement("D"); model.addElement("E"); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); JPanel leftPanel = new JPanel(); JPanel rightPanel = new JPanel(); leftPanel.setLayout(new BorderLayout()); rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS)); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { int index = list.locationToIndex(e.getPoint()); Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = ""; if (text != null) newitem = text.trim(); else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); ListSelectionModel selmodel = list.getSelectionModel(); selmodel.setLeadSelectionIndex(index); }//from w w w . jav a2s. c o m } } }); leftPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); leftPanel.add(new JScrollPane(list)); JButton removeall = new JButton("Remove All"); JButton add = new JButton("Add"); JButton rename = new JButton("Rename"); JButton delete = new JButton("Delete"); add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String text = JOptionPane.showInputDialog("Add a new item"); String item = null; if (text != null) item = text.trim(); else return; if (!item.isEmpty()) model.addElement(item); } }); delete.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index >= 0) model.remove(index); } }); rename.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ListSelectionModel selmodel = list.getSelectionModel(); int index = selmodel.getMinSelectionIndex(); if (index == -1) return; Object item = model.getElementAt(index); String text = JOptionPane.showInputDialog("Rename item", item); String newitem = null; if (text != null) { newitem = text.trim(); } else return; if (!newitem.isEmpty()) { model.remove(index); model.add(index, newitem); } } }); removeall.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { model.clear(); } }); rightPanel.add(add); rightPanel.add(rename); rightPanel.add(delete); rightPanel.add(removeall); rightPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 20)); panel.add(leftPanel); panel.add(rightPanel); f.add(panel); f.setSize(350, 250); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) { JPanel gui = new JPanel(new BorderLayout()); String HTML = "<html>" + "<head>" + "<style type=text/css>" + "body {" + " background-image: http://www.java2s.com/style/download.png;" + " background-repeat:no-repeat;" + " background-position:left top;" + " background-attachment: scroll;" + " color: #BBBBBB;" + "}" + "</style>" + "</head>" + "<body>" + "<h1>Heading 1</h1>"; String PARAGRAPH = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean eu nulla urna. Donec sit amet risus nisl, a porta enim. Quisque luctus, ligula eu scelerisque gravida, tellus quam vestibulum urna, ut aliquet sapien purus sed erat. Pellentesque consequat vehicula magna, eu aliquam magna interdum porttitor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Sed sollicitudin sapien non leo tempus lobortis. Morbi semper auctor ipsum, a semper quam elementum a. Aliquam eget sem metus."; gui.setPreferredSize(new Dimension(400, 100)); StringBuilder sb = new StringBuilder(); sb.append(HTML);/*from w w w. j a v a 2 s. c o m*/ for (int ii = 0; ii < 10; ii++) { sb.append("<h2>Header 2</h2>"); sb.append(PARAGRAPH); } JEditorPane jep = new JEditorPane(); jep.setOpaque(false); jep.setContentType("text/html"); jep.setText(sb.toString()); JScrollPane jsp = new JScrollPane(jep) { BufferedImage bg = new BufferedImage(350, 50, BufferedImage.TYPE_INT_RGB); @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.drawImage(bg, 0, 0, this); } }; jsp.getViewport().setOpaque(false); gui.add(jsp); Main bih = new Main(); JFrame f = new JFrame(); f.add(gui); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); }
From source file:DocumentModel.java
public static void main(String[] args) { final StyledDocument doc; final JTextPane textpane; JFrame f = new JFrame(); f.setTitle("Document Model"); JToolBar toolbar = new JToolBar(); JButton boldb = new JButton("bold"); JButton italb = new JButton("italic"); JButton strib = new JButton("strike"); JButton undeb = new JButton("underline"); toolbar.add(boldb);/*from w ww . j a v a 2 s .c o m*/ toolbar.add(italb); toolbar.add(strib); toolbar.add(undeb); f.add(toolbar, BorderLayout.NORTH); JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); JScrollPane pane = new JScrollPane(); textpane = new JTextPane(); textpane.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8)); doc = textpane.getStyledDocument(); Style style = textpane.addStyle("Bold", null); StyleConstants.setBold(style, true); style = textpane.addStyle("Italic", null); StyleConstants.setItalic(style, true); style = textpane.addStyle("Underline", null); StyleConstants.setUnderline(style, true); style = textpane.addStyle("Strike", null); StyleConstants.setStrikeThrough(style, true); boldb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Bold"), false); } }); italb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Italic"), false); } }); strib.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Strike"), false); } }); undeb.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { doc.setCharacterAttributes(textpane.getSelectionStart(), textpane.getSelectionEnd() - textpane.getSelectionStart(), textpane.getStyle("Underline"), false); } }); pane.getViewport().add(textpane); panel.add(pane); f.add(panel); f.setSize(new Dimension(380, 320)); f.setLocationRelativeTo(null); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); }
From source file:MainClass.java
static void addIt(JTabbedPane tabbedPane, String text) { JLabel label = new JLabel(text); JButton button = new JButton(text); JPanel panel = new JPanel(); panel.add(label); panel.add(button);/*from w w w. j a v a 2 s.co m*/ tabbedPane.addTab(text, panel); }
From source file:TabSample.java
static void addIt(JTabbedPane tabbedPane, String text) { JLabel label = new JLabel(text); JButton button = new JButton(text); JPanel panel = new JPanel(); panel.add(label); panel.add(button);/*from w w w . j ava 2 s. c o m*/ tabbedPane.addTab(text, panel); tabbedPane.setTabComponentAt(tabbedPane.getTabCount() - 1, new JTextField(text)); }
From source file:JListBackground.java
public static void addComponentsToPane(Container pane) { String[] bruteForceCode = { "int count = 0", "int m = mPattern.length();", "int n = mSource .length();", "outer:", " ++count;", " }", " return count;", "}" }; JList list = new JList(bruteForceCode); Border etch = BorderFactory.createEtchedBorder(); list.setBorder(BorderFactory.createTitledBorder(etch, "Brute Force Code")); JPanel listPanel = new JPanel(); listPanel.add(list); listPanel.setBackground(lightBlue);/* w w w . ja v a 2 s. c om*/ list.setBackground(lightBlue); pane.add(listPanel, BorderLayout.CENTER); pane.setBackground(lightBlue); }
From source file:Main.java
public static JPanel newPane(String labelText) { JPanel pane = new JPanel(new BorderLayout()); pane.add(newLabel(labelText)); pane.add(newButton("Open dialog"), BorderLayout.SOUTH); return pane;/*from w w w . j a v a 2s . c o m*/ }
From source file:Main.java
public static JPanel embed(Component comp) { JPanel pan = new JPanel(); pan.add(comp); return pan;/*from ww w. j a v a 2 s . co m*/ }
From source file:Main.java
/** * "Wraps" a component in a JPanel. In layouts where components tend to stretch, this is * useful because the JPanel will stretch but the actual component will not. * @return A JPanel containing the specified component. *//*from ww w . j av a 2 s. co m*/ public static JPanel box(Component component) { JPanel panel = new JPanel(); panel.add(component); return panel; }
From source file:Main.java
/** * @return a JPanel containing a label with the specified String as its text, and the component. *//*from ww w. j av a2s .c o m*/ public static JPanel labelComponent(JComponent c, String s) { JPanel panel = new JPanel(); panel.add(new JLabel(s)); panel.add(c); return panel; }