Display JPanels on top of JProgressBar - java

I am trying to display JLabels on top of a JProgressBar so that I can have fancy formatted text on a JProgressBar.
Here is the constructor for my component, which extends JPanel:
public InfoDisplay() {
//setLayout(new GridLayout(0, 1));
setLayout(new OverlayLayout(this));
lblPlayer = new JLabel();
lblPlayer.setName("Owner");
lblUnit = new JLabel();
lblUnit.setName("Unit");
lblCoords = new JLabel();
lblCoords.setName("Coordinates");
lblResources = new JLabel();
lblResources.setName("Resouces");
prgProgress = new JProgressBar();
prgProgress.setMaximum(4);
prgProgress.setStringPainted(true);
fmtDefault = "<html><font color='gray'>%s:</font> %s</html>";
JPanel pnlInfo = new JPanel();
pnlInfo.setLayout(new FlowLayout(FlowLayout.LEADING));
pnlInfo.setOpaque(false);
pnlInfo.add(lblPlayer);
pnlInfo.add(lblUnit);
pnlInfo.add(lblCoords);
pnlInfo.add(lblResources);
add(pnlInfo);
add(prgProgress);
displayInfo(Collections.EMPTY_LIST);
setProgress("Upkeep", 2);
}
I have tried using OverlayLayout, which achieves this goal when the form is created. However, when the JProgressBar updates, it covers up the other panel which holds the text fields.
I have also tried using a JLayeredPane, but I would have to write a custom layout manager for that to work and I was hoping to avoid doing all of the resizing code by hand for such a simple thing.
Does anyone have any other solutions or ideas?

An easier approach is to just add the panel to the progress bar then you don't have to worry about invoking repaint. The progress bar will automatically repaint its child component:
See the Swing tutorial on How to Use Progress Bars. I added the following code to the ProgressBarDemo:
progressBar.setLayout( new BorderLayout() );
JPanel child = new JPanel( new BorderLayout() );
child.setOpaque(false);
child.add( new JLabel("WEST"), BorderLayout.WEST );
child.add( new JLabel("EAST"), BorderLayout.EAST );
progressBar.add(child);

Related

Create a blue colored rectangular java bean [duplicate]

public void start_Gui() {
JFrame window = new JFrame("Client Program");
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
window.setContentPane(panel);
panel.setLayout(new GridLayout(1,2));
JLabel leftside = new JLabel();
leftside.setLayout(new GridLayout(2, 1));
JTextArea rightside = new JTextArea();
rightside.setEditable(false); //add scroll pane.
rightside.setBorder(BorderFactory.createLineBorder(Color.BLACK));
rightside.setLayout(new FlowLayout());
JTextArea client_text_input = new JTextArea();
client_text_input.setBorder(BorderFactory.createLineBorder(Color.BLACK));
leftside.add(client_text_input);
JLabel buttons_layer = new JLabel();
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
buttons_layer.setBorder(BorderFactory.createLineBorder(Color.BLACK));
buttons_layer.setLayout(new GridLayout(2, 1));
buttons_layer.add(login);
buttons_layer.add(logout);
leftside.add(buttons_layer);
panel.add(leftside);
panel.add(rightside);
window.setSize(300, 400);
window.setResizable(false);
window.setVisible(true);
}
I am working on a simple java chat client gui application. (the server etc, is done by others).
It is not a big project, but my only problem is that whatever I do to try to resize any components on the above GUI, won't work.
For example:
JTextArea client_text_input = new JTextArea();
client_text_input.setSize(100,200);
Won't work.
Thanks for the help.
In Swing, you have two options for layout: do everything manually or let a LayoutManager handle it for you.
Calling setSize() will only work when you're not using a LayoutManager. Since you're using a GridLayout you'll have to use other ways to specify what you want.
Try calling setPreferredSize() and setMinimumSize().
Two things - firstly you should be setting the preferredSize of the scrollpane, but secondly, trying to resize it inside the componentResized handler isn't a very effective technique because the 'resized' events aren't continuous.
check resizing text area in a JFrame
but setXxxSize (for ContainersChilds) works as chaims if you change from setSize() (for TopLayoutContainer) to setPreferredSize() and you have to call pack() before setVisible()

Java GUI shows blank until resize

I have written a java gui code for many options available on it. the gui is also set visible true but it doesn't show until I pick its border and drag them to resize the gui window. After manually resizing it, it shows everything. Also, the textlabels and the textfields and buttons are not in new lines, they are placed one after one. Please tell me whats wrong with that: here is a part of code:
public static void initGUI(){
JFrame fr = new JFrame();
Container cont = fr.getContentPane();
cont.setLayout( new FlowLayout( ) );
FlowLayout layout = new FlowLayout();
cont.setLayout(layout);
frame.setSize(200,300) ;
frame.setVisible(true) ;
JTextField tName = new JTextField(30);
JTextField tCNIC = new JTextField(15);
JLabel nameLabel = new JLabel("Name:");
JLabel cnicLabel = new JLabel("CNIC #:");
cont.add(nameLabel);
cont.add(tName);
cont.add(cnicLabel);
cont.add(tCNIC);
JButton Cancel = new JButton ("Canel" );
JButton OK = new JButton ("OK" );
savebtn.setPreferredSize(new Dimension(150, 50));
retbtn.setPreferredSize(new Dimension(150, 50));
cont.add(savebtn);
cont.add(retbtn);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
frame.setVisible(true) ;
The above statement should be invoked AFTER all the components have been added to the frame. So it should be the last statement in your method.
Also, you should be invoking:
frame.pack();
instead of setSize(), before making the frame visible so all the components are displayed at their preferred size.
frame.setVisible(true);
This Statement should be invoked in the last of adding other components to the Frame.

unable add multiple jpanel(each jpanel is a separate timer run concurrently) to jframe using arraylist only initial jpanel is getting addeed?

this is code is in jframe class with name"Timer1.java" and jpanel class name is "Timer_UI.java"
ArrayList<Timer_UI> mul_panels = new ArrayList<Timer_UI>();
public void jButton2ActionPerformed(java.awt.event.ActionEvent evt){
Timer_UI d_timer = new Timer_UI();
mul_panels.add(d_timer);
Timer_UI dis_timer = mul_panels.get(i);
i++;
dis_timer.setBackground(Color.white);
dis_timer.setBounds(34, 110, 434, 178);
add(dis_timer);
height = height + 230;
setSize(new Dimension(523,height));
}
the execution of the application
Execution of the application. Only one jpanel object is add on click again there is no timer added to frame:
Don't use setBounds() to set the size of your components.
Swing was designed to be used with layout managers.
If you want to add more timer panels horizontally then you need to use an appropriate layout manager. Maybe a BoxLayout, or a GridLayout.
Start by reading the section from the Swing tutorial on Layout Manager for more information and working examples
So the basic logic might be something like:
JPanel topPanel = new JPanel( new BorderLayout());
topPanel.add(label, BorderLayout.PAGE_START);
topPanel.add(button1, BorderLayout.LINE_START);
topPanel.add(button2, BorderLayout.LINE_END);
Box timerPanel = new Box.createVerticalBox();
frame.add(topPanel, BorderLayout.PAGE_START);
frame.add(timerPanel, BorderLayout.CENTER);
Now when you want to create a new timer the ActionListner code would be something like:
Timer_UI dis_timer = mul_panels.get(i);
dis_timer.setMaximumSize( dis_timer.getPreferredSize() );
timerPanel.add( dis_timer );
timerPanel.revalidate();
timerPanel.repaint();

JScrollPane - to JFrame or JPanel?

(using netbeans)
So for my project I need to add a JscrollPane so that the user can see all of the JTextArea output, a piechart and the two buttons I have added. This is the code I have implementing the JscrollPane. However it is causing the program to no longer produce an output screen. My question is do I need to add the JscrollPane to the JPanel or to the JFrame and if so what am I doing wrong (tried to include as much of the code as I thought was relevant)
P.S Should I change from Borderlayout to a Boxlayout? Would that make a difference in terms of adding a jscroll?
JFrame frame1 = new JFrame("Portfolio Results");
frame1.setSize(800,800);
// frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// output screen declartions
frame1.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
frame1.add(panel1,BorderLayout.PAGE_START);
panel1.setLayout(new BorderLayout());
JTextArea area1 = new JTextArea();
area1.setPreferredSize(new Dimension(600,600));
panel1.add(area1,BorderLayout.PAGE_START);
JScrollPane scp1 = new JScrollPane(frame1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
frame1.add(scp1);
//code for Pie chart and two button
DefaultPieDataset piedata = new DefaultPieDataset();
piedata.setValue("test", new Integer (100));
JFreeChart chart = ChartFactory.createPieChart("test", piedata, true, true, true);
PiePlot p = (PiePlot)chart.getPlot();
ChartPanel testpan = new ChartPanel(chart);
panel1.add(testpan,BorderLayout.CENTER);
JButton button= new JButton("SAVE");
// button.setPreferredSize(new Dimension(80,20));
// Listener listener = new Listener();
// button.addActionListener(this);
panel1.add(button,BorderLayout.PAGE_END);
JButton pbutton=new JButton("Print");
panel1.add(pbutton,BorderLayout.SOUTH);
You should init the JScrollPane with the object you want to scroll through.
In your example, it seems the JTextArea is the object you want, so:
JScrollPane scp1 = new JScrollPane(area1,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
From the Oracle docs:
JScrollPane(Component view)
Creates a JScrollPane that displays the contents of the specified component, where both horizontal and
vertical scrollbars appear whenever the component's contents are
larger than the view.
Also, see this Oracle example.

JFrame with JscrollPane

I'm trying to add few panel to JScrollpane and in turn to frame, but i'm not able to scroll.
Here is the code.
first_panel = new JPanel();
first_panel.setBackground(Color.red);
second_panel = new JPanel();
second_panel.setBackground(Color.blue);
third_panel = new JPanel();
third_panel.setBackground(Color.green);
fourth_panel = new JPanel();
fourth_panel.setBackground(Color.red);
fifith_panel = new JPanel();
fifith_panel.setBackground(Color.blue);
six_panel = new JPanel();
six_panel.setBackground(Color.green);
final_panel = new JPanel();
final_panel.setLayout(null);
final_panel.setBackground(Color.gray);
final_panel.add(first_panel);
final_panel.add(second_panel);
final_panel.add(third_panel);
final_panel.add(fourth_panel);
final_panel.add(fifith_panel);
final_panel.add(six_panel);
first_panel.setBounds(10,10,200,100);
second_panel.setBounds(10,10,200,200);
third_panel.setBounds(10,10,200,300);
fourth_panel.setBounds(10,10,200,400);
fifith_panel.setBounds(10,10,200,500);
six_panel.setBounds(10,10,200,600);
panel_scroll = new JScrollPane(final_panel);
panel_scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
final_panel.setBounds(0,0,400,400);
// scroll_panel = new JPanel();
// scroll_panel.setLayout(null);
// scroll_panel.add(panel_scroll);
// panel_scroll.setBounds(0,0,400,400);
frame.getContentPane().add(panel_scroll);
Can someone helpme with this. Thanks in advance.
use standard LayoutManagers instead of AbsoluteLayout, then Swing GUI will be continuously or proportionally resize with JFrame
use GridLayout() for final_panel in the case that all JPanels can have got the same size on the screen
override getPreferredSize for JPanel, for inital and correct PreferredSize on the screen (greater dimension for final_panel than JScrollPane)
is required to override ScrollPanel.setPreferredScrollableViewportSize(new Dimension(int, int));, then to call JFrame.pack() for inital and correct PreferredSize on the screen
is required to override JScrollPane.getVerticalScrollBar().setUnitIncrement(int); for natural scrolling

Categories