the components of the tabs are not shown - java

I have written the following code but the components of tabs are not shown....in fact I want to create tabs dynamically when I enter something in the text-box...the created tabs should contain a new text-field and button. this code is a sample and after it is completed I have another question.
please let me know where I have made mistake.
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
public class TestingTab extends JFrame {
private JTextField jTextField1;
private JButton jButton1;
static ArrayList<JPanel> ary = new ArrayList<JPanel>();
private int tabIndex=0;
static int index=0;
private JTabbedPane tabbedPane;
/**
* #param args
*/
public TestingTab(){
super("Testing Tab Frame");
setLayout(null);
Handler but1 = new Handler();
jTextField1 = new JTextField();
jTextField1.setVisible(true);
jTextField1.setBounds(12, 12, 85 , 30);
add(jTextField1);
jButton1 = new JButton("Button1");
jButton1.setVisible(true);
jButton1.setBounds(130, 12, 85, 30);
add(jButton1);
jButton1.addActionListener(but1);
tabbedPane = new JTabbedPane();
tabbedPane.setBounds(12, 54, 200, 220);
tabbedPane.setVisible(false);
add(tabbedPane);
pack();
setSize(250,110);
}
private class Handler implements ActionListener{
public void actionPerformed(ActionEvent evt){
String input = jTextField1.getText();
setSize(250,330);
JPanel inst = createPanel(input);
inst.setVisible(true);
tabbedPane.addTab(Integer.toString(tabIndex), inst);
tabbedPane.setVisible(true);
}
}
protected JPanel createPanel(String input){
JPanel inst = new JPanel();
inst.setVisible(true);
inst.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED),input));
JTextField textField = new JTextField();
textField.setVisible(true);
JButton button = new JButton();
button.setVisible(true);
inst.setLayout(null);
inst.add(button);
inst.add(textField);
ary.add(inst);
tabIndex=index;
index++;
return inst;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestingTab inst = new TestingTab();
inst.setVisible(true);
}
}

You're using null layout when creating a panel. That's the reason the components are not displayed. When layout property is null, the container uses no layout manager. This is called absolute positioning. In case of absolute positioning you must specify size and location of components. Absolute positing approach has many drawbacks and should be taken into consideration with care. Null layouts should/can be avoided in most cases.
Remove inst.setLayout(null); and you will see the button and text field.
Check out A Visual Guide to Layout Managers and Using Layout Managers for more details on layout managers.

Related

MouseListener inside loop isnt using updated variable values

I want to first apologize for my poor English.
I made some JPanels which I'm putting inside one JPanel and this last JPanel I'm putting it in one JScrollPane, each Jpanel has some JLabels that have text inside them. I've done that and everything is working until now, then I added a mouseListener to System.out.print the text inside the JLabels.
The texts are different, but on the console I'm getting the same text,
here's my code:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testa;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.event.*;
public class Test extends JFrame{ //see https://www.javatpoint.com/java-naming-conventions
private JLabel[] titles;
private JLabel[] descriptions;
private JPanel [] panels;
private JScrollPane jScrollPane1;
private JPanel bigPanel;
private final static int NUM_OF_RESULTS =10;
String identifier ;
String title;
String authors;
String resume;
String references;
public Test() throws IOException {
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(1000,500);
//jScrollPane1.setSize(1000, 500); and layouts. see following comments
bigPanel = new JPanel();
bigPanel.setBounds(30, 90, 950, 400);
//set layout to
GridLayout layout = new GridLayout(NUM_OF_RESULTS, 0);
bigPanel.setLayout(layout);
jScrollPane1 = new JScrollPane(bigPanel);
jScrollPane1.setBounds(30, 90, 950, 400);
getContentPane().add(jScrollPane1);
requetezQuery();
//pack(); //see https://stackoverflow.com/questions/22982295/what-does-pack-do
setVisible(true); //set visible typically comes last
}
public void requetezQuery() {
int actual=0;
titles = new JLabel[NUM_OF_RESULTS];
descriptions = new JLabel[NUM_OF_RESULTS];
panels = new JPanel[NUM_OF_RESULTS];
for(int i = 0; i<NUM_OF_RESULTS; i++){
title = "Title "+i;
resume = "Description "+i ;
titles[i]= new JLabel();
descriptions[i]= new JLabel();
panels[i]= new JPanel();
panels[i].setPreferredSize(new Dimension(250, 50));
panels[i].setLayout(new FlowLayout()); //FlowLayout is default for JPanel
titles[i].setText(title);
descriptions[i].setText(resume.substring(0, Math.min(resume.length(), 100))+"...");
titles[i].setForeground(Color.blue);
descriptions[i].setForeground(Color.black);
titles[i].addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println(title);
}
});
panels[i].add(titles[i]);
panels[i].add(descriptions[i]);
bigPanel.add(panels[i],i, 0);
}
}
public static void main(String args[]) throws IOException{
new Test();
}
}
I believe your reference to title refers to the last created title at runtime. What you can do to fix this is instead of printing the title, print the labels text. Try putting this inside of your mouseClicked method.
JLabel obj = (JLabel) me.getSource();
System.out.println(obj.getText());
Your mouse click event reference variable from outer class:
addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
System.out.println(title); // This `title` is a reference of Test.title variable
}
});
So all of your MouseAdapter references the same title (whose value is replaced by each loop) -> System.out.println all prints the same last title.

Java FlowLayout

I am writing some Java code that allows the user to see a frame with JLabel, JTextField and JButton.
I want the JLabel to be called "Count" and I have a problem with FlowLayout.
I want the interface to look like this:
Instead, I have this:
This is my code:
package modul1_Interfate_Grafice;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Exercitiu04 implements ActionListener {
private JFrame frame;
private JLabel labelCount;
private JTextField tfCount;
private JButton buttonCount;
private int count = 0;
public void go() {
frame = new JFrame("Java Counter");
labelCount = new JLabel("Counter");
labelCount.setLayout(new FlowLayout());
frame.getContentPane().add(BorderLayout.CENTER, labelCount);
tfCount = new JTextField(count + " ", 10);
tfCount.setEditable(false);
labelCount.add(tfCount);
buttonCount = new JButton("Count");
labelCount.add(buttonCount);
buttonCount.addActionListener(this);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 150);
frame.setLocation(400, 200);
}
#Override
public void actionPerformed(ActionEvent event) {
count++;
tfCount.setText(count + "");
}
public static void main(String[] args) {
Exercitiu04 a = new Exercitiu04();
a.go();
}
}
Solve it.
Instead of labelCount.setLayout(new FlowLayout());` i should have had
frame.setLayout(new FlowLayout());
From description of JLabel class,
JLabel is:
A display area for a short text string or an image, or both.
But here: labelCount.add(tfCount) and here labelCount.add(buttonCount) you're trying to put a textfield and a button into a label. In this case, positions of button and textfield are controlled by FlowLayout but position of the text in the label is not.
Instead of this, you should put all of your elements in common JPanel, like this:
...
frame = new JFrame("Java Counter");
frame.setLayout(new BorderLayout());
JPanel wrapper = new JPanel(); // JPanel has FlowLayout by default
labelCount = new JLabel("Counter");
labelCount.setLayout(new FlowLayout());
wrapper.add(labelCount);
tfCount = new JTextField(count + " ", 10);
tfCount.setEditable(false);
wrapper.add(tfCount);
buttonCount = new JButton("Count");
buttonCount.addActionListener(this);
wrapper.add(buttonCount);
frame.add(BorderLayout.CENTER, wrapper);
...
And, like MasterBlaster said, you should put swing methods in EDT.
There are only two things you should know about FlowLayout:
a) It is a default layout manager of the JPanel component
b) It is good for nothing.
This trivial layout cannot be achieved with FlowLayout.
When doing layouts in Swing, you should familiarize yourself
with some powerful layout managers. I recommend MigLayout and
GroupLayout.
package com.zetcode;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import net.miginfocom.swing.MigLayout;
/*
Simple UI with a MigLayout manager.
Author Jan Bodnar
Website zetcode.com
*/
public class MigLayoutCounterEx extends JFrame {
public MigLayoutCounterEx() {
initUI();
}
private void initUI() {
JLabel lbl = new JLabel("Counter");
JTextField field = new JTextField(10);
JButton btn = new JButton("Count");
createLayout(lbl, field, btn);
setTitle("Java Counter");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
setLayout(new MigLayout());
add(arg[0]);
add(arg[1]);
add(arg[2]);
pack();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
MigLayoutCounterEx ex = new MigLayoutCounterEx();
ex.setVisible(true);
});
}
}
The example is trivial. You just put the three components into the
cells.
Screenshot:
You shouldn't use setSize when dealing with FlowLayout. Instead use pack(). It makes the window just about big enough to fit all your components in. That should tidy things up for you

Jtabbedpane layout for button does not work

The below swing interface using tabbedpane works fine until i set layout for button, that is when i set the content pane loginpage to null (loginpage.setlayout(null)) the buttons disappear from the pane but works when i replace button with textfield or textarea.
package atmg;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
public class newgui extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
public JPanel clickfn= new JPanel(),gui= new JPanel(),trasgui= new JPanel(),contentpanel = new JPanel();
public static JTabbedPane Tabs = new JTabbedPane();
public JButton loginpage, filloginfo,createlogin ;
public JLabel label1 ,label2;
private CardLayout cardlayout = new CardLayout();
//static JPanel panel1 = new JPanel();
newguilogin nw;
public static void main(String[] args) {
newgui tf = new newgui();
tf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tf.setSize(700,700);
tf.setVisible(true);
tf.setLocation(400,20);
}
public newgui(int a)
{
System.out.println(a);
}
public newgui() {
super("ATM");
Initialize();
}
public void Initialize()
{
nw= new newguilogin();
nw.setgui(this);
loginpage = new JButton("Go to loginpage");
filloginfo = new JButton("go to fill log info");
createlogin = new JButton("create a new user");
// TODO Auto-generated constructor stub
actionListener a1 = new actionListener();
loginpage.addActionListener(a1);
filloginfo.addActionListener(a1);
createlogin.addActionListener(a1);
clickfn.add(loginpage);
clickfn.setSize(20,20);
clickfn.setLocation(50,50);
clickfn.add(filloginfo);
contentpanel.setLayout(cardlayout);
contentpanel.add(Tabs, "tab");
Tabs.add(clickfn,"panel1");
//Tabs.add(trasgui,"panel3");
this.setContentPane(contentpanel);
cardlayout.show(contentpanel, "tab");
}
public class actionListener implements ActionListener
{
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JButton src = (JButton)e.getSource();
if(src.equals(loginpage))
//threader = new Threading();
//Tabs.addTab("panel2", gui);
nw = new newguilogin();
nw.initialize();
}
}
}
that is when i set the content pane loginpage to null (loginpage.setlayout(null)) the buttons disappear
A good rule of thumb to follow with Swing layouts: never use null layouts.
While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
Understand also that you can nest JPanels, each using its own simple layouts, and thereby create complex GUI's with simple layout managers.

Is it possible that JButton and actionPerformed method are in different files?

Is it possible to have a button on one file and its actionPerformed(ActionEvent e) method in a different file? I am trying to add an actionListener to a button, choose1 which is in file trialdump.java but the actionPerformed(ActionEvent e) method is in file listen.java. I tried extending public class trialdump extends listen but it shows an error. Any idea how I can add the method to the button? Thanks.
Here is my code in file trialdump.java:
package Core;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.*;
import java.awt.*;
// Create a simple GUI window
public class trialdump {
private static void createWindow() {
// Create and set up the window.
JFrame frame = new JFrame("PDF Denoiser");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// My edit
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
JLabel label1 = new JLabel("Image File");
JLabel label2 = new JLabel("Destination");
JLabel label3 = new JLabel("Preview");
JTextField current = new JTextField();
JTextField dest = new JTextField();
JTextArea preview = new JTextArea();
preview.setEditable(false);
JScrollPane previewScrollPane = new JScrollPane(preview);
JButton choose1 = new JButton("Search1");
JButton choose2 = new JButton("Search2");
JButton algo1 = new JButton("MDWM");
JButton algo2 = new JButton("BFMR");
JButton algo3 = new JButton("Mine");
// Horizontal arrangement
layout.setHorizontalGroup(layout
.createSequentialGroup()
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(label1)
.addComponent(label2).addComponent(label3))
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(current)
.addComponent(dest).addComponent(preview))
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(choose1)
.addComponent(choose2).addComponent(algo1).addComponent(algo2).addComponent(algo3)));
layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);
// Vertical arrangement
layout.setVerticalGroup(layout
.createSequentialGroup()
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label1)
.addComponent(current).addComponent(choose1))
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2)
.addComponent(dest).addComponent(choose2))
.addGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(label3)
.addComponent(preview)
.addGroup(
layout.createSequentialGroup().addComponent(algo1).addComponent(algo2)
.addComponent(algo3))));
// Display the window.
frame.setLocationRelativeTo(null);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
createWindow();
}
}
And here is my code in listen.java:
package components;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
public class listen extends JPanel implements ActionListener{
static private String newline = "\n";
private JTextArea log;
private JFileChooser fc;
public listen() {
}
public void actionPerformed(ActionEvent e) {
//Set up the file chooser.
if (fc == null) {
fc = new JFileChooser();
//Add a custom file filter and disable the default
//(Accept All) file filter.
fc.addChoosableFileFilter(new imagefilter());
fc.setAcceptAllFileFilterUsed(false);
//Add custom icons for file types.
fc.setFileView(new imagefileview());
//Add the preview pane.
fc.setAccessory(new imagepreview(fc));
}
//Show it.
int returnVal = fc.showDialog(listen.this,
"Attach");
//Process the results.
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
} else {
log.append("Attachment cancelled by user." + newline);
}
log.setCaretPosition(log.getDocument().getLength());
//Reset the file chooser for the next time it's shown.
fc.setSelectedFile(null);
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Turn off metal's use of bold fonts
UIManager.put("swing.boldMetal", Boolean.FALSE);
}
});
}
}
You can add the listener defined in another class doing something like this
JButton choose1 = new JButton("Search1");
choose1.addActionListener(new listen());
BTW, you should take care of some more bits of your code:
Classs names should be typed in CamelCase with the first letter capital
Use proper names for your packages
I don't think that the main method in the listener class is making anything usefull
There is not need at all to extend from JPanel in your listener class
Try to avoid make empty constructor, like you are doing in listen(). At least call super(), or just, don't include it.
Just the sample code, you can make the changes on similar lines-
class Some extends JFrame {
private JButton button = new JButton("Something");
Some() {
button.addActionListener(new MyListener());
}
}
class MyListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
// do something
}
}
The proper way of doing this is by extending the the AbstractAction class adding all your action code to it's actionPerformed method, and then passing a new instance of it to either the JButton's constructor or by calling setAction on the JButton.
Here's a link to help clear it all out for you.

Replacing a JPanel with a different JPanel

Hi this is a bit of a basic question. In my code I create a gui in a constructor then nest a ActionListener class to handle button changes. This code will create the gui and the action listener runs through the actionPerformed method correctly. However, I've tried multiple ways to change the panel in the gui but I feel like the way I have the program set up it is not possible for this to work. Sorry if this is a repeat but after searching for a while on S.O. I haven't found a good example that would help me with my problem.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import org.math.plot.Plot2DPanel;
import org.math.plot.plotObjects.BaseLabel;
public class GraphGui extends JFrame {
//default width and height of the GUI
private static final int WIDTH = 1200;
private static final int HEIGHT = 700;
GraphPlot gp = new GraphPlot();
Plot2DPanel plotPanel =gp.determinePlotToPlot("duration");
/**
* This is the constructor that initializes the JFrame and the layout of the GUI.
* The radio buttons are also created here and grouped accordingly.
*/
public GraphGui() {
//title of GUI
setTitle("VibeTech Graph Gui");
//First JRadioButton for date vs duration
JRadioButton durToDate = new JRadioButton("Duration vs. Date");
durToDate.addActionListener(new RadioButtonListener());
durToDate.setActionCommand("duration");
durToDate.setSelected(true);
//JRadioButton for weight vs date
JRadioButton weightToDate = new JRadioButton("Weight vs. Date");
weightToDate.addActionListener(new RadioButtonListener());
weightToDate.setActionCommand("weight");
//JRadioButton for plan type vs date
JRadioButton planToDate = new JRadioButton("Plan vs. Date");
planToDate.addActionListener(new RadioButtonListener());
planToDate.setActionCommand("level");
//button group of the buttons to display them as one group
ButtonGroup group = new ButtonGroup();
group.add(planToDate);
group.add(weightToDate);
group.add(durToDate);
//create JPanel to add objects to
JPanel jplRadio = new JPanel();
jplRadio.setLayout(new GridLayout(0, 1));
//add radio buttons
jplRadio.add(planToDate);
jplRadio.add(weightToDate);
jplRadio.add(durToDate);
Plot2DPanel dvt = new Plot2DPanel();
dvt.addLinePlot("Duration over Time", gp.getDate(), gp.getDuration());
BaseLabel title = new BaseLabel("Duration over Time", Color.RED,
0.5, 1.1);
title.setFont(new Font("Courier", Font.BOLD, 20));
dvt.addPlotable(title);
dvt.setAxisLabels("Time", "Duration");
setLayout(new BorderLayout());
add(jplRadio, BorderLayout.WEST);
add(plotPanel, BorderLayout.EAST);
setSize(WIDTH, HEIGHT);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
//main method to run program
public static void main(String [ ] args)
{
//create new GUI
#SuppressWarnings("unused")
GraphGui test = new GraphGui();
}
//create a radio button listener to switch graphs on button press
class RadioButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("duration")) {
plotPanel = gp.determinePlotToPlot("duration");
} else if (e.getActionCommand().equals("weight")) {
plotPanel = gp.determinePlotToPlot("weight");
} else if (e.getActionCommand().equals("level")) {
plotPanel = gp.determinePlotToPlot("level");
}
//here is where I tried to do removes, adds, and validates but
//I have trouble getting to the frame itself to remove the JPanel
//component. I think this is a setup problem.
}
}
}
You would need to add the panel and revalidate/repaint the JFrame for it to appear:
add(plotPanel, BorderLayout.EAST);
revalidate();
repaint();
Better to use CardLayout to manage this type of functionality.
Try using CardLayout for switching between panels. Here is my solution for a similar question: https://stackoverflow.com/a/9377623/544983

Categories

Resources