Ad selected panel throw JCombobox - java

I'm having problem with adding another JPanel to my frame when I'm using combobox.
I want to change the center panel according to the selection in combobox.
I made differehnt panel to all selections but it didn't add to my main panel.
I added the code.
thanks :)
import AllClasses.FlightCompany;
{
public class WorkerDialog extends JFrame {
private JPanel Worker;
private String[] LabelNames = { "Worker Type:", " Worker Name:" };
String [] str = { "Office", "Host",
"Pilot" };
JComboBox<String> ChooseBox = new JComboBox<>(str);
public JPanel MainPanel;
private JPanel [] p= new JPanel[3];
public WorkerDialog(FlightCompany f) {
super("Worker Dialog");
p[0] = new Office_Gui();
p[1] = new Host_Gui();
p[2] = new Pilot_Gui();
Worker = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 5));
JLabel LabelName = new JLabel(LabelNames[0]);
JLabel LabelName2 = new JLabel(LabelNames[1]);
JTextField fieldBox = new JTextField();
LabelName.setSize(40, 25);
ChooseBox.setPreferredSize(new Dimension(180, 22));
Worker.add(LabelName);
Worker.add(ChooseBox);
Worker.add(LabelName2);
fieldBox.setPreferredSize(new Dimension(180, 22));
Worker.add(fieldBox);
JPanel AddPanel = new JPanel(new GridLayout(2, 1, 1, 1));
AddPanel.add(new JButton("Add"));
AddPanel.add(new JButton("TakeOff"));
MainPanel = new JPanel(new BorderLayout(3, 3));
AddPanel.setPreferredSize(new Dimension(0, 110));
ChooseBox.addItemListener(new ItemListener() {
#Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
//String str = e.getActionCommand();
String jb = (String) ChooseBox.getSelectedItem();
if (jb.equals("Office")){
MainPanel.add(p[0],BorderLayout.CENTER);
System.out.println("Office");}
}
});
MainPanel.add(Worker, BorderLayout.NORTH);
MainPanel.add(AddPanel, BorderLayout.SOUTH);
add(MainPanel);
//pack();
setSize(560, 300);
setAlwaysOnTop(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);

What you want to do is use a CardLayout for your mainPanel, that will allow you to easily switch between panels. Then add all your panels to the mainPanel, specifying a name for the panel. That name will go in the combo box. When you want to show a certain panel, just call cardLayout.show(mainPanel, "nameOfPanel")
To learn more about Cardlayout see How to Use CardLayout. You can also see a simple example here
An aside: Use Java naming convention. Variables begin with lower case letters, using camel casing. ie:
ChooseBox → chooseBox
MainPanel → mainPanel
etc.

Related

How do I create the following GUI in Java Swing?

I want to create the following GUI with Java Swing.
Since I'm not experienced enough with Java Swing, I'm not sure how to exactly recreate that GUI.
I've tried using GridLayout which looks like this:
I've tried other LayoutManagers but due to my inexperience, I couldn't get anything even remotely resembling the GUI I want to achieve.
I probably have to use GridBagLayout but I've tried it and simply wasn't able to get anything done.
I'm not sure how to exactly use GridBagLayout, especially since there is a variance of the amount of colums needed (2, 2 and then 3).
Here is the code used for creating the second GUI:
import java.awt.*;
import javax.swing.*;
public class GUITest extends JFrame {
public GUITest() {
super("Testing Title");
Container pane = getContentPane();
pane.setLayout(new GridLayout(3,1));
pane.add(getHeader());
pane.add(getTextArea());
pane.add(getButtonPanel());
}
public JComponent getHeader() {
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(1,2));
labelPanel.setSize(getPreferredSize());
JLabel labelLocal = new JLabel("Left value: ", JLabel.CENTER);
JLabel labelDB = new JLabel("Right value: ", JLabel.CENTER);
labelPanel.add(labelLocal);
labelPanel.add(labelDB);
return labelPanel;
}
public JComponent getTextArea() {
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(1,2,5,0));
JTextArea testTextArea = new JTextArea();
testTextArea.setEditable(false);
JScrollPane sp1 = new JScrollPane(testTextArea);
JTextArea testTextArea2 = new JTextArea();
JScrollPane sp2 = new JScrollPane(testTextArea2);
testTextArea2.setEditable(false);
testTextArea.setText("Hello Hello Hello\nTesting!\ntesterino\ntesteroni");
testTextArea2.setText("Hello Hello Hello\nTesting!\ntest\nABC123\ncdef123\nhijk123");
textPanel.add(sp1);
textPanel.add(sp2);
return textPanel;
}
public JComponent getButtonPanel() {
JPanel inner = new JPanel();
inner.setLayout(new FlowLayout((FlowLayout.CENTER),0,100));
inner.add(new JButton("Do something"));
inner.add(new JButton("Do something different"));
inner.add(new JButton("Do something even more different"));
return inner;
}
public static void main(String[] args) {
GUITest e = new GUITest();
e.setSize(700, 500);
e.setVisible(true);
e.setResizable(false);
e.setDefaultCloseOperation(EXIT_ON_CLOSE);
e.setLocationRelativeTo(null);
}
}
I'm thankful for any kind of support!
You could try something like this:
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class Example {
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.setTitle("Testing Title");
jFrame.setLocationRelativeTo(null);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
JPanel listPanel = new JPanel(new GridLayout(0, 2, 10, 0));
JPanel leftListPanel = new JPanel(new BorderLayout(0, 10));
JLabel leftLabel = new JLabel("Left value:");
JTextArea leftTextArea = new JTextArea("Hello Hello Hello\nTesting!\ntest");
JScrollPane leftScrollPane = new JScrollPane(leftTextArea);
leftListPanel.add(leftLabel, BorderLayout.NORTH);
leftListPanel.add(leftScrollPane, BorderLayout.CENTER);
JPanel rightListPanel = new JPanel(new BorderLayout(0, 10));
JLabel rightLabel = new JLabel("Right value:");
JTextArea rightTextArea = new JTextArea("Hello Hello Hello\nTesting!\ntest");
JScrollPane rightScrollPane = new JScrollPane(rightTextArea);
rightListPanel.add(rightLabel, BorderLayout.NORTH);
rightListPanel.add(rightScrollPane, BorderLayout.CENTER);
listPanel.add(leftListPanel);
listPanel.add(rightListPanel);
mainPanel.add(listPanel, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel(new BorderLayout());
buttonsPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
buttonsPanel.add(new JButton("Do something"), BorderLayout.WEST);
buttonsPanel.add(new JButton("Do something different"), BorderLayout.CENTER);
buttonsPanel.add(new JButton("Do something even more different"), BorderLayout.EAST);
mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
jFrame.setContentPane(mainPanel);
jFrame.pack();
jFrame.setVisible(true);
}
}
Explanation:
Firstly I created a main JPanel with a BorderLayout. This JPanel will be split horizontally, the CENTRE component will be another JPanel containing the text areas and labels, and the SOUTH component will be a JPanel containing the buttons.
The JPanel that contains the text areas is given a GridLayout so that it can be easily split vertically, and is also given a hgap of 10 to add some spacing.
The left and right JPanels that are put into that are both the same. They have a BorderLayout with a vgap to add spacing. The NORTH component is a JLabel and the CENTRE component is a JScrollPane containing a JTextArea.
Finally, the SOUTH component of the main JPanel is another JPanel which is given a BorderLayout again. Three JButtons are added with WEST, CENTRE and EAST attributes allocated accordingly.
The overall result looks like:
Here is your code with just some little changes :)
import java.awt.*;
import javax.swing.*;
public class GUITest extends JFrame {
public GUITest() {
super("Testing Title");
Container pane = getContentPane();
pane.setLayout(new BorderLayout());//Modified Layout to BorderLayout
pane.add(getHeader(),BorderLayout.NORTH); //BorderLayout.NORTH
pane.add(getTextArea(),BorderLayout.CENTER);//BorderLayout.CENTER
pane.add(getButtonPanel(),BorderLayout.SOUTH);//BorderLayout.SOUTH
}
public JComponent getHeader() {
JPanel labelPanel = new JPanel();
labelPanel.setLayout(new GridLayout(1,2));
labelPanel.setSize(getPreferredSize());
JLabel labelLocal = new JLabel("Left value: ", JLabel.CENTER);
JLabel labelDB = new JLabel("Right value: ", JLabel.CENTER);
labelPanel.add(labelLocal);
labelPanel.add(labelDB);
return labelPanel;
}
public JComponent getTextArea() {
JPanel textPanel = new JPanel();
textPanel.setLayout(new GridLayout(1,2,5,0));
JTextArea testTextArea = new JTextArea();
testTextArea.setEditable(false);
JScrollPane sp1 = new JScrollPane(testTextArea);
JTextArea testTextArea2 = new JTextArea();
JScrollPane sp2 = new JScrollPane(testTextArea2);
testTextArea2.setEditable(false);
testTextArea.setText("Hello Hello Hello\nTesting!\ntesterino\ntesteroni");
testTextArea2.setText("Hello Hello Hello\nTesting!\ntest\nABC123\ncdef123\nhijk123");
textPanel.add(sp1);
textPanel.add(sp2);
return textPanel;
}
public JComponent getButtonPanel() {
JPanel inner = new JPanel();
inner.setLayout(new FlowLayout());//Modified to standard FlowLayout
inner.add(new JButton("Do something"));
inner.add(new JButton("Do something different"));
inner.add(new JButton("Do something even more different"));
return inner;
}
public static void main(String[] args) {
GUITest e = new GUITest();
e.pack(); //Modified setSize(700,500) to pack()
e.setVisible(true);
e.setResizable(false);
e.setDefaultCloseOperation(EXIT_ON_CLOSE);
e.setLocationRelativeTo(null);
}
}
GridLayout sizes all cells the same, i.e. your outer layout with 3 rows and 1 column makes 3 cells of all the same size.
Instead, use BorderLayout for your outer container and add the top, mid and lower panels with constraints BorderLayout.NORTH, BorderLayout.CENTER and BorderLayout.SOUTH respectively

BeautyEye LaF JTextField in JPopupMenu is disabled

I'm using the BeautyEye Laf for a Java Swing application.
I'm setting a JTextField inside a JPopupMenu. The JTextField appears disabled no matter what I do. The code is a bit complicated but I've made this snippet that's easily testable
public static void main(String[] s)
throws Exception
{
BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
final JPopupMenu popupTable = new JPopupMenu();
// find panel
JLabel findLabel = new JLabel("Filter for:");
findLabel.setPreferredSize(new Dimension(60, 20));
final JTextField findTextField = new JTextField();
findTextField.setColumns(10);
final JPanel container = new JPanel();
container.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 0));
container.add(findLabel);
container.add(findTextField);
popupTable.add(container);
JButton button = new JButton("Action");
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(button);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(popupTable, BorderLayout.CENTER);
panel.add(buttonPanel, BorderLayout.SOUTH);
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 800);
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setVisible(true);
button.addActionListener
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int x = (frame.getWidth() / 2);
int y = (frame.getHeight() / 2);
popupTable.show(frame, x, y);
}
}
);
}
If I remove the LaF (comment the first two lines) everything works as expected. The JTextField is editable. I'm asking here first in hope that I'm doing something wrong. If this proves to be a bug I will post this as an issue on Github.
In the end it was a minor change in the library. From the author:
You can find class
org.jb2011.lnf.beautyeye.ch7_popup.TranslucentPopupFactory source code
at line 416, change "setFocusableWindowState(false);" to
"setFocusableWindowState(true);" or just delete this line.

Effect on JTextfield due to JComboBox

I am using JCombobox and just below that there is a panel which contains JTextFields. Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself. I don't know what to do. I am unable to post a pic of the problem here because i am a new user and my reputation is below 10.
public class MainClass {
public static void main(String args[]){
Form form = new Form();
int width = 400;
int height = 400;
form.setSize(width, height);
form.setVisible(true);
form.setTitle("Create Network");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
form.setLocation((screenSize.width-width)/2, (screenSize.height-height)/2);
}
}
This is the form class
public class Form extends JFrame {
private JLabel ipAddress, networkTopo, numNodes;
private JTextField nodes;
private JButton addIp;
private JButton removeIp;
private JButton next, back;
private JPanel jPanel, jPanel1, jPanel2, commonPanel;
private JComboBox<String> dropDown;
private String[] topologies = { "Grid", "Diagnol Grid", "Bus", "Ring",
"Star" };
public Form() {
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
setResizable(false);
this.getContentPane().setBackground(Color.WHITE);
// add(Box.createRigidArea(new Dimension(0,10)));
GridLayout commonPanelLayout = new GridLayout(0, 2);
commonPanelLayout.setVgap(10);
commonPanel = new JPanel(commonPanelLayout);
commonPanel.setVisible(true);
commonPanel.setAlignmentX(commonPanel.LEFT_ALIGNMENT);
commonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
commonPanel.setBackground(Color.white);
numNodes = new JLabel("Number of Nodes");
commonPanel.add(numNodes);
nodes = new JTextField(20);
commonPanel.add(nodes);
networkTopo = new JLabel("Network Topology");
commonPanel.add(networkTopo);
dropDown = new JComboBox<String>(topologies);
dropDown.setBackground(Color.WHITE);
commonPanel.add(dropDown);
add(commonPanel);
add(Box.createRigidArea(new Dimension(0, 10)));
ipAddress = new JLabel("IP Addresses");
ipAddress.setAlignmentX(ipAddress.LEFT_ALIGNMENT);
ipAddress.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 0));
add(ipAddress);
add(Box.createRigidArea(new Dimension(0, 10)));
jPanel = new JPanel(new FlowLayout());
jPanel.setVisible(true);
jPanel.setAlignmentX(jPanel.LEFT_ALIGNMENT);
jPanel.setBackground(Color.WHITE);
// jPanel1.setAutoscrolls(true);
add(jPanel);
GridLayout layout = new GridLayout(0, 1);
jPanel1 = new JPanel();
layout.setVgap(10);
// jPanel1.setBackground(Color.WHITE);
jPanel1.setVisible(true);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(jPanel1);
scroll.setAutoscrolls(true);
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setPreferredSize(new Dimension(300, 150));
jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width,
Integer.MAX_VALUE));
jPanel.add(scroll);
jPanel2 = new JPanel(new GridLayout(0, 1, 10, 10));
jPanel2.setBackground(Color.WHITE);
jPanel2.setVisible(true);
jPanel.add(jPanel2);
addIp = new JButton("Add");
jPanel2.add(addIp);
removeIp = new JButton("Remove");
jPanel2.add(removeIp);
JPanel savePanel = new JPanel(new FlowLayout());
savePanel.setVisible(true);
savePanel.setAlignmentX(savePanel.LEFT_ALIGNMENT);
savePanel.setBackground(Color.white);
back = new JButton("Back");
back.setAlignmentX(next.LEFT_ALIGNMENT);
back.setEnabled(false);
savePanel.add(back);
next = new JButton("Next");
next.setAlignmentX(next.LEFT_ALIGNMENT);
savePanel.add(next);
add(savePanel);
AddIPEvent addIPEvent = new AddIPEvent();
addIp.addActionListener(addIPEvent);
RemoveIPEvent removeIPEvent = new RemoveIPEvent();
removeIp.addActionListener(removeIPEvent);
}
public class AddIPEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
JPanel subPanel = new JPanel(new FlowLayout());
// subPanel.setBackground(Color.WHITE);
subPanel.setVisible(true);
JCheckBox jCheckBox = new JCheckBox();
// jCheckBox.setBackground(Color.WHITE);
subPanel.add(jCheckBox);
JTextField ip = new JTextField(12);
subPanel.add(ip);
JTextField nodesInIp = new JTextField(6);
nodesInIp.setVisible(false);
subPanel.add(nodesInIp);
jPanel1.add(subPanel);
jPanel1.repaint();
jPanel1.revalidate();
}
}
public class RemoveIPEvent implements ActionListener {
public void removeIP(Container container) {
for (Component c : container.getComponents()) {
if (c instanceof JCheckBox) {
if (((JCheckBox) c).isSelected()) {
jPanel1.remove(container);
jPanel.revalidate();
jPanel1.repaint();
}
} else if (c instanceof Container) {
removeIP((Container) c);
}
}
}
public void actionPerformed(ActionEvent e) {
removeIP(jPanel1);
}
}
}
After Clicking on the Add button and then clicking on the JComboBox will make portion of JTextField dissapear. Could someone pls point out the mistake?
Whenever i click on the dropdown (JCombobox) some portion of JTextField disappears itself.
//jPanel1.setPreferredSize(new Dimension(scroll.getPreferredSize().width, Integer.MAX_VALUE));
Don't set the preferred size of components. The layout manager will determine the preferred size as components are added/removed. Then scrollbars will appear as required.
jPanel1.repaint();
jPanel1.revalidate();
The order should be:
jPanel1.revalidate();
jPanel1.repaint();
The revalidate() first invokes the layout manager before it is repainted.
//form.setSize(width, height);
form.pack();
Again, don't try to set the size. Let the layout managers do their jobs. The pack() will size the frame based on the sizes of the components added to the frame.

Java Swing JButton must create 4 new objects and add to JPanel

Okay, so when I press the JButton menuselect1, I want it to create 4 new objecs, attack1 2 3 and 4, and then add them to the JPanel fightmenu.
This is my code so far, it's a mini pokemon game.
First I create all my objects, and then I set the sizes and adds them to the different JPanels
public class MainFrame extends JFrame {
JPanel mainwindow = new JPanel();
JPanel bottom = new JPanel();
JPanel combat = new JPanel();
JPanel selectionmenu = new JPanel();
JPanel fightmenu = new JPanel();
JButton menuselect1 = new JButton("Fight");
JButton menuselect2 = new JButton("Minimons");
JButton menuselect3 = new JButton("Bag");
JButton menuselect4 = new JButton("Run");
JButton attack1 = new JButton("Tackle");
JButton attack2 = new JButton("Lightningbolt");
JButton attack3 = new JButton("Thunder-Shock");
JButton attack4 = new JButton("Hyper-Beam");
JButton poke1 = new JButton("Ekans");
JButton poke2 = new JButton("Pikachu");
public static void main(String[] args){
new MainFrame();
}
public MainFrame(){
super("MiniMon");
setSize(640,640);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(mainwindow);
// SIZES
combat.setPreferredSize(new Dimension(640,452));
bottom.setPreferredSize(new Dimension(640,160));
selectionmenu.setPreferredSize(new Dimension(320,160));
fightmenu.setPreferredSize(new Dimension(320,160));
mainwindow.setLayout(new BorderLayout());
mainwindow.add(combat, BorderLayout.NORTH);
mainwindow.add(bottom, BorderLayout.SOUTH);
combat.setLayout(new BorderLayout());
combat.add(poke1, BorderLayout.NORTH);
combat.add(poke2, BorderLayout.SOUTH);
bottom.setLayout(new BorderLayout());
bottom.add(selectionmenu, BorderLayout.EAST);
bottom.add(fightmenu, BorderLayout.WEST);
selectionmenu.setLayout(new GridLayout(2,2));
selectionmenu.add(menuselect1);
selectionmenu.add(menuselect2);
selectionmenu.add(menuselect3);
selectionmenu.add(menuselect4);
fightmenu.setLayout(new GridLayout(2,2));
setVisible(true);
}
}
I set up my fightmenu to use a 2x2 gridlayout, so I just need to add the 4 objects whenever I press the JButton menuselect1. I'm not really sure how to go about this. I know I should add an eventlistener, but when I tried, it did nothing at all.
I tried doing this:
fightmenu.setLayout(new GridLayout(2,2));
menuselect1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fightmenupress();
}
private void fightmenupress() {
fightmenu.add(attack1);
fightmenu.add(attack2);
fightmenu.add(attack3);
fightmenu.add(attack4);
}
} );
But it just did nothing.
When you add (or remove) components to a visible GUI, the basic code is:
panel.add(...);
panel.revalidate(); // to invoke the layout manager
panel.repaint(); // to repaint all the components on the panel
I added revalidate and repaint, and it worked!
private void fightmenupress() {
fightmenu.add(attack1);
fightmenu.add(attack2);
fightmenu.add(attack3);
fightmenu.add(attack4);
fightmenu.revalidate();
fightmenu.repaint();
}
} );

JList not updating when set to new DefaultListModel

public class MainMenu extends JFrame {
private JPanel panel,file_list_panel;
private JPanel contentPane;
private JMenuBar menuBar;
private JMenu mnNewMenu1,mnNewMenu2,mnNewMenu3;
private JMenuItem mt1,mt2,mt3;
private JPanel right,left ,bottom;
private JSplitPane spver ,sphor;
private JTabbedPane tabbedPane;
private JLabel label;
private JList<String> list_1;
private JScrollPane jscroll_list;
private DefaultListModel listmodel_1 = new DefaultListModel();
public MainMenu() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(0,0,screenSize.width, screenSize.height);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
int intValue = Integer.parseInt( "EEEEEE",16);
Color aColor = new Color( intValue );
UIManager.put("TabbedPane.background", new Color(230, 216, 174));
UIManager.put("TabbedPane.selected", Color.WHITE);
UIManager.put("TabbedPane.contentAreaColor",aColor );
UIManager.put("TabbedPane.focus", Color.WHITE);
setTitle("Main Menu");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//setBounds(100, 100, 848, 680);
//setLocation(10, 10);
setLocationRelativeTo(null); // set jFrame alignment to center
//parent(first) panel
contentPane = new JPanel();
contentPane.setBackground(Color.BLACK);
contentPane.setBorder(new EmptyBorder(2, 2, 2, 2));
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout(0, 0));
//Main Menu BAR
menuBar = new JMenuBar();
menuBar.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 11));
setJMenuBar(menuBar);
//Menu 1
mnNewMenu1 = new JMenu("New menu");
menuBar.add(mnNewMenu1);
//Menu 1 MenuItem 1
mt1 = new JMenuItem("Browse New Project");
mt1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
BrowseScreen frame = new BrowseScreen();
frame.setVisible(true);
}
});
mnNewMenu1.add(mt1);
//second panel
panel = new JPanel();
contentPane.add(panel, BorderLayout.CENTER);
panel.setLayout(new BorderLayout(0, 0));
spver =new JSplitPane(JSplitPane.VERTICAL_SPLIT);
spver.setDividerLocation(500);
spver.setEnabled(false);
bottom=new JPanel();
sphor =new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
sphor.setEnabled(false);
spver.setTopComponent(sphor);
spver.setBottomComponent(bottom);
bottom.setLayout(null);
panel.add(spver);
sphor.setDividerLocation(180);
left =new JPanel();
right =new JPanel();
sphor.setRightComponent(right);
right.setLayout(new GridLayout(0, 1, 0, 0));
tabbedPane = new JTabbedPane(JTabbedPane.TOP);
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
JPanel tab1 = new JPanel();
tabbedPane.addTab("fffa", tab1);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
JPanel tab2 = new JPanel();
tabbedPane.addTab("TAB1", tab2);
tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
right.add(tabbedPane);
sphor.setLeftComponent(left);
left.setLayout(new BorderLayout(0, 0));
file_list_panel= new JPanel();
file_list_panel.setBackground(Color.BLACK);
file_list_panel.setForeground(Color.WHITE);
label = new JLabel("Java Files of Project");
label.setBackground(Color.BLACK);
label.setForeground(Color.WHITE);
label.setFont(new Font("Garamond", Font.BOLD, 14));
file_list_panel.add(label);
left.add(file_list_panel, BorderLayout.NORTH);
list_1 = new JList<String>(listmodel_1);
jscroll_list = new JScrollPane(list_1);
left.add(jscroll_list, BorderLayout.CENTER);
}
public void setList(Vector<String> files){
listmodel_1.removeAllElements();
list_1.removeAll();
for(int i=0;i<files.size();i++)
listmodel_1. addElement(files.elementAt(i));
list_1.setModel(listmodel_1);
this.invalidate();
this.validate();
this.repaint();
}
}
setList is called from the browse window before it calls setVisible(false);
ie . this methods gets called when the browse window disappears .. it does all the things in method but does not update it in the MainMenu
public void setFileList(){
MainMenu mm= new MainMenu();
mm.setList(java_files);
}
list_1 -JList listmodel_1=DefaultListModel
i've tried to refreshing the frame but does not refresh after adding the new list to the Main Window ...
before JList is updated im browsing the the files in another window then it is set to setVisible(false) then MainMenu gets focused and calls the setList method but its not changing
You're creating a new JList<String> and copying the reference to your instance variable - but you're neither removing the old JList from the frame, nor adding the new one. Just changing the variable won't do that.
Rather than creating a new JList<String>, why don't you just replace the model of the existing one? Remove this line:
list_1=new JList<String>(listmodel_1);
and you may find it just works. (You're already setting the model in the subsequent line...)
Basically, you are creating a new JList and associating your model with it, but this has no effect on the JList that is on the screen
public void setList(Vector<String> files){
// Good...
listmodel_1.removeAllElements();
// Not required, has nothing to do with the items in the list
//list_1.removeAll();
// Good
for(int i=0;i<files.size();i++)
listmodel_1. addElement(files.elementAt(i));
// This here is your problem
//list_1=new JList<String>(listmodel_1);
// Because I have no idea what model list_1 is actually using...
list_1.setModel(listmodel_1);
//list_1.setSelectedIndex(0);
//this.invalidate();
//this.validate();
//this.repaint();
}

Categories