jpanel

add component to jpanel on runtime

半腔热情 提交于 2019-11-28 04:46:17
问题 I have a JPanel and JButton on the JFrame . on runtime add JLabel to JPanel When click JButton . I use of the following code: panel.setLayout(null); jLabel _lbl=new jLabel(); _lbl.setText("Label"); panel.add(_lbl); panel.validate(); but no display any JLabel in JPanel . 回答1: I see you create a JLabel called _lbl : JLabel _lbl=new JLabel(); but you never add it to your panel. Instead you add a new JLabel with no text to your panel: panel.add(new JLabel()); This would ofcourse construct an

Java setBounds not working with JPanel

爷,独闯天下 提交于 2019-11-28 04:45:46
问题 i am doing a small Gui in java. i am using setBounds methods to set the position of buttons etc on my JFrame , but problem is that when i use it with JPanel button is not visible on JFrame , and without JPanel its quite ok ,, see both the codes and please help me as i am beginner and facing these foolish problems . This one is working fine JFrame jframe = new JFrame("Working Fine"); jframe.setLayout(null); JButton jbutton = new JButton("Position Test"); jbutton.setBounds(0, 0, 100, 100);

loop through JPanel

爱⌒轻易说出口 提交于 2019-11-28 04:25:10
问题 In order to initialize all JTextfField s on a JPanel when users click a "clear button", I need to loop through the JPanel (instead of setting all individual field to ""). How can I use a for-each loop in order to iterate through the JPanel in search of JTextField s? 回答1: for (Component c : pane.getComponents()) { if (c instanceof JTextField) { ((JTextField)c).setText(""); } } But if you have JTextFields more deeply nested, you could use the following recursive form: void clearTextFields

JScrollPane resize containing JPanel when scrollbars appear

ε祈祈猫儿з 提交于 2019-11-28 03:30:46
问题 I have a small problem when using JScrollPane in my Java application. I have a JScrollPane containing a JPanel . This JPanel is dynamically updated with buttons (vertically ordered) that can be of any width. The JPanel automatically adjusts its width to the largest JButton component inside. Now when the vertical scrollbar appears, it takes away some space on the right side of my JPanel , which causes the largest buttons not to appear completely. I don't want to use a horizontal scrollbar in

JFileChooser embedded in a JPanel

自古美人都是妖i 提交于 2019-11-28 03:21:54
问题 I am writing a java program that needs a file open dialog. The file open dialog isn't difficult, I'm hoping to use a JFileChooser . My problem is that I would like to have a dual pane JFrame (consisting of 2 JPanels ). The left panel would have a JList , and the right panel would have a file open dialog. When I use JFileChooser.showOpenDialog() this opens the dialog box above all other windows, which isn't what I want. Is there any way to have the JFileChooser (or maybe another file selection

Can I create a BufferedImage from a JPanel without rendering in a JFrame?

99封情书 提交于 2019-11-28 03:03:19
问题 Is it possible to create a BufferedImage from a JPanel without first rendering it in a JFrame? I've searched everywhere I can think of and cannot find an answer. Can anyone help? Here is some sample code. If I don't un-comment the JFrame code, my BufferedImage is blank. test(){ // JFrame frame = new JFrame(); JPanel panel = new JPanel(); Dimension dim = new Dimension(50,50); panel.setMinimumSize(dim); panel.setMaximumSize(dim); panel.setPreferredSize(dim); JLabel label = new JLabel("hello");

How to print a large JPanel in several page

空扰寡人 提交于 2019-11-28 02:26:29
I want to print a very large panel and this panel contains some components like jtable, jlabel and others jpanel. Now i want to print it in differents pages. But i don't know how to do it. I have implemented Printable in my panel class. But if i print it, I get only one page. Try This ? package com.mymoney.util; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.print.PageFormat; import java.awt.print.Pageable; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob;

Fit image into the printing area

為{幸葍}努か 提交于 2019-11-28 02:20:32
I need to print images in Java. So I implemented print() method of Printable interface. But printer always prints only some piece of original image. How I can cause image to fit printing area (A4), so printer be able to print whole image, not piece of them? Now my code looks like this: public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { if (pageIndex >= images.size()) { return Printable.NO_SUCH_PAGE; } RenderedImage image; Graphics2D graphics2D = (Graphics2D) graphics; image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null,

How to disable all components in a JPanel

流过昼夜 提交于 2019-11-28 02:04:30
In my JPanel I have many components, including other JPanels, JLabels, JTextAreas, and JButtons. Becuase I want to implement a tutorial mode where another window appears and everything in my main JPanel is disabled as the new window explains each 'feature' one by one... I want a to know how to disable all the components that are inside my origiinal JPanel. I know you can use: component.setEnabled(false); But I don't want to write it for each component in my JPanel. I would like to know if it's possible to disable ALL components within my JPanel with a for loop or something? Note: There are

GridBagLayout panels alignment

社会主义新天地 提交于 2019-11-28 01:55:35
I have a little issue with the GridBag Layout Manager. I am trying to display 9 panels like this: To do so, I separated the Grid like this: For the panels 1 to 7 there is no problem, they show up just as I want. But the issue starts with the panels S8 and S9 . As you can see, the S8 and S9 takes up half the frame, but I can't make it display like this. The S8 ends at the start of S4 , and the S9 begins at the end of S4 , I cannot handle the half space. Only way I figured out is to put the S8 and S9 panel in another panel which takes all the frame width - but surely there must be a proper way